Copying lua states

Started by
10 comments, last by utopico 20 years, 10 months ago
I need to copy a part of the lua stack from one lua state to another. How do I do this. Long version: I have lua function (LF1) what can call a C function. Some of the params in the lua function (LF1) (can have arbitrary number of params) will be used for calling another lua functin (LF2) from the C function ( CF ). Under is an example, hope it i s understandardable. The problem comes when copying all the unkown params, how do I do this. Example: LF1: cFunctionCalledFromLua(StandardParam1, StandardParam2, UnkownParam1, UnknownParam2) LF2: function luaFunctionCalledFromC(paramThatOnceWasStandardParam2, paramThatOnceWasUnknownParam2,paramThatOnceWasUnknownParam2) end CF1: cFunctionCalledFromLue(lua_State){ pop standardparam1 pop standardparam1 lua_State newLua = init new state and parse file. push standardparam1 on newLUa push all the rest of unkown params on newLUa (problem) call LF2 ( luaFunctionCalledFromC) in newLua state with standardparam and all unkonwn params. }
Advertisement
with lua_gettop(LUA) you can get the number of variables currently on the stack so if your cfunc is beeing called with 4 params (your example) it will return 4.

if you also need the type of them a quick call to lua_is[nil|boolean|number|string|...] will do the trick
just read the manual

simple example (the cfunc):
cFunctionCalledFromLue(lua_State *LUA){  pop standardparam1  pop standardparam2  lua_State newLua = init new state and parse file.  push standardparam1 on newLUa  push standardparam2 on newLUa  for(int i=2i    push param(i) on newLUa  call LF2 ( luaFunctionCalledFromC) in newLua state with standardparam and all unkonwn params.} 


this should work bur by reading your code why do you want to create a new lua state, and what do you want to pass?


mfg Phreak
--
"Input... need input!" - Johnny Five, Short Circuit.
http://mitglied.lycos.de/lousyphreak/
Thanks. Got it working now.
I have C++ class that can be a wrapper for a lua script. This class has a common interface together with a lot of other C++ classes, only the constructors differ.
Since I want to be able to construct it with different input params and from another lua script it is very nice just to pass the params from the first lua script to the second.

Hope you understood this cause I gues this was not very well explained.

Jarda
if i understand you correctly you dont need multiple lua states, cos you can execute more than one script/action,... in a single state with the benefit that the stuff changed from the others is still active --> if you set variables they will stay active in the state until you clear them, if you define functions you can call them until you close the state,...

so having multiple of them is usually not necessary, you can do almost everything with just a single state (actually i cant think of anything right now that requires more than one...)



mfg Phreak
--
"Input... need input!" - Johnny Five, Short Circuit.
http://mitglied.lycos.de/lousyphreak/
The tric is that each lua state represents a C++ object. The benifit of this is that variables between C++ object.

Typically a lua script of ours is: //self i typically the original C++ object

fuction init(self)
-- do init stuff
end

function procMsg(self, msg)
-- process message
-- a stat variable used in the global space will be accessibel
-- here next time to so that it can use as state information
end


If there are other ways to do this in a nice way I welcome suggestionns.

Utopico
Each lua state does NOT represent one object. I like to think more of it as one lua state represents one "virtual machine". So every game object can exist in only one Lua state. You need to ensure that you maintain the Lua state object, but if you use a script manager class like I do, then that''s easy.
---------------------http://www.stodge.net
I know that each lua state does not represent one object, but I use the lua state as if to represent an object. Since there can be many scriptet object with the same interface (init, procMsg) these function will not mix, and the variables will not either. But if there is another way that is more practical I would realy like to here them.
I don''t understand what you need. If you create three Lua states, then you have three separate Lua virtual machines which can''t interact.

In my engine, I create one Lua state. I then create instances of all manager objects in my engine and export them to Lua. I create Lua bindings for these objects using ToLua.

		void hbScriptManager::Expose(	const HBchar* VarName,										void* Data,										HBchar* TagName)		{			hbLogManager::GetInstance()->LogPrintf("*** Exposing object %s of type %s",				VarName,				TagName);			lua_pushusertag(mStateMachine, Data, tolua_tag(mStateMachine, TagName));			lua_setglobal(mStateMachine, VarName);		} 


			// Expose the resource manager instance variable.			scriptManager->Expose(	"Audio",									hbAudioManager::GetInstance(),									"hbAudioManager"); 


In this example, I''m exporting the Audio manager object to Lua. I can then access the Audio manager in Lua using Audio:LoadSample() for example.

Is this the kind of thing you mean?
---------------------http://www.stodge.net
I don''t understand what you need. If you create three Lua states, then you have three separate Lua virtual machines which can''t interact.

In my engine, I create one Lua state. I then create instances of all manager objects in my engine and export them to Lua. I create Lua bindings for these objects using ToLua.

		void hbScriptManager::Expose(	const HBchar* VarName,										void* Data,										HBchar* TagName)		{			hbLogManager::GetInstance()->LogPrintf("*** Exposing object %s of type %s",				VarName,				TagName);			lua_pushusertag(mStateMachine, Data, tolua_tag(mStateMachine, TagName));			lua_setglobal(mStateMachine, VarName);		} 


			// Expose the audio manager instance variable.			scriptManager->Expose(	"Audio",									hbAudioManager::GetInstance(),									"hbAudioManager"); 


In this example, I''m exporting the Audio manager object to Lua. I can then access the Audio manager in Lua using Audio:LoadSample() for example.

Is this the kind of thing you mean?
---------------------http://www.stodge.net
I don''t understand what you need. If you create three Lua states, then you have three separate Lua virtual machines which can''t interact.

In my engine, I create one Lua state. I then create instances of all manager objects in my engine and export them to Lua. I create Lua bindings for these objects using ToLua.

		void hbScriptManager::Expose(	const HBchar* VarName,										void* Data,										HBchar* TagName)		{			hbLogManager::GetInstance()->LogPrintf("*** Exposing object %s of type %s",				VarName,				TagName);			lua_pushusertag(mStateMachine, Data, tolua_tag(mStateMachine, TagName));			lua_setglobal(mStateMachine, VarName);		} 


			// Expose the audio manager instance variable.			scriptManager->Expose(	"Audio",									hbAudioManager::GetInstance(),									"hbAudioManager"); 


In this example, I''m exporting the Audio manager object to Lua. I can then access the Audio manager in Lua using Audio:LoadSample() for example.

Is this the kind of thing you mean?
---------------------http://www.stodge.net

This topic is closed to new replies.

Advertisement