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

Defining global / local variable in lua

Started by iliak Feb 28, 2007 at 3:25 AM 3 replies 2.8k views
Original Post
iliak
iliak
Hello Can someone point me to an url / source / ... where I can define a variable in lua from c++ ? Here's what I want in pseudo code : From C++: std::string variable = "arg1"; int value = 10; SetLuaVariable(variable.c_str(), value); CallLUAFunction("func"); From Lua: function func() print ("arg1 = "..arg1) end Lua doc is not realy clear. Thx
- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]
Kylotan
Kylotan
Find 'lua_setglobal' on that page and give that a go.
iliak
iliak
Thanks Kylotan

Here's what I do now :

cLevel *level = new cLevel();...lua_pushinteger(Lua, elapsed);lua_setglobal(Lua, "elapsed");lua_pushlightuserdata(Lua, Level);lua_setglobal(Lua, "level");SendEvent("UPDATE");


Everything works well excepted that "level" in lua fire this error :

attempt to index global 'level' (a userdata value)


How can I set it correctly in lua ?
- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]
Kylotan
Kylotan
Lua doesn't know what to do with userdata. After all, it could represent absolutely anything. You have to write functions to handle userdata in the way that you need.
iliak
iliak
For the curious ones, here's how a solved my problems :

Passing variables from C++ to lua with luabind :
	template<class T>	void SetScriptGlobal(const char *name, T value)	{		globals(Lua)[name] = value;	}

- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]

Topic Locked

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

Sign in to reply to this topic.