• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
infectedbrain

Ideas for Lua integration in a game engine.

16 posts in this topic

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
1

Share this post


Link to post
Share on other sites
[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
1

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites
[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
1

Share this post


Link to post
Share on other sites
[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
2

Share this post


Link to post
Share on other sites
Why should I have a separate function for initialization when I can just start everything in the constructor?

P.S. Thanks everyone for all this great feedback!
0

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites
[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?
0

Share this post


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

Share this post


Link to post
Share on other sites
Oh okay. So you serialize object then deserialize them when you use them or am I just mixing up two different things?
0

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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
2

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  
Followers 0