🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Lua Environments

Started by
4 comments, last by Dwiel 20 years, 2 months ago
Hey guys, I did a search, and couldn''t find on google or here how to create multipule environments and switch between them. I have seen people say that this is what they do, but I can''t find any examples... just links to the references. I have looked through the references and still can''t seem to get it to work. If someone could post some simple code that would create 2 new environments, and call a function from C switching between the two that would be really cool. I don''t think the code for that would be that long... I have been able to access global variables by doing:

float lua_getglobalvar(lua_State* L, char* s)
{
	lua_pushstring(L, s);
	lua_gettable(L, LUA_GLOBALSINDEX);
	float f = lua_tonumber(L, 2);
	lua_pop(L, 1);
	return f;
}

void lua_setglobalvar(lua_State* L, char* s, float value)
{
	lua_pushstring(L, s);
	lua_pushnumber(L, value);
	lua_settable(L, LUA_GLOBALSINDEX);
}

void lua_rmglobalvar(lua_State* L, char* s)
{
	lua_pushstring(L, s);
	lua_pushnil(L);
	lua_settable(L, LUA_GLOBALSINDEX);
}
I just can not figure out how to change the gobal variables on other environments and switch between them. Thanks for the help! Dwiel My home page!!!
Find out about my diy LCD projector and programming projects!
Advertisement
Just create multiple threads, and use lua_replace() to set a new environment for each one. Read the manual for more information.

"Sneftel is correct, if rather vulgar." --Flarelocke
Must I create different threads for each environment? That seems like a lot of overhead When I can use the functions void lua_getfenv (lua_State *L, int index); and int lua_setfenv (lua_State *L, int index);

I am reading the manual @ http://www.lua.org/manual/5.0/manual.html. Is this the one that you are reffering to? I have done a couple of searches through that page, and can''t seem to find anything but this short bit. I have been trying to get those functions to work, but can''t seem to.

Thanks for the quick response!

Dwiel


My home page!!!
Find out about my diy LCD projector and programming projects!
Alright, under what conditions do you want to switch global environments? Is it per thread, per function, what?

"Sneftel is correct, if rather vulgar." --Flarelocke
I am trying to have multipule environments which functions can execute in. Some functions may need to be able to switch environments.

I think I have an idea of how I can do this... I took me a while to get the environments figured out in lua but let me know if this is how I should do it:

make a new table and give it a name such as envs. Push any environments that will be used onto that table. When I find out what context a function might need to run in, I can check it and see if already has a custom environment. If ti does, keep track of that and know that I might need to switch between a list of environments. If the function has the default environment, change it to the one we just found out it might to run under. This can be done by getting the value from the envs table and then calling a setfenv on the function and environment.

Is this the quickest way to setup up what I want?

do you understand what I want?

Thak you for being cooperative!

Dwiel

My home page!!!
Find out about my diy LCD projector and programming projects!
quote: Original post by Tazzel3D
Some functions may need to be able to switch environments.

...why?


"Sneftel is correct, if rather vulgar." --Flarelocke

This topic is closed to new replies.

Advertisement