lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


i need beginners help with passing variables (and later C++ functions)
to my lua scripts and back.

I read all the docs and I understand how to use the stack from C++, as
there is a pretty big section describing it. But what I am really
missing right now is how to do the same thing using lua, namely pushing
onto the stack and getting variables from the stack.

My problem is the following. I got a game engine, and this game has a
class with the following structure:

Class DoSomethingWithObject
{
	Object *theobject
	String NameOfWhatToDoWithObject

Public:
	Void CallLuaScriptToDoIt ();

	string GetDataFromObject (Object* object, WhichData)          //
Normally I wouldnt send the Object* in
	bool SetDataOnObject (Object* object, WhichData, ToWhichData) //
as it can read it from the member var
}

void DoSomethingWithObject::CallLuaScriptToDoIt ()
{
	StartLua()
	ExecuteLuaFile (NameOfWhatToDoWithObject+.lua)
	CloseLua()
}

Fact is, I need to call the two functions GetData.. and SetData.. from
my Lua script, as those are the functions that actually change the
object. Those functions need to know which Object, which is why they
need the Object pointer. The object class is hell big and way
complicated, so I cant pass over the object variables to the Lua script.
I know how to make the Lua script send the Object*, WhichData and
ToWhichData to a LuaGlue function which then calls the required SetData
function, however that would mean I need to first pass the object
pointer into Lua and then from Lua back to the C++ code, which I think
is a pretty bad idea from a design point of view, as the C++ code should
KNOW the Object pointer, and that way scripters could screw up the whole
game, making it actually crash using script code.

Now my questions:

1. After I push variables onto the stack in my C++ code, how do I read
them in the main execution code in the script? I understand if I first
call a script that declares functions, and then call the function from
C++ putting the required variables onto the stack, this will work
without problem. But how do I do it if I DON'T call a function in the
script, but just have a simple script that needs to access the
variables? I read something about globals but I didn't find out how to
actually code it using lua... I need a Lua function that basicly does:
value = tostring (-1), like it works on the C++ side.

2. How can I have the Lua script call Member functions of a C++ class
that way that they can still access the members variables of their
instance afterwards? I always had to make the glue functions global,
which is a pretty bad idea on a large OOP project like this, also that
way they cant access the above Object* thats inside the class anymore,
which in turn means I got to make the pointer jump to Lua and back.
It would still help me if I could at least export member functions that
then cannot access the variables anymore, that way I would at least
still have a clean interface afterwards thats hidden inside a class.

Alright, thats all for now! Different solutions to tackle this problem
are highly appreciated... :)
Thx in advance.

SkyFlash