My problem is this. I am making a script engine using lua 5.1.4 for a game engine. At some point I want my script to be able to create an event for example a InitializeEvent (there are other events) which will be done through calling this method:

int ParticleSubSystem::GetIniEvent(lua_State* L) {
  IEvent<InitializeEventArg>& e = en->InitializeEvent();
  lua_pushlightuserdata(L, &e);
  return 1;
}

Next I want the script to be able to create a listener on for example a InitializeEvent such as a ParticleSystem (there are other listeners). This is the code for this:

int ParticleSubSystem::GetParticleSystem(lua_State* L) {
  OpenEngine::ParticleSystem::ParticleSystem* ps = new OpenEngine::ParticleSystem::ParticleSystem();
  lua_pushlightuserdata(L, ps);
  return 1;
}

Now I would like to attach the listener to the event but since all type information is lost when parsing stuff from and to lua I have the following problem:

int ParticleSubSystem::Attach(lua_State* L) {
  IEvent<????????>* event = (IEvent<?????????>*) lua_touserdata(L, -1);
 IListener<???????>* lis = (IListener<????????>*) lua_touserdata(L, -2);
event->Attach(lis);
  return 0;
}

Is there any way to pass a type argument around so you can do this:

int ParticleSubSystem::Attach(lua_State* L) {
Type t = ......; //???????
  IEvent<t>* event = (IEvent<t>*) lua_touserdata(L, -1);
 IListener<t>* lis = (IListener<t>*) lua_touserdata(L, -2);
event->Attach(lis);
  return 0;
}

I have tried using strings and macros but can't crack it. Any ideas or confirmation that it is impossible would be appreciated.

Recommended Answers

All 3 Replies

Your question is a bit unclear to me; but why do you need the type information? The best solution would be to grab the type prior to executing your script as lua uses dynamic typing anyways so it doesn't really matter if your arguments go in as a string, int, ect.

Your question is a bit unclear to me; but why do you need the type information? The best solution would be to grab the type prior to executing your script as lua uses dynamic typing anyways so it doesn't really matter if your arguments go in as a string, int, ect.

Well I want my scripts to be able to create arbitrary listeners and arbitrary events so the cpp code can't know what listener and event the scripts will call Attach with. It only knows that it is some kind of IListener and some kind of IEvent but not with what typename. However I can't attach a IListener and IEvent without knowing the typename. Therefore I need some way of getting a typename t from lua to cpp in the call:

int ParticleSubSystem::Attach(lua_State* L) {
  IEvent<t>* event = (IEvent<t>*) lua_touserdata(L, -1);
 IListener<t>* lis = (IListener<t>*) lua_touserdata(L, -2);
event->Attach(lis);
  return 0;
}

And since lua can't call templated methods I need some other way. That is why I tried converting a string to a type since I can pass strings from lua to c++.

Google C++ RTTI to see if that's what you are thinking about or to give you some possible leads. Otherwise you might be able to embed class type information in a state variable of the base class so the type can be determined when using a base class pointer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.