Topic: Pass data between levels

Is there a way to pass data between levels in lua?
A sort of global variables for lua?

Re: Pass data between levels

There is no ready made feature right now, we'll try to do something in the future.

Right now you can :
- write and read a file in lua
- use a custom game plugin to handle it (best)

Re: Pass data between levels

Hi,
Yes, a way to pass values from Lua to specific levels would be nice smile
I realized I was in need of something similar today and hacked together two api functions so I could "preserve" Lua numbers from one level to another.

To use a game plugin is much better, but I'm not there yet.
I just drop the code here anyway in case it's useful for someone else.

file: ...Maratis/Common/MScript/MScript.cpp

#define NUMSLOTS 64
static double g_persistentSlots[NUMSLOTS];

//...

static int getPersistentSlot(lua_State * L)
{
    if(! isFunctionOk(L, "getPersistentSlot", 1))
        return 0;

    int i = luaL_checkinteger(L, -1);
    if(i<0 || i>NUMSLOTS)
    {
        printf("ERROR get persistent slot: %d, invalid index!\n", i);
        return 0;
    }

    lua_pushnumber(L, g_persistentSlots[i-1]);
    return 1;
}

static int setPersistentSlot(lua_State * L)
{
    if(! isFunctionOk(L, "setPersistentSlot", 2))
        return 0;

    int i = luaL_checkinteger(L, -2);
    if(i<1 || i>NUMSLOTS)
    {
        printf("ERROR set persistent slot: %d, invalid index!\n", i);
        return 0;
    }

    g_persistentSlots[i-1] = luaL_checknumber(L, -1);
    return 0;
}

// ...
// Then register these functions in MScript::init with the other API functions.
// ...