Ideas for Lua integration in a game engine.

Started by
15 comments, last by ddn3 11 years, 5 months ago
If you think the best place to do it is the constructor, then the best place to do it is the constructor!
Code is very objective and personal, what makes sense for one person might not for another. The only way to find out what the best way is to try.

Me, i always separate construction and initialization. It makes sense to me. I often find myself needing to take different paths to initialize something (IE, make an object from nothing vs deserialize the object form a datastream). I don't want to waste time initializing an object in the constructor if the deserialization step is going to overwrite those changes anyway. Or the more common thing is i don't delete objects unless i absolutely have to. When i don't need an object i don't delete it, i pass it back to the object factory which adds it to an array of dead objects. Then when i need a new object, one of these dead objects is given back to me and i just re-initialize it with new data. This helps avoid extra allocations / memory fragmentation. Useful for the devices i develop for.

Jump in head first, try what you think is best and if you find a better way later just change it smile.png
Advertisement
Then when i need a new object, one of these dead objects is given back to me and i just re-initialize it with new data.


You create a object recycler?? I never thought of that.

I don't want to waste time initializing an object in the constructor if the deserialization step is going to overwrite those changes anyway.


Can you explain this statement a bit more? What should the deserialization step do?
Recycler is one way to put it :) On devices like the 3DS where you must manage memory because there is no new it's pretty essential.

Deserializing an object is taking data from some arbitrary format you saved out (or serialized) and making an object out of it.
An oversimplified example:
[source lang="cpp"]void Object::Serialize(std::ofstream& ofs) {
ofs.write((char*)&member1, sizeof(member1));
ofs.write((char*)&member2, sizeof(member2));
}

void Object::Deserialize(std::istream &is) {
is.read((char*)&member1, sizeof(member1));
is.read((char*)&member2, sizeof(member2));
}[/source]

When your game exits, you serialize the current scene (all active objects / states). Then when the game re-opens you deserialize the entire scene, that way you can just resume play, without needing to go trough the menu and restarting the level (useful for social games, like the Simpsons tapped out). If you have a more traditional game that needs a menu this makes a great save system.
Oh okay. So you serialize object then deserialize them when you use them or am I just mixing up two different things?
Yeah, you got them way mixed up. You serialize objects when your game closes, to save the state of your game. You deserialize object when your game loads, to resume the game from the point of serialization.

In any case, don't worry about it. Make your engine, and when you come across the need to include a serializer deal with it. If the need doesn't pop-up then don't worry about it. The most important thing you can do is start coding and learning from the mistakes you make (or don't make)
I want to play devil's advocate with this post. Scripting sounds cool and all, but you are making asmall project, and you are going to find that going back in forth between scripting and getting the scripting to work with your engine, is going to be incredibly difficult and take a really long time, and you won't have anything worth showing anyone for a long time. I know a lot of people use Unreal, which is all about scripting, and so when it comes time to make your own engine, you're like "I want scripting, like Unreal". I think you should take a note from id Software, instead of using a scripting language, just use good old C++ and put your AI and game flow into a DLL file, then you just have to write some kind of SDK, a pure virtual class, or list of callback functions. It's going to save you a lot of headache, plus you will know exactly how memory is being managed.
I use to think that scripting wasn't worth the effort for "small" project but it turns out those are probably the best project for scripting. Why?

Scripting is a work multiplier, create a domain specific language for your game making every line you write do more work than trying to do it in something like C++ / C# / Java etc.. Small projects are usually limited by manpower, the more work you can get done in fewer lines of code, over the long run results in massive increase productivity..

2nd for small projects, it's less risky to try new things. Learning what to do and what not to do when integrating and building scripting framework is the same for a small project as a big one. The only difference is the mistakes u make on a "big" project will cost u much more $$$$ and manpower to fix. Better to learn on a small project..

3rdly the more languages you learn the more out of the box thinking you will do from your dominate language giving u better insight and more rounded approach to programming in general.

This topic is closed to new replies.

Advertisement