I am working on my first ACTUAL game enigne and I wanted some advice from the good people who roam these fourms.

I plan on using LuaJIT as a scripting language for the game but I am not sure how it is usually integrated. I can come up with some simple way to get it in the engine but what I wanted some advice on was.. really what to use Lua for.

In what systems should I integrate Lua (I know AI is one but that's it) and, if you don't mind, could you spare some tips for integrating it?

I'm not sure what LuaJIT is, but Lua can be used to control any parts of your game. It's nice because you can easily change logic and not have to recompile the game.

I use it for scripting events in the game. Take for example, you have a 3d shooter. You might have a script function for doors. Like 'IsPlayerAtWpt('Door1')'. While that's false, do nothing and wait a second and check again. Then if they are, you can call something 'OpenDoor('Door1')'. So, when they move towards a door, it opens.... stuff like that is great for scripts.

I'm not sure if that answered your question. Both of those functions IsPlayerAtWpt(name) and OpenDoor(name) would call down into more elaberate C++ functions that check your distance against a sphere perhaps and the other would simply play an animation (dynamic or premade)

I've just started looking at LuaJIT recently, and an idea I had apart from scripting world objects, was to use it to setup all the monotonous code, such geometry data (eg vertexarrayobjects, vertex buffers, attribute bindings, index buffers etc), shader programs, textures, texture samplers etc. Using luajit ffi to minimise the amount of cbindings necessary.

And then in your main program, load those 'resource files' into a table and just say:

Following that pattern, a script component would need a string for what script file to execute and a dictionary (hash table) of variable values to set.

Then, each script has an Awake, Update, LateUpdate, Destroy, etc... Attached to a game object, the script component (written in c++, don't care about what binding you use) can call the update function of the lua object it owns. You will need to expose math and other utility functions to the script, but done right your whole engine can be scripted. You should be able to make a full game using your ending without needing to touch C++.

Ideally you would want: Constructor > [optional] Deserialization > Initialization. The nice thing about LUA reading plain text is you can at any point save the state of a lua file (Or lua object as i like to think of it), then read it back in.

So basically I would write functions into the C++ classes (components) and just make lua bindings for them then call them in lua? that sounds.... genius.

That is exactly what I do in my game engine. Once the engine is compiled, you can use Lua to create everything in game without ever having to recompile again. Menus, Logic, AI, serialization, synchronization, everything.

Here is a link to my engine's source in case you want to check it out :

So that I can do a lot of prototyping and be free to try out new ideas, I make everything scriptable. I have a core executable that fires up the Lua VM in a thread and registers some built in systems in Lua and it's own system of extensions, such as memory management, event handling, streaming, logging, etc. It then pulls in DLLs from an extensions directory, passes it's own API pointer to an entry function in each so that the DLLs can register their own functions as extensions in the core executable. At this point, DLLs can request the APIs of other DLLs and all functions can be called from Lua. Most of these functions are for setup with a low-level "update" function handling the bulk. So, there's not much loss in performance.

With the Lua VM fired up, a kernel script is loaded that manages other scripts as "applications". This allows control over what each script has access to (see function environments) so that player scripts don't have access to low-level functionality. I also have a debug feature that uses Lua's built in line-by-line debug feature to "wrap" a script without affecting other scripts (would be insanely slow otherwise).

I've been working with this for awhile and it's saved me incredible amounts of time. Have fun! =D

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

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.

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.