Skip to main content
GameDev.net gamedev.net
🔒 Locked

Calling C Functions from Lua

Started by seeressk Oct 25, 2005 at 3:08 PM 0 replies 900+ views
Original Post
seeressk
seeressk
I am currently working on a project to drive a user interface system through Lua and XML. Currently, to do something like making a window visible, I do the following in lua: WindowSetShowing( 'LoginWindow', true ) Where WindowSetShowing( lua_state *pState ) is an exposed C function that parses the 'window name' and the 'showing state' parameters. What I would like to do instead is this: LoginWindow.SetShowing( true) Where Instead of providing 'LoginWindow' as a parameter, SetShowing() is a psudo 'member function' of 'LoginWindow'. In C, I'd like to examine the name of the table from which the function was called to determine the window name. The problem with this is that when entering an exposed C function, the top of the lua stack is equal to the number of parameters being sent to the function. Not the current stack height. So, there is no obvious way for me to get at the lua_stack. Does this mean the lua stack is only used for interaction with c, and not internal lua processing? Any suggestions?
WMCoolmon
WMCoolmon
http://www.lua.org/pil/28.1.html

Read on through 28.3, and your question should be answered.

Keep in mind that object-oriented access is done with ":", eg LoginWindow:SetShowing(true). A "." merely indicates that a function is part of that library/object, and will not pass the userdata (if it has any associated with it) along with the function.

The basic steps you need to do are...
- set the metatable for all function 'methods' when you register them
- set the metatable for all userdata objects instantiated
- when a member function is called, use "isuserdata" and "touserdata" to grab the userdata off the stack at index 1 (I had trouble with the example in the online book)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.