Advertisement Jump to content
Sign in to follow this  

Should I use factories/Singletons? Need some pointers

This topic is 3872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My current game is very OO orientated, no singletons, no global factories, no globals, pretty much any class can be ripped out into another project without needing to take any other class with it, etc, etc. I'm actually very happy with the way it's progressing, it's at a point where I can sit back, look at it all, and actually be happy with how it all fits, and can very easily understood. </endselfsatisfaction> lol However... I've recently written Lua into my game as such that a "level" script looks like:
cube1 = Entity('cube')
cube1:SetPosition(1, 3, 0)
function cube1:OnUpdate(t)
    self:Rotate(1*t, 0, 0)
My problem is, Lua is too decoupled, lol. At some point it goes:
m_pLua->RunScript(L"level1.lua"); (script above)
<<Lua intercepts Entity()>>
template <class T>
int LuaClass<T>::__New(lua_State* pLuaState)
    <<do stuff>>
    T* pObject = new T(pLuaState);
LuaEntity::LuaEntity(lua_State* pLuaState)
    <<get args>>
    if (strcmp(arg1, "cube") == 0)
        m_pEntity = new Entity(pCubeMesh);
My problem; how do I get this CubeMesh? lol Pre-Lua, I'd been hard-coding everything, so I'd never had a problem with trying to get something like a mesh all the way out over there. At that point, LuaClass<T> is never instantiated (since all the main callbacks need to be static/global, it's essentially a glorified namespace). I could create an instance of it I guess, but once again, it's kinda pointless with everything being static, it really shouldn't be instantiated. And frankly, I'd rather not have to pass some number of parameters when I new(), since down the road, I know I'm going to need other possible parameters. Add to the fact that because Lua is "oh so much fun" and without templating it'd be a mad house, if I want different classes, I'd essentially have to make the constructors on all "Lua Classes" have a stupid amount of arguments. So... I'm considering creating a factory/global type class: m_pEntity = MeshList::GetMesh('cube'); While it "looks neat", I really don't know if I want to start doing things like that... But it really would be the nicest way of doing it (that I can think of). Opinions? Comments? Abuse? How do other people do scripting entity creation where it is so decoupled from the rest of the game like this?

Share this post

Link to post
Share on other sites
Write a Lua_State wrapper class, into which you pass the external references.

There should be even some examples on how to do that on Lua or related sites.

Then, when you receive callbacks, they are aware of this state. It even allows you to run several states at the same time, keeping scripts separate, reloading them, running them in different threads, etc...

For Lua callbacks, you can bind member functions, that are resolved through free functions. The syntax is a bit annoying, but it's wrapped by several different APIs, and even doing it yourself isn't that hard.

Lua can be fully encapsulated, meaning that even though it's static in nature, it can be wrapped into different instances of classes. At very least, I don't remember too many issues doing that.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!