0

So I am still working on a flexible scripting system for this school project and have come to a road block. The problem is this: I have a script state manager interface that any scripting state will implement (currently implementing lua, but want to be able to add python later). It has pure virtual functions for things such as DoFile, DoString, RegisterObject and RegisterFunction. So the LuaStateManager implements this interface using luaplus classes and encapsulates the LuaState object as it should.

So when an object registers itself with lua and throws all its methods into a metatable I have to do something like this:

metaTable.RegisterObjectDirect(“Move”, (Actor*)0, &Actor::Move);

This would be fine if I had access to the underlying state object in the StateManager interface as it would be getting called from the Actor class itself so the cast could be guaranteed. Unfortunately, I need to somehow pass this info to the LuaState::RegisterFunction method so the I don't have to expose the LuaState object and couple my classes to it. As far as I can see though, there is no way to pass information about which type of class to cast to.

Does any body have any suggestions? I thought about trying to use a templated function to perform the cast, but I know that you can't have function pointers to templates, so that is out the window. Thanks!

user3355098
  • 173
  • 7
  • Are all your `userdata` based on classes with virtual functions? – Deduplicator Jan 20 '15 at 21:09
  • You can indeed have pointers to template functions. – gmbeard Jan 20 '15 at 21:40
  • @gmbeard Really? How does that work because I was thinking about doing something like (sorry if formatting is off and this is psuedocode as well): template T* NinjaClass::PerformCast() { return (T*)0; } and then passing that function pointer to the interface. – user3355098 Jan 20 '15 at 21:59
  • @Deduplicator In reality most won't have virtual functions and many wont even be member functions. There may be instances where a virtual member function is used though. I think the LuaPlus libraries use templates to resolve the function pointer at compile time though so it shouldn't matter. – user3355098 Jan 20 '15 at 22:01
  • @user3355098 Given a function `template T f()`, you can create a pointer to it using `int (*fp)()`, which would deduce `T` to be `int`. In your example, as long as it's a static member function, you would use `int* (*fp)() = &NinjaClass::PerformCast;` – gmbeard Jan 20 '15 at 22:28

0 Answers0