Script

Namespace: dmScript Language: C++ Type: Defold C++ File: script.h Source: engine/script/src/dmsdk/script/script.h Include: dmsdk/script/script.h

Built-in scripting functions.

API

CheckHash

Type: FUNCTION Check if the value in the supplied index on the lua stack is a hash.

Parameters

Returns

CheckHash

Type: FUNCTION Check if the value in the supplied index on the lua stack is a hash.

Parameters

Returns

CheckHashOrString

Type: FUNCTION Check if the value in the supplied index on the lua stack is a hash or string. If it is a string, it gets hashed on the fly

Parameters

Returns

CheckHashOrString

Type: FUNCTION Check if the value in the supplied index on the lua stack is a hash or string. If it is a string, it gets hashed on the fly

Parameters

Returns

CheckMatrix4

Type: FUNCTION Check if the value in the supplied index on the lua stack is a dmVMath::Matrix4.

Notes

Parameters

Returns

CheckMatrix4

Type: FUNCTION Check if the value in the supplied index on the lua stack is a dmVMath::Matrix4.

Notes

Parameters

Returns

CheckQuat

Type: FUNCTION Check if the value in the supplied index on the lua stack is a dmVMath::Quat.

Notes

Parameters

Returns

CheckQuat

Type: FUNCTION Check if the value in the supplied index on the lua stack is a dmVMath::Quat.

Notes

Parameters

Returns

CheckTable

Type: FUNCTION Serialize a table to a buffer Supported types: LUA_TBOOLEAN, LUA_TNUMBER, LUA_TSTRING, Point3, Vector3, Vector4 and Quat Keys must be strings

Parameters

Returns

CheckTable

Type: FUNCTION Serialize a table to a buffer Supported types: LUA_TBOOLEAN, LUA_TNUMBER, LUA_TSTRING, Point3, Vector3, Vector4 and Quat Keys must be strings

Parameters

Returns

CheckURL

Type: FUNCTION Check if the value in the supplied index on the lua stack is a dmMessage::URL and returns it if so.

Parameters

Returns

CheckURL

Type: FUNCTION Get the current game object URL

Parameters

Returns

CheckURL

Type: FUNCTION Check if the value in the supplied index on the lua stack is a dmMessage::URL and returns it if so.

Parameters

Returns

CheckURL

Type: FUNCTION Get the current game object URL

Parameters

Returns

CheckVector3

Type: FUNCTION Check if the value in the supplied index on the lua stack is a dmVMath::Vector3.

Notes

Parameters

Returns

CheckVector3

Type: FUNCTION Check if the value in the supplied index on the lua stack is a dmVMath::Vector3.

Notes

Parameters

Returns

CheckVector4

Type: FUNCTION Check if the value in the supplied index on the lua stack is a dmVMath::Vector3.

Notes

Parameters

Returns

CheckVector4

Type: FUNCTION Check if the value in the supplied index on the lua stack is a dmVMath::Vector3.

Notes

Parameters

Returns

CreateCallback

Type: FUNCTION Stores the current Lua state plus references to the script instance (self) and the callback. Expects SetInstance() to have been called prior to using this method. The allocated data is created on the Lua stack and references are made against the instances own context table. If the callback is not explicitly deleted with DestroyCallback() the references and data will stay around until the script instance is deleted.

Parameters

Returns

Examples

static int SomeFunction(lua_State* L) // called from Lua
{
    LuaCallbackInfo* cbk = dmScript::CreateCallback(L, 1);
    ... store the callback for later
}

static void InvokeCallback(LuaCallbackInfo* cbk)
{
    lua_State* L = dmScript::GetCallbackLuaContext(cbk);
    DM_LUA_STACK_CHECK(L, 0);

    if (!dmScript::SetupCallback(callback))
    {
        return;
    }

    lua_pushstring(L, "hello");

    dmScript::PCall(L, 2, 0); // self + # user arguments

    dmScript::TeardownCallback(callback);
    dmScript::DestroyCallback(cbk); // only do this if you're not using the callback again
}

CreateCallback

