 |
Icy North Technologies User Forums
|
| View previous topic :: View next topic |
| Author |
Message |
chaos
Joined: 03 Dec 2009 Posts: 2
|
Posted: Thu Dec 03, 2009 5:16 pm Post subject: [C/C++] use some script without reloading again and again |
|
|
Hi, sorry to my bad English !
I'm working on a shmup engine, i use XML to script everything and now i want implement LUA to more flexibility. I tried some example but now i'm lost.
To increase performance i want load script at the beginning from my program et launch script later. I understand how to load and run a script but no how to load everything and launch it when i want.
I don't know if it's clear :p, for example i'm creating LUA state et loading sub library :
| Code: |
lua_State * state;
state = lua_open();
luaopen_string(state);
luaopen_math(state); |
now i want load my scripts with something like this
| Code: |
luaL_loadstring(state, script1);
luaL_loadstring(state, script2);
luaL_loadstring(state, script3);
|
an later use the bytecode generated when i need it
| Code: |
If something
Call Script 1
Else
Call Script 2
Else
...
End
|
any idea ?
Last edited by chaos on Fri Dec 04, 2009 7:08 am; edited 1 time in total |
|
| Back to top |
|
 |
lhf
Joined: 20 Feb 2006 Posts: 175 Location: Rio de Janeiro, Brazil
|
Posted: Fri Dec 04, 2009 4:23 am Post subject: |
|
|
luaL_loadstring returns a function corresponding to the body of the script.
luaL_loadstring does not run the script.
So, simply save the function left on the stack by luaL_loadstring and run it as many times as you need. |
|
| Back to top |
|
 |
chaos
Joined: 03 Dec 2009 Posts: 2
|
Posted: Fri Dec 04, 2009 7:22 am Post subject: |
|
|
i know this but i'm lost with how to use the stack.
Example, i use this class to generate a trunk (my tools generate it, while the engine load an use it)
| Code: |
class Script
{
public:
Script(){size=0; data=NULL;}
static int Append(lua_State* L, const void* p, size_t size, void* u)
{
Script* script = (Script*)u;
if(size == 0)
script->data = malloc(size);
else
script->data = realloc(script->data, script->size + size);
memcpy((void*)((char*)script->data + script->size), p, size);
script->size += size;
return 0;
}
static Script* Generate(string script)
{
lua_State * state;
state = lua_open();
luaopen_string(state);
luaopen_math(state);
if(luaL_loadstring(state, script.c_str()) != 0)
return NULL;
Proto* f = clvalue(state->top+(-1))->l.p;
lua_lock(state);
Script* trunk = new Script();
luaU_dump(state,f,Script::Append,trunk, 0);
lua_unlock(state);
lua_close(state);
if(trunk->GetSize()==0)
{
delete trunk;
return NULL;
}
return trunk;
}
void* GetData(){return data;}
size_t GetSize(){return size;}
private:
void* data;
size_t size;
};
|
i use it like this :
| Code: |
Script* test1 = Script::Generate("test = test + math.cos(1)");
Script* test2 = Script::Generate("test = test + math.cos(2)");
.
.
.
.
.
Script* testn = Script::Generate("test = test + math.cos(n)");
|
i can load a script with
| Code: |
luaL_loadbuffer (state, (char*)test1->GetData(), test1->GetSize(), NULL);
luaL_loadbuffer (state, (char*)test2->GetData(), test2->GetSize(), NULL);
.
.
.
.
luaL_loadbuffer (state, (char*)testn ->GetData(), testn ->GetSize(), NULL);
|
But now i don't know how to play with the stack |
|
| Back to top |
|
 |
lhf
Joined: 20 Feb 2006 Posts: 175 Location: Rio de Janeiro, Brazil
|
Posted: Fri Dec 04, 2009 7:32 am Post subject: |
|
|
| Use the registry or any other table to cache the script functions. So when you want to run a script, first test whether there's a function store at the key given by the script name. If there isn't, luaL_loadstring it and store the result in the cache. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|