how to link a c++ object to a local variable in Lua

Posted by MahanGM on Game Development See other posts from Game Development or by MahanGM
Published on 2013-06-28T18:01:07Z Indexed on 2013/06/28 22:29 UTC
Read the original article Hit count: 328

Filed under:
|
|

I'm completing my scripting interface with Lua, but recently I've stuck at some point. I have several functions for my Entitiy events like Update(). I have a function called create_entitiy() which instantiate a new entity from a given entity index:

function Update()
  local bullet = create_entity(0, 0, "obj_bullet")
end

create_entity returns a table which is the properties of the created entity. Now how can I make a connection between bullet variable and my newly created object?

Right now for previously added objects to the scene, I simply set a global table for each of them and then after every call to Update(), I go through registered names to find object tables and perform new changes. Like the one below:

function Update()
  if keyboard_key_press(vk_right) then obj_player.x += 3
end

I can get obj_player table because I know its name from C++, plus I can get it as a global table and simply reach for the first instance named obj_player.

Is there any solution for me to make bullet variable act like this?

I was thinking to get all local variables in Update() function and check for every one to see if is it table and it has an unique field attached to it like id, this way I can determine that this is an object table and do the rest of the process.

By the way, is this interface going to work easier with luaBind if I implement it?

Bottom line:

How can I make a local variable in Lua that receives a table from create_entity function and track that local variable to capture it from C++.

e. g.

function Update()
  local bullet = create_entity(0, 0, "obj_bullet")
  bullet.x = 10 <== Commit a change in table
end

Now I want to get variable bullet from C++. And it's not just this variable, there might be a ton of these local variables with different names.

© Game Development or respective owner

Related posts about c++

Related posts about scripting