Type: FUNCTION Stores the current Lua state plus references to the script instance (self) and the callback. Expects SetInstance() to have been called prior to using this method. The allocated data is created on the Lua stack and references are made against the instances own context table. If the callback is not explicitly deleted with DestroyCallback() the references and data will stay around until the script instance is deleted.

Parameters

Returns

Examples

static int SomeFunction(lua_State* L) // called from Lua
{
    LuaCallbackInfo* cbk = dmScript::CreateCallback(L, 1);
    ... store the callback for later
}

static void InvokeCallback(LuaCallbackInfo* cbk)
{
    lua_State* L = dmScript::GetCallbackLuaContext(cbk);
    DM_LUA_STACK_CHECK(L, 0);

    if (!dmScript::SetupCallback(callback))
    {
        return;
    }

    lua_pushstring(L, "hello");

    dmScript::PCall(L, 2, 0); // self + # user arguments

    dmScript::TeardownCallback(callback);
    dmScript::DestroyCallback(cbk); // only do this if you're not using the callback again
}

DestroyCallback

Type: FUNCTION Deletes the Lua callback

Parameters

DestroyCallback

Type: FUNCTION Deletes the Lua callback

Parameters

DM_LUA_ERROR

Type: MACRO This macro will verify that the Lua stack size hasn’t been changed before throwing a Lua error, which will long-jump out of the current function. This macro can only be used together with DM_LUA_STACK_CHECK and should be prefered over manual checking of the stack.

Parameters

Examples

static int ModuleFunc(lua_State* L)
{
    DM_LUA_STACK_CHECK(L, 1);
    if (some_error_check(L))
    {
        return DM_LUA_ERROR("some error message");
    }
    lua_pushnumber(L, 42);
    return 1;
}

DM_LUA_ERROR

Type: MACRO This macro will verify that the Lua stack size hasn’t been changed before throwing a Lua error, which will long-jump out of the current function. This macro can only be used together with DM_LUA_STACK_CHECK and should be prefered over manual checking of the stack.

Parameters

Examples

static int ModuleFunc(lua_State* L)
{
    DM_LUA_STACK_CHECK(L, 1);
    if (some_error_check(L))
    {
        return DM_LUA_ERROR("some error message");
    }
    lua_pushnumber(L, 42);
    return 1;
}

DM_LUA_STACK_CHECK

Type: MACRO Diff is the expected difference of the stack size. If luaL_error, or another function that executes a long-jump, is part of the executed code, the stack guard cannot be guaranteed to execute at the end of the function. In that case you should manually check the stack using lua_gettop. In the case of luaL_error, see DM_LUA_ERROR.

Parameters

Examples

DM_LUA_STACK_CHECK(L, 1);
lua_pushnumber(L, 42);

DM_LUA_STACK_CHECK

Type: MACRO Diff is the expected difference of the stack size. If luaL_error, or another function that executes a long-jump, is part of the executed code, the stack guard cannot be guaranteed to execute at the end of the function. In that case you should manually check the stack using lua_gettop. In the case of luaL_error, see DM_LUA_ERROR.

Parameters

Examples

DM_LUA_STACK_CHECK(L, 1);
lua_pushnumber(L, 42);

GetCallbackLuaContext

Type: FUNCTION Gets the Lua context from a callback struct

Parameters

Returns

GetCallbackLuaContext

Type: FUNCTION Gets the Lua context from a callback struct

Parameters

Returns

GetInstance

Type: FUNCTION Retrieve current script instance from the global table and place it on the top of the stack, only valid when set. (see dmScript::GetMainThread)

Parameters

GetInstance

Type: FUNCTION Retrieve current script instance from the global table and place it on the top of the stack, only valid when set. (see dmScript::GetMainThread)

Parameters

GetLuaState

Type: FUNCTION Retrieve Lua state from the context

Parameters

Returns

GetLuaState

Type: FUNCTION Retrieve Lua state from the context

Parameters

Returns

GetMainThread

Type: FUNCTION Retrieve the main thread lua state from any lua state (main thread or coroutine).

Parameters

Returns

Examples

How to create a Lua callback

dmScript::LuaCallbackInfo* g_MyCallbackInfo = 0;

