Skip to main content
gamedev.net
🔒 Locked

Lua: Modify Lua Variable

Started by Rob Loach 4 replies 2.3k views
Original Post
Rob Loach
Rob Loach
I've figured out how to get a value of a Lua variable from C, but how do you modify it? Right now I'm just running a line of script that just changes the variable by name. It works, but there's got to be a "better" way.
Rob Loach [Website] [Projects] [
nuc
nuc
I believe there is not direct way to do it other than querying the correct variable from the lua_State globals table. Every variable and function that is defined inside the lua state can be found by digging it from the table at the pseudo index LUA_GLOBALSINDEX.

After popping the correct table to the stack you should be able to set values inside it.

The following is from the Lua 5.0 Reference Manual chapter 3.12:

"To access and change the value of global variables, you can use regular table operations over an environment table. For instance, to access the value of a global variable, do

lua_pushstring(L, varname);
lua_gettable(L, LUA_GLOBALSINDEX);"

After that the value of the variable in the globals table should be on the top of the stack. You could try changing the value and pushing it back to the globals table.

If you try it, let me know if it worked. I've never done that :P
Toes
Toes
Personally I would recommend using CPB instead (http://ww.thomasandamy.com/projects/CPB/ ) since it supports that functionality directly. but then I’m biased.

If you want to do it directly assign a value a number in Lua and then update the backend C variable, it is a complete pain in the ass. Getting it out is easy, having it work the other way is not.

The problem is that you can’t simply assign it, because that would overwrite the variable.

For example

YourTable.m_Value1 = (Some Value you exposed from C)
YourTable.m_Value1 = 50;

Now you’ve over-written the data in m_Value1 with your variable, but that’s not what you want, you want to update your C variable instead.

I have not found a way to over ride what happens when some thing is assigned to a value. That operation as far as I know is controlled by LUA. That’s okay, because in order to access your C value you need to first store the memory address of the C value anyway, not just it’s value

What you need to do is in to leave YourTable empty, create a meta-table for YourTable that has a function entry both __index, __newindex and the address of the variables in it, indexed all by name. On __newindex which is the setting of the value, you need to look up the name in to your C function you want to change and modify the value at the internal address to match what you’ve assigned the variable. Also keep in mind that your C variable can be a variety of different types where as your LUA ones are all floats.

If you really want to know how to make this change, I would look at how it’s setup in CPB itself and just copy the code out. Keep in mind it is a whole lot of code to support this type of functionality.

Thomas
corysama
corysama
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
}

const char luaScript[] = "luaFoo = 1.0";

int main(int argc, char *argv[])
{
lua_State *luaState;
lua_Number foo;

luaState = lua_open();

/* Run the script */
luaL_loadbuffer(luaState, luaScript, sizeof(luaScript)-1, "luaScript");
lua_call(luaState, 0, 0);

/* Get the value */
lua_pushstring(luaState, "luaFoo");
lua_gettable(luaState, LUA_GLOBALSINDEX);
foo = lua_tonumber(luaState, -1);
lua_pop(luaState, 1);

/* Modify the C variable */
foo = foo + 1.0;

/* Set the Lua variable */
lua_pushstring(luaState, "luaFoo");
lua_pushnumber(luaState, foo);
lua_settable(luaState, LUA_GLOBALSINDEX);

/* Verify the change */
lua_pushstring(luaState, "luaFoo");
lua_gettable(luaState, LUA_GLOBALSINDEX);
foo = lua_tonumber(luaState, -1);

lua_close(luaState);
return 0;
}
skyfire360
skyfire360
This site helped me get Lua up and running in about 20 mins. Now I can script entire demo scenes to be rendered in my engine without recompiling any code.

P.S. PM me if you want my lua code :)
I do real things with imaginary numbers
Rob Loach
Rob Loach
Excellent, got it working. Thanks a lot... I also had a huge bug because I wasn't breaking the cases in the switch statement to determine what type of Lua variable was. Another set of eyes would've helped a bunch [smile].
Rob Loach [Website] [Projects] [

Topic Locked

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

Sign in to reply to this topic.