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

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.

• I'm not sure what you're asking. Locals in Lua reference tables, not copy them. If you modify the value of a table like position in C++ then that value is automatically visible Lua. You only need to change variables values in Lua if you're setting primitive value types (numbers, booleans, etc.) or you're changing references. – Sean Middleditch Jun 28 '13 at 18:11
• See last part I've added. – MahanGM Jun 28 '13 at 18:26
• You want to make it so that when you change the value of a local variable (not a value inside a table), C++ picks up on it? Or make it so that when you change a value inside a table, C++ gets a callback letting it know? Your example code indicates you're modifying a value in a table but your question update indicates you just want to change the value of a local variable - these are not the same things. – Sean Middleditch Jun 28 '13 at 18:31
• The one that is changing a table's field is my normal modifying of global registered tables from C++. They're my added objects to the scene and not run-time instantiated. The one with local variable receives a table returned from create_entity. I want to track that local variable and capture it from C++ in order to extract its contents which is a table and do my job with that data... I know how to pass and get tables but I don't know how to track local variables. They can be hundreds. – MahanGM Jun 28 '13 at 18:39
• I am not sure enough what you are asking to try to draw up an answer. But if I understand correctly, you would like to be able to "create" an object in Lua and then get a hold of them C++-side. Thinking from a bullet perspective, you may want to create a pool of bullet objects C++ side, and create an API for "getting" a bullet object within Lua. You can then set it as active or something and change whatever data you want to. Then the C++ side could act on this in whatever way you wanted to. But in the end, C++ would be managing the lifetime of the Bullet object. Lua would simply input data. – Dean Knight Jun 28 '13 at 19:10

local bullet = create_entity(0, 0, "obj_bullet")

what you really need to keep track of is not the local variable bullet, but rather the table returned by create_entity. There can be any number of local variables, fields, etc. that reference this same entity, but (as far as I understand your question) you don't need to keep track of all of them, only the single table they refer to.
So all you need to do is, in the implementation of create_entity, store a reference to the created table someplace where you can retrieve it later. For instance, you might have a global table of all entities, keyed by the entity's ID, and reference the entity table from there. You could then find this table from C++ (since its name is fixed) and walk over its entries whenever you needed to check for updates.
• Thanks. The thing is I didn't pay attention to locals in Lua reference tables, not copy them. – MahanGM Jun 29 '13 at 0:54