static void InvokeCallback(dmScript::LuaCallbackInfo* cbk)
{
    if (!dmScript::IsCallbackValid(cbk))
        return;

    lua_State* L = dmScript::GetCallbackLuaContext(cbk);
    DM_LUA_STACK_CHECK(L, 0)

    if (!dmScript::SetupCallback(cbk))
    {
        dmLogError("Failed to setup callback");
        return;
    }

    lua_pushstring(L, "Hello from extension!");
    lua_pushnumber(L, 76);

    dmScript::PCall(L, 3, 0); // instance + 2

    dmScript::TeardownCallback(cbk);
}

static int Start(lua_State* L)
{
    DM_LUA_STACK_CHECK(L, 0);

    g_MyCallbackInfo = dmScript::CreateCallback(L, 1);

    return 0;
}

static int Update(lua_State* L)
{
    DM_LUA_STACK_CHECK(L, 0);

    static int count = 0;
    if( count++ == 5 )
    {
        InvokeCallback(g_MyCallbackInfo);
        if (g_MyCallbackInfo)
            dmScript::DestroyCallback(g_MyCallbackInfo);
        g_MyCallbackInfo = 0;
    }
    return 0;
}

GetMainThread

Type: FUNCTION Retrieve the main thread lua state from any lua state (main thread or coroutine).

Parameters

Returns

Examples

How to create a Lua callback

dmScript::LuaCallbackInfo* g_MyCallbackInfo = 0;

static void InvokeCallback(dmScript::LuaCallbackInfo* cbk)
{
    if (!dmScript::IsCallbackValid(cbk))
        return;

    lua_State* L = dmScript::GetCallbackLuaContext(cbk);
    DM_LUA_STACK_CHECK(L, 0)

    if (!dmScript::SetupCallback(cbk))
    {
        dmLogError("Failed to setup callback");
        return;
    }

    lua_pushstring(L, "Hello from extension!");
    lua_pushnumber(L, 76);

    dmScript::PCall(L, 3, 0); // instance + 2

    dmScript::TeardownCallback(cbk);
}

static int Start(lua_State* L)
{
    DM_LUA_STACK_CHECK(L, 0);

    g_MyCallbackInfo = dmScript::CreateCallback(L, 1);

    return 0;
}

static int Update(lua_State* L)
{
    DM_LUA_STACK_CHECK(L, 0);

    static int count = 0;
    if( count++ == 5 )
    {
        InvokeCallback(g_MyCallbackInfo);
        if (g_MyCallbackInfo)
            dmScript::DestroyCallback(g_MyCallbackInfo);
        g_MyCallbackInfo = 0;
    }
    return 0;
}

GetStringFromHashOrString

Type: FUNCTION Gets as good as possible printable string from a hash or string

Parameters

Returns

GetStringFromHashOrString

Type: FUNCTION Gets as good as possible printable string from a hash or string

Parameters

Returns

HContext

Type: TYPEDEF The script context

HContext

Type: TYPEDEF The script context

IsCallbackValid

Type: FUNCTION Check if Lua callback is valid.

Parameters

IsCallbackValid

Type: FUNCTION Check if Lua callback is valid.

Parameters

IsHash

Type: FUNCTION Check if the value at #index is a hash

Parameters

Returns

IsHash

Type: FUNCTION Check if the value at #index is a hash

Parameters

Returns

IsInstanceValid

Type: FUNCTION Check if the script instance in the lua state is valid. The instance is assumed to have been previously set by dmScript::SetInstance.

Parameters

Returns

IsInstanceValid

Type: FUNCTION Check if the script instance in the lua state is valid. The instance is assumed to have been previously set by dmScript::SetInstance.

Parameters

Returns

IsMatrix4

Type: FUNCTION Check if the value at #index is a dmVMath::Matrix4*

Parameters

Returns

IsMatrix4

Type: FUNCTION Check if the value at #index is a dmVMath::Matrix4*

Parameters

Returns

IsQuat

Type: FUNCTION Check if the value at #index is a dmVMath::Quat*

Parameters

Returns

IsQuat

Type: FUNCTION Check if the value at #index is a dmVMath::Quat*

Parameters

Returns

IsURL

Type: FUNCTION Check if the value at #index is a URL

Parameters

Returns

IsURL

Type: FUNCTION Check if the value at #index is a URL

