Jump to content
  • Advertisement
Sign in to follow this  

Accessing Lua identifier names from C/C++

This topic is 4862 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

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:
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:
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], "n", tostring(k)) -- instance name to 'n'
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
   return t
newgrape = grape(", Awesome!")

--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
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!