Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


Ideas for Lua integration in a game engine.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 infectedbrain   Members   -  Reputation: 157

Like
1Likes
Like

Posted 13 November 2012 - 01:32 PM

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

Sponsor:

#2 jeffkingdev   Members   -  Reputation: 785

Like
5Likes
Like

Posted 13 November 2012 - 02:19 PM

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)

Jeff.

#3 CaptainMurphy   Members   -  Reputation: 262

Like
1Likes
Like

Posted 13 November 2012 - 05:01 PM

I'm not sure what LuaJIT is,


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

glUseProgam(program);

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

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

vao = CreateVertexArrayObject {
..
}

Edited by andrew111, 13 November 2012 - 05:09 PM.


#4 uglybdavis   Members   -  Reputation: 962

Like
1Likes
Like

Posted 13 November 2012 - 07:21 PM

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.

#5 infectedbrain   Members   -  Reputation: 157

Like
1Likes
Like

Posted 13 November 2012 - 10:20 PM

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.

#6 infectedbrain   Members   -  Reputation: 157

Like
0Likes
Like

Posted 14 November 2012 - 06:21 AM

Continuing this brainstorming topic, should I bind the C++ functions to lua in the constructor or init function?

#7 uglybdavis   Members   -  Reputation: 962

Like
1Likes
Like

Posted 14 November 2012 - 11:23 AM

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:
Posted Image

Edited by uglybdavis, 14 November 2012 - 11:24 AM.


#8 xhr17   Members   -  Reputation: 125

Like
1Likes
Like

Posted 14 November 2012 - 12:44 PM

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.

Posted Image

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, 14 November 2012 - 12:48 PM.


#9 coderx75   Members   -  Reputation: 406

Like
2Likes
Like

Posted 14 November 2012 - 02:38 PM

If you're gonna script your game, go balls to the walls!

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

Quit screwin' around! - Brock Samson

#10 infectedbrain   Members   -  Reputation: 157

Like
0Likes
Like

Posted 15 November 2012 - 07:40 AM

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!

#11 uglybdavis   Members   -  Reputation: 962

Like
1Likes
Like

Posted 15 November 2012 - 11:48 AM

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 Posted Image

Edited by uglybdavis, 15 November 2012 - 11:52 AM.


#12 infectedbrain   Members   -  Reputation: 157

Like
0Likes
Like

Posted 15 November 2012 - 01:22 PM

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?

#13 uglybdavis   Members   -  Reputation: 962

Like
0Likes
Like

Posted 15 November 2012 - 03:07 PM

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.

#14 infectedbrain   Members   -  Reputation: 157

Like
0Likes
Like

Posted 15 November 2012 - 07:32 PM

Oh okay. So you serialize object then deserialize them when you use them or am I just mixing up two different things?

#15 uglybdavis   Members   -  Reputation: 962

Like
1Likes
Like

Posted 16 November 2012 - 03:23 AM

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)

#16 Beem   Members   -  Reputation: 127

Like
1Likes
Like

Posted 18 November 2012 - 10:50 AM

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.

#17 ddn3   Members   -  Reputation: 1331

Like
2Likes
Like

Posted 19 November 2012 - 02:52 PM

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, 19 November 2012 - 02:53 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS