Best Practice Scoping Different Scripts+Expose Object/ luabind:resume with optional return value
Harakiri <[email protected]> Mon, 11 Jun 2012 17:09:15 -0700 (PDT)
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <[email protected]> |
1. Resuming a coroutine which has yields forces me to always have a final "return 0" in my function to not produce any error e.g. consider
luabind::object m_CoroutineFunc = ...
m_CoroutineFunc.push(m_CoroutineFunc.interpreter());
lua_State * pThread = lua_tothread(m_CoroutineFunc.interpreter(), -1);
lua_pop(m_CoroutineFunc.interpreter(), 1);
int yieldTime = luabind::resume<int>(pThread);
in my coroutine i use coroutine.yield(seconds*1000) - so this value will be returned and i know when i should call luabind::resume in my c++ logic again.
However the issue arises that this forces me to always use return 0 at the end of my coroutines - otherwise "luabind::resume<int>" will throw an error which is not pretty - is there another way?
Additionally - i set this coroutine to c++ via a callback e.g. in lua i have myobj->addCoroutine(coroutine.create(myLuaFunc)) - the c++ side checks if luabind::object is (luabind::type(obj) == LUA_TTHREAD) - now for simplicity i dont want to use "coroutine.create" in lua but simply give the myLuaFunc to my callback of type LUA_TFUNCTION - how can i "convert" this function to a coroutine? e.g. LUA_TFUNCTION to LUA_TTHREAD in c++ as opposed by doing this in lua?
2. I want to create an AI engine with different scripts for each actor - lets call them actor1.lua actor2.lua - im binding my actor class with luabind - and want to expose my actor1 obj to actor1.lua and actor2 obj to actor2.lua - while only using one lua_state for everything (i think this is the best if you have many actors..)
Currently i use luabind::globals(myLuaState)["actor"] = actor1; - for the sake of simplicity i want that every actorX.lua script simply refers to his own object as "actor" and not have a different object name for every actor - e.g. in actor1.lua i might use actor->Print("Im actor 1") and in actor2.lua i use actor->Print("Im actor 2") - how do i scope this correctly ? That once i call a function defined in lua from c++ in script actor1.lua with actor1 obj and the other time with actor2 obj and actor2.lua.
Moreover i read that by default everything in lua is global - so how do i make sure that there is no function name collision when actor1.lua defines "function OnEventX" and actor2.lua does the same ? I also like to compile these script once at startup only...
Thanks!
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/