Parameters

Returns

IsVector3

Type: FUNCTION Check if the value at #index is a dmVMath::Vector3*

Parameters

Returns

IsVector3

Type: FUNCTION Check if the value at #index is a dmVMath::Vector3*

Parameters

Returns

IsVector4

Type: FUNCTION Check if the value at #index is a dmVMath::Vector4*

Parameters

Returns

IsVector4

Type: FUNCTION Check if the value at #index is a dmVMath::Vector4*

Parameters

Returns

JsonToLua

Type: FUNCTION Convert a Json string to Lua table.

Notes

Parameters

Returns

JsonToLua

Type: FUNCTION Convert a Json string to Lua table.

Notes

Parameters

Returns

LuaCallbackInfo

Type: STRUCT callback info struct that will hold the relevant info needed to make a callback into Lua

LuaCallbackInfo

Type: STRUCT callback info struct that will hold the relevant info needed to make a callback into Lua

LuaToJson

Type: FUNCTION Convert a Lua table to a Json string

Parameters

Returns

LuaToJson

Type: FUNCTION Convert a Lua table to a Json string

Parameters

Returns

PCall

Type: FUNCTION This function wraps lua_pcall with the addition of specifying an error handler which produces a backtrace. In the case of an error, the error is logged and popped from the stack.

Parameters

Returns

PCall

Type: FUNCTION This function wraps lua_pcall with the addition of specifying an error handler which produces a backtrace. In the case of an error, the error is logged and popped from the stack.

Parameters

Returns

PushDDF

Type: FUNCTION Push DDF message to Lua stack

Parameters

PushDDF

Type: FUNCTION Push DDF message to Lua stack

Parameters

PushHash

Type: FUNCTION Push a hash value onto the supplied lua state, will increase the stack by 1.

Parameters

PushHash

Type: FUNCTION Push a hash value onto the supplied lua state, will increase the stack by 1.

Parameters

PushMatrix4

Type: FUNCTION Push a matrix4 value onto the Lua stack. Will increase the stack by 1.

Parameters

PushMatrix4

Type: FUNCTION Push a matrix4 value onto the Lua stack. Will increase the stack by 1.

Parameters

PushQuat

Type: FUNCTION Push a quaternion value onto Lua stack. Will increase the stack by 1.

Parameters

PushQuat

Type: FUNCTION Push a quaternion value onto Lua stack. Will increase the stack by 1.

Parameters

PushURL

Type: FUNCTION Push a URL value onto the supplied lua state, will increase the stack by 1.

Parameters

PushURL

Type: FUNCTION Push a URL value onto the supplied lua state, will increase the stack by 1.

Parameters

PushVector3

Type: FUNCTION Push a dmVMath::Vector3 value onto the supplied lua state, will increase the stack by 1.

Parameters

PushVector3

Type: FUNCTION Push a dmVMath::Vector3 value onto the supplied lua state, will increase the stack by 1.

Parameters

PushVector4

Type: FUNCTION Push a dmVMath::Vector4 value onto the supplied lua state, will increase the stack by 1.

Parameters

PushVector4

Type: FUNCTION Push a dmVMath::Vector4 value onto the supplied lua state, will increase the stack by 1.

Parameters

Ref

Type: FUNCTION Creates and returns a reference, in the table at index t, for the object at the top of the stack (and pops the object). It also tracks number of global references kept.

Parameters

Returns

Ref

Type: FUNCTION Creates and returns a reference, in the table at index t, for the object at the top of the stack (and pops the object). It also tracks number of global references kept.

Parameters

Returns

RefInInstance

Type: FUNCTION Creates a reference to the value at top of stack, the ref is done in the current instances context table. Expects SetInstance() to have been set with an value that has a meta table with META_GET_INSTANCE_CONTEXT_TABLE_REF method.

Parameters

Returns

RefInInstance

Type: FUNCTION Resolves a url in string format into a dmMessage::URL struct. Special handling for:

Parameters

Returns

RefInInstance

Type: FUNCTION Creates a reference to the value at top of stack, the ref is done in the current instances context table. Expects SetInstance() to have been set with an value that has a meta table with META_GET_INSTANCE_CONTEXT_TABLE_REF method.

