Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


MajinMLF

Member Since 21 Nov 2011
Offline Last Active Jul 15 2012 06:21 AM

Posts I've Made

In Topic: Delayed execution of behavior functions in Lua/C++

11 June 2012 - 02:10 PM

This is one heck of a question. I am not familliar with luabind, so I will go over the general ideas, and maybe hit some specifics.


Aye, and thanks alot for taking the time to lay out all that - i checked this post yesterday regularly but there wasnt a reply and in the meantime i got the idea to the solution myself which is basically similar to your pseudo code - however there is a gist - first the basic schedule function was pretty easy - its just a callback which - as you said i check the time in c++ - here it is for completion


void NPC::AddSE(const luabind::objectluabind::object &fn,int millisec) {
if(luabind::type(fn) == LUA_TFUNCTION)
	{
  ScheduledEvent sEvent;
  sEvent.eventFunction = fn;
  sEvent.time = GetMilliCount() + millisec;
  printf("AddScheduledEvent\n");
  m_scheduledEventFunctions.push_back(sEvent);
}
}
void NPC::CheckForScheduledEvents() {
std::vector<ScheduledEvent>::iterator sEvent = m_scheduledEventFunctions.begin();

while (sEvent != m_scheduledEventFunctions.end())
{
  if(sEvent->time < GetMilliCount())
  {
   if(sEvent->eventFunction.is_valid()) {
	luabind::call_function<void>(sEvent->eventFunction);  
   }
   // erase returns the new iterator
   sEvent = m_scheduledEventFunctions.erase(sEvent);
  }
  else
  {
   ++sEvent;
  }
}  
}
Now the hard part was having scheduled movie like events with delays in between for scheduled events e.g.

NPC:Say("Hello")
WaitTime(1000)
NPC:Say("Continue after 1sec")

That without letting the mainloop sleep! I got around this with the unique function of lua = coroutines - basically if WaitTime calls yield(x) the x will be returned to my lua_resume function - and in my code i will schedule the next call to resume after XX amount of time.

To answer your second question: The functions are in the same scope as long as they are loaded (luaL_dofile) with the same lua_State. So you can call those functions any time you want. However, I do suggest that you create tables named after each NPC behavior to hold those functions to avoid name conflicts.


Yes - thats what i want todo - is there a scope for a lua_state ? i dont like to name my functions unqiuely in each lua script - e.g. there should be no collision - how can i export one lua script to a specific scope only valid for one NPC ? Lets say all NPCs have an ID instead of doing

luabind::globals(myLuaState)["npc"] = npc;

i somehow have to put this NPC only in the scope of the loaded script - which brings the next problem if i use Lual_doFile or similar luaL_dostring(myLuaState, "code...") how put this in a scope ? I dont want that the scripts need to make namespaces or something similar - this should be safely done in c++ code. To reach the script of another NPC i would just expose an object to lua e.g. npc->GetScript()->CallFunction(luabind::object)... (I gather best practise is to have one lua_state for all NPCs and not multiple)

Any idea?

And thanks again =)

In Topic: Help me with ASM

11 June 2012 - 01:51 PM

This is a ida pro dump (CODE XREF: gives it away) - so why not use hexrays to give you pseudo c

PARTNERS