Sign in to follow this  
swordfish

Accessing Lua identifier names from C/C++

Recommended Posts

swordfish    276
Does anybody know if it's possible to access the identifier name strings of Lua5 variables from C/C++? I suspect it involves some sort of global table, but I can't figure it out and there seems to be no information on the topic. To give you an idea of what I mean, here is a sample from one of my current scripts:
--lua5
MyButton = LuaGUIButton("MyButton", "Click Here", 10, 10, 75, 35)


I am required to pass a string, "MyButton", as a function parameter (from Lua) in order for the invoked C++ 'LuaGUIButton-interface' class' constructor (I'm using Lunar to bind my C++ classes to Lua) to know what to name the requested 'GUIButton widget' which the interface class (LuaGUIButton) then passes to the singleton 'GUIManager' which inserts the widget into it's scene-graph system as a spatial-geometry node. Although it's not absolutely necessary, this widget/node needs to be named the same as the variable which the user (Lua script-programmer) will be using to access it. The above sample scrupt works fine, but as you can see, it's not exactly what I'd call "professional". Basically what I need is for the C++ LuaGUIButton-interface class' constructor to automatically find the identifier name of the "yet-to-be-assigned" Lua variable - this way the user (Lua script-writer) can utilize the following usage:
--lua5
MyButton = LuaGUIButton("Click Here", 10, 10, 75, 35)


...and the GUIManager will have created a widget/node named "MyButton" and with properties provided as function parameters, without the user explicitly providing the name as a function parameter. I found this script which I think illustrates the solution I need, but I can't get it to run, and even if it did run, I don't fully understand how it works - perhaps it will strike a chord in someone's head:
mt = {
       __newindex=function (t,k,v)
                       if type(v)=='table' then
                        rawset(t,k,v)
                        rawset(t[k], "n", tostring(k)) -- instance name to 'n'
                        return
                     end
                     rawset(t,k,v)
                  end
}
setmetatable(getfenv(), mt)
grape = function()
   local t = {n=''}
   function t:test(x)
      print( n..x ) -- ERROR here, Lua says 'n' is a nil value and cant be concatenated
   end
   return t
end
newgrape = grape(", Awesome!")
newgrape:test()

--This script SHOULD print "newgrape, Awesome!"



I've done some thinking and perhaps keeping it the way it currently is would provide a more robust usage, and perhaps even solve a few future problems which I can already see forming, but I'd still like to know how to do this. Thanks in advance for any help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this