Parameters

Returns

RefInInstance

Type: FUNCTION Resolves a url in string format into a dmMessage::URL struct. Special handling for:

Parameters

Returns

ResolveURL

Type: FUNCTION Resolves the value in the supplied index on the lua stack to a URL. It long jumps (calls luaL_error) on failure. It also gets the current (caller) url if the a pointer is passed to out_default_url

Parameters

Returns

ResolveURL

Type: FUNCTION Resolves the value in the supplied index on the lua stack to a URL. It long jumps (calls luaL_error) on failure. It also gets the current (caller) url if the a pointer is passed to out_default_url

Parameters

Returns

SetInstance

Type: FUNCTION Sets the current script instance Set the value on the top of the stack as the instance into the global table and pops it from the stack. (see dmScript::GetMainThread)

Parameters

SetInstance

Type: FUNCTION Sets the current script instance Set the value on the top of the stack as the instance into the global table and pops it from the stack. (see dmScript::GetMainThread)

Parameters

SetupCallback

Type: FUNCTION The Lua stack after a successful call:

   [-4] old instance
   [-3] context table
   [-2] callback
   [-1] self

In the event of an unsuccessful call, the Lua stack is unchanged

Parameters

Returns

SetupCallback

Type: FUNCTION The Lua stack after a successful call:

   [-4] old instance
   [-3] context table
   [-2] callback
   [-1] self

In the event of an unsuccessful call, the Lua stack is unchanged

Parameters

Returns

TeardownCallback

Type: FUNCTION Sets the previous instance Expects Lua stack:

   [-2] old instance
   [-1] context table

Both values are removed from the stack

Parameters

TeardownCallback

Type: FUNCTION Sets the previous instance Expects Lua stack:

   [-2] old instance
   [-1] context table

Both values are removed from the stack

Parameters

ToHash

Type: FUNCTION Check if the value at #index is a hash

Parameters

Returns

ToHash

Type: FUNCTION Check if the value at #index is a hash

Parameters

Returns

ToMatrix4

Type: FUNCTION Get the value at index as a dmVMath::Matrix4*

Parameters

Returns

ToMatrix4

Type: FUNCTION Get the value at index as a dmVMath::Matrix4*

Parameters

Returns

ToQuat

Type: FUNCTION Get the value at index as a dmVMath::Quat*

Parameters

Returns

ToQuat

Type: FUNCTION Get the value at index as a dmVMath::Quat*

Parameters

Returns

ToURL

Type: FUNCTION get the value at index as a dmMessage::URL*

Parameters

Returns

ToURL

Type: FUNCTION get the value at index as a dmMessage::URL*

Parameters

Returns

ToVector3

Type: FUNCTION Get the value at index as a dmVMath::Vector3*

Parameters

Returns

ToVector3

Type: FUNCTION Get the value at index as a dmVMath::Vector3*

Parameters

Returns

ToVector4

Type: FUNCTION Get the value at index as a dmVMath::Vector4*

Parameters

Returns

ToVector4

Type: FUNCTION Get the value at index as a dmVMath::Vector4*

Parameters

Returns

Unref

Type: FUNCTION Releases reference ref from the table at index t (see luaL_ref). The entry is removed from the table, so that the referred object can be collected. It also decreases the number of global references kept

Parameters

Unref

Type: FUNCTION Releases reference ref from the table at index t (see luaL_ref). The entry is removed from the table, so that the referred object can be collected. It also decreases the number of global references kept

Parameters

UnrefInInstance

Type: FUNCTION Deletes the instance local lua reference Expects SetInstance() to have been set with an value that has a meta table with META_GET_INSTANCE_CONTEXT_TABLE_REF method.

Parameters

UnrefInInstance

Type: FUNCTION Deletes the instance local lua reference Expects SetInstance() to have been set with an value that has a meta table with META_GET_INSTANCE_CONTEXT_TABLE_REF method.

Parameters

UrlToString

Type: FUNCTION Converts a URL into a readable string. Useful for e.g. error messages

Parameters

Returns

UrlToString

Type: FUNCTION Converts a URL into a readable string. Useful for e.g. error messages

Parameters

Returns