Jump to content
  • Advertisement
Sign in to follow this  

Lua and non-static class member functions

This topic is 2902 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

To do my AI I wanted to use Lua with registered functions to get information about the AI controlled entity and the world while also letting me modify it's position/rotation but I've hit a snag. As far as I know you can only register static functions. This leaves a big problem:

Note this code has been gutted because I don't have the source with me and I'm writing it from memory and don't want to type the whole thing out.

class CAIComponent: public CComponent
void update();
void init();
luaState* L;
CSpatial* spatial; //pointer to object containing the particular entity's rotation and position;
int id; //Entity id this component belongs to

//Functions for ai script
static int ai_position(luaState* L);

basically in my init() function I open a luaState and register the function ai_position. ai_Position needs to access the objects spatial reference but it can't do that because it's static. Is there a way around this? Am I going at this design wrong?

Share this post

Link to post
Share on other sites
You need to obtain the active component from the lua_State. For example, you might have passed a lightuserdata value into the script, or an upvalue attached to the function.

Share this post

Link to post
Share on other sites
Do you mean pushing a pointer somehow to the lua_State in the init() function?

Edit -
Would it be better to do it the opposite way like described here?

That way I just call a lua function from c++ and feed it the data instead?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!