How can I exchange variables in a C ++ class with Lua?

I am new to Lua, I was working on an attempt to implement Lua scripts for logic in the Game Engine, which I am compiling. I have not had any problems so far when I get Lua and start the engine, and I can call Lua functions from C and C functions from Lua.

The way the engine works now, each Object class contains a set of variables that the engine can quickly iterate over for drawing or processing for physics. While game objects all need to access and manipulate these variables so that the Game Engine itself can see any changes, they can freely create their own variables, Lua is extremely flexible in this, so I see no problems.

In any case, the Game Engine game is currently sitting on ground C, and I really want them to stay there for performance reasons. Therefore, in an ideal world, when you create a new game object, I will need to provide Lua read / write access to this standard set of variables as part of the base class of the Lua object, and then its game logic can proceed to run wild with.

Until now, I have saved two separate object tables - Lua creates a new game object, which adds itself to the numeric indexed global object table, and then proceeds to calling the C ++ function, which creates a new GameObject and registers the Lua (int) index with the class. So far so good, C ++ functions can now see the Lua object and easily perform operations or call functions in the Lua land using dostling.

Now I need to make C ++ variables, part of the GameObject class, and expose them in Lua, and this is where Google does not work. I came across a very nice method here that details the process using tags, but I read that this method is deprecated in favor of metadata.

What is the ideal way to achieve this? Is it worth the hassle of passing class definitions using libBind or some equivalent method, or is there a simple way that I can simply register each variable (once, at the time of occurrence) using the global lua object? What is the “best” way to do this like Lua 5.1.4?

+4
source share
1 answer

One approach is to use

  • lightuserdata pointing to a C ++ variable
  • C function to access a C ++ variable using lightuserdata li>
  • save lightuserdata as an upvalue of a C function, so one function is enough for all variables
  • use the number of function arguments to choose between getting and setting a variable

For instance:

int game_state_var_accessor (lua_State *L) { int *p = lua_topointer(L, lua_upvalueindex(1)); if (lua_gettop(L) == 0) { // stack empty, so get lua_pushinteger(L, *p); return 1; } else { // arg provided, so set *p = lua_tointeger(L,1); return 0; } } 

When you create a new game state, you can create accessors for each variable using:

 lua_pushlightuserdata(L, (int *)p); // where p points to your variable lua_pushcclosure(L, game_state_var_accessor, 1); 

The accessor is now on the stack and can be bound to the global name or to the method name in the Lua class. If your Lua class table is on the stack with index t , this will be:

 lua_setfield(L, t, "name_of_accessor"); 
+4
source

Source: https://habr.com/ru/post/1307479/


All Articles