Sign in to follow this  
infectedbrain

Ideas for Lua integration in a game engine.

Recommended Posts

infectedbrain    157
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?

Thanks in advance,
Dartos

Share this post


Link to post
Share on other sites
andrew111    310
[quote name='jeffkingdev' timestamp='1352837944' post='5000654']
I'm not sure what LuaJIT is,
[/quote]

Well one thing you can do is use c code from within it.

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:
[code]
load_resource("shader_program.lua");
lua_getfield(L,-1,"prog");
GLuint program = *((GLuint*)lua_touserdata(L,-1));
lua_pop(L,2);
..

glUseProgam(program);
[/code]

A resource file might look like:
[code]
prog = CreateShaderProgram {
{"vert",
[[
attribute vec3 a_pos;
void main() {
..
}
]]},

{"frag",
[[
void main() {
..
}
]]},
{"attrib", "a_pos", 0}}

vao = CreateVertexArrayObject {
..
}
[/code] Edited by andrew111

Share this post


Link to post
Share on other sites
uglybdavis    1065
If you're gonna script your game, go balls to the walls! Kinda like unity.

Speaking of.... Take a look at my post here: http://www.gamedev.net/topic/633218-howwho-create-the-gameobjects/page__p__4992951#entry4992951 It describes a component system for game entities.

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++.

Hope that makes sense, i can go more in detail if you want.

Share this post


Link to post
Share on other sites
uglybdavis    1065
Constructor.

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.

Not related to my last post, but here is some pseudocode:
[source lang="cpp"]#include <iostream>

class ScriptComponent : Component {
protected:
WhateverLuaWrapperYouHaveForInternalObject* lua_obj;
std::vector<LuaCoroutineWrappers*> lua_coroutines;
ScriptComponent(const ScriptComponent&);
ScriptComponent& operator=(const ScriptComponent&);
public:
Component(char* luaFile) {
// Some arbitrary way to get your lua "Object"
lua_obj = LoadLuaFile(luaFile)->newObject("type");
lua_obj->Awake();
ObjectEnabled();
Start();
}

~Component() {
ObjectDisable();
lua_obj->Destroy();
delete lua_obj;
}

void ObjectEnabled() {
lua_obj->OnEnable();
}

void ObjectDisable() {
lua_obj->OnDisable();
}

void Start() {
lua_obj->Start();
}

void Update(float dt) {
lua_obj->Update(dt);
for (int i = 0, size = int(lua_coroutines.size()); i < size; ++i) {}
lua_coroutines[i]->ProcessYieldingForNull();
lua_coroutines[i]->ProcessYieldingForTime(dt);
}
}

void FixedUpdate(float dt) { // dt being a constant at this point
lua_obj->FixedUpdate(dt);
for (int i = 0, size = int(lua_coroutines.size()); i < size; ++i)
lua_coroutines[i]->ProcessYieldingForFixedUpdate();
}

void LateUpdate(float dt) {
lua_obj->LateUpdate(dt);
for (int i = 0, size = int(lua_coroutines.size()); i < size; ++i)
lua_coroutines[i]->ProcessYieldingForEndOfFrame();
}

void ObjectWillRender() {
lua_obj->OnWillRenderObject();
}
};[/source]

Referring to an object lifecycle that looks something like so:
[img]http://www.third-helix.com/blog/wp-content/uploads/2012/02/unity-lifetime.png[/img] Edited by uglybdavis

Share this post


Link to post
Share on other sites
Lukas Irwin    125
[quote name='infectedbrain' timestamp='1352866847' post='5000782']
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.
[/quote]

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.

[IMG]http://i1211.photobucket.com/albums/cc428/xhrit/Xenos%20Engine/xenos_diagram.jpg[/IMG]

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

http://sourceforge.net/projects/xenosengine/

Here is the engine compiled, with a game.

http://www.indiedb.com/games/dungeon-tactics Edited by xhr17

Share this post


Link to post
Share on other sites
coderx75    435
[quote name='uglybdavis' timestamp='1352856096' post='5000737']
If you're gonna script your game, go balls to the walls!
[/quote]
Agreed!

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

Share this post


Link to post
Share on other sites
uglybdavis    1065
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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by uglybdavis

Share this post


Link to post
Share on other sites
infectedbrain    157
[quote name='uglybdavis' timestamp='1353001739' post='5001292'] 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. [/quote]

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

[quote name='uglybdavis' timestamp='1353001739' post='5001292']I don't want to waste time initializing an object in the constructor if the deserialization step is going to overwrite those changes anyway.[/quote]

Can you explain this statement a bit more? What should the deserialization step do?

Share this post


Link to post
Share on other sites
uglybdavis    1065
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.

Share this post


Link to post
Share on other sites
uglybdavis    1065
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)

Share this post


Link to post
Share on other sites
Beem    127
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.

Share this post


Link to post
Share on other sites
ddn3    1610
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. Edited by ddn3

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this