Skip to main content
gamedev.net
🔒 Locked

Lua: Object data and Thread

Started by AverageJoeSSU Mar 8, 2009 at 5:33 PM 5 replies 1.3k views
Original Post
AverageJoeSSU
AverageJoeSSU
I can do this in one of two ways... what do you guys think? So... i have a game object... which Lua or My C++ engine may manipulate things from position to "Attack and Move Orders" (Lua doing high level stuff obviously and C++ doing low level blah blah). I have one Lua Thread dedicated to AI. 1) The way I see it is that each Object will either pass itself to its AI script, and i have a wrapper that exposes all of the orders and everything via getters and setters. 2) Or I can have each Object create and manage its own table that is loaded before the script is run. On Completion I think I would have to read the table and set the stuff in my Object or something to that affect right? I personally like the idea 1). I'm using Luna and am in the process of exposing this stuff. Thanks.
------------------------------ redwoodpixel.com
ddn3
ddn3
Transferring data to and from Lua into C++ isn't free, each get and set within Lua will chip at your performance. If you don't do it often, both are equivalent methods, and the only difference is one of convince and ease of use for you.

There are advantages to keeping all properties of an object in Lua, in that it resides with the native high level logic ( also written in Lua ) so there won't be any penalty to fetch data from C++. I normally keep the properties all in Lua now and only use a select API to control the C++ side, judiciously keeping the calls into C++ to a minimum ( for mostly performance reasons ), even basic types like 3D vectors, color RGB values, etc.. reside in Lua now. The C++ have become more and more just a manager for rendering, file io, gfx and sfx rendering.

Good Luck!

-ddn
AverageJoeSSU
AverageJoeSSU
Wow that is very interesting.

I exposed my C++ Object Class already and am exposing my C++ 3D Vector class now for my Object AI....
i basically have ships that have their own AIScripts to call,
and when it is all said and done the script will add an animator to the ship that will take the ship to the given point.

I can definitely see where keeping my Vectors in Lua would help performance, i'm going to have to keep an eye on that. I'm going to have to have a way of connecting the render functions, collision info and other stuff that needs to stay in C++ to lua.

I am having a bit of a problem with my add method for my LuaVector Wrapper class for my c++ vector. Any help would be appriciated, I seemed to have confused myself with lua pushing and popping and userdata.. ugh....

LuaVector4::LuaVector4(lua_State *Lua){	  double x = lua_tonumber(Lua, -3);  /* get argument */      double y = lua_tonumber(Lua, -2);  /* get argument */	  double z = lua_tonumber(Lua, -1);  /* get argument */	 	  real_object = new glVector4(x,y,z, 1.0);	  lua_pushlightuserdata(Lua, (void*)this);	  	  cout << "Lua Vector Created "<< x <<" , "<<  y << " , " << z << endl;}int LuaVector4::add(lua_State *Lua){	 //cout << "Adding Vector" << endl;	//stackDump(Lua);	LuaVector4 * x = (LuaVector4 *)lua_touserdata(Lua, -1);	*(x->real_object) = *(real_object) + *(x->real_object);	lua_pushlightuserdata(Lua, (void*)x);	return 1;}


here is the lua script

o = LuaObject('luaobject') --this is the wrapped object* in C++.... v = LuaVector4(1.0 , 1.0 ,2.0)  --locally created Vector4 w-value = 1.0c = LuaVector4(2.0, 2.0, 2.0)z = LuaVector4( 2.0, 3.0 , 2.0 )v:add( c ) 


here is the output...

Lua Vector Created 1 , 1 , 2
Lua Vector Created 2 , 2 , 2
Lua Vector Created 2 , 3 , 2

and it crashes in the add function.... not sure what i should do.







------------------------------ redwoodpixel.com
ddn3
ddn3
I believe you have the wrong convention for popping elements from the stack. From all the examples I've seen it goes like this:

LuaVector4::LuaVector4(lua_State *Lua){	double x = lua_tonumber(Lua, 1);  /* get argument */	double y = lua_tonumber(Lua, 2);  /* get argument */	double z = lua_tonumber(Lua, 3);  /* get argument */		real_object = new glVector4(x,y,z, 1.0);		lua_pushlightuserdata(Lua, (void*)this);		cout << "Lua Vector Created "<< x <<" , "<<  y << " , " << z << endl;}


Other than that everything seems correct. Give this a try. BTW from the test I've done with LuaJit, doing basic arithmetic operations in Lua is faster than pushing into C and doing it there, due to the overhead. LuaJit optimizes the arithmetic operations into assembly executed inline within Lua which is faster than calling a C function to do it. This isn't true for expensive operations on matrices or the like ofcoruse, but for simple stuff like add/mul/sub/div vectors better to just do it in Lua, IMO.

Good Luck!

-ddn
AverageJoeSSU
AverageJoeSSU
cool thanks,

i still get a bad pointer when i do

LuaVector4 * x = (LuaVector4 *)lua_touserdata(Lua, -1);
I get an address of 0x000 in visual studios debugger after that line.... could the constructor not be working?


I think moving more of the engine to lua is going to have to happen. for now i'm going to leave it just to get it working.
------------------------------ redwoodpixel.com
ddn3
ddn3
You shouldn't be using -1 to pop the elements off the stack. It's positive numbers to access each parameter on stack using those functions, the lua_toXXXX already handles the negative offset from the stack for you. That's my understanding, I've not tried it with userdata, since i don't go that low level with Lua myself.

It should be.

LuaVector4 * x = (LuaVector4 *)lua_touserdata(Lua, 1);

If you were say to do it with raw push and pop then u would use -1 offsets.

Good Luck!

-ddn
AverageJoeSSU
AverageJoeSSU
ohhhh i see.... i didnt know lua took care of the offset! so cool haha...
------------------------------ redwoodpixel.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.