TIGSource Forums

Developer => Technical => Topic started by: MrGando on August 29, 2010, 05:39:41 AM



Title: Using Lua to create Game Entities on C++ Game Engine ?
Post by: MrGando on August 29, 2010, 05:39:41 AM

I really want to add Lua Scripting to my Game Engine. I am working with Lua and bound it to C++ using luabind, but I need help to design the way I will construct my Game Entities using Lua.

Engine Information:

Component Oriented, basically each GameEntity is a list of components that are updated in a delta T interval. Basically Game Scenes are composed by collections of Game Entities.

So, here's the dilemma:

Let's say I have this Lua file to define a GameEntity and it's components:

Code:
GameEntity =
{
 -- Entity Name
 "ZombieFighter",

 -- All the components that make the entity.
 Components =
 {
  -- Component to define the health of the entity
  health =
  {
   "compHealth",  -- Component In-Engine Name
   100,    -- total health
   0.1,    -- regeneration rate
  },

  -- Component to define the Animations of the entity
  compAnimation =
  {
   "compAnimatedSprite",

   spritesheet =
   {
    "spritesheet.gif", -- Spritesheet file name.
    80,     -- Spritesheet frame width.
    80,     -- Spritesheet frame height.
   },

   animations =
   {
    -- Animation name, Animation spritesheet coords, Animation frame duration.
    {"stand", {0,0,1,0,2,0,3,0}, 0.10},
    {"walk", {4,0,5,0,6,0,7,0}, 0.10},
    {"attack",{8,0,9,0,10,0}, 0.08},
   },
  },
 },
}

As you can see, this GameEntity is defined by 2 components, "compHealth" and "compAnimatedSprite". Those two totally different components require totally different initialization parameters. Health requiring an integer and a float ( total and regeneration ), and on the other side, animations requiring a sprite sheet name , and to define all of the animations ( the frames, durations, etc ).

I would love to make some kind of abstract class with some virtual initializer method that could be used by all my components that require Lua binding in order to facilitate initialization from Lua, but it seems difficult, because the virtual class is not going to have one virtual init method. This is because all of the component initializers (or most of them) require different init parameters (health component requires different init than Animated Sprite component, or AI component).

What do you suggest to make the Lua bindings to the constructors of this components easier ? or how would you do it ?

PS: I must use C++ for this project.


Title: Re: Using Lua to create Game Entities on C++ Game Engine ?
Post by: dcarrigg on August 29, 2010, 06:34:35 PM
I have a virtual constructor for my component classes, and each essentially takes a single argument of a DataTable (which is essentially a dictionary or lua table of variables). Then, in the constructor it can get whatever it needs out of the dictionary.


Title: Re: Using Lua to create Game Entities on C++ Game Engine ?
Post by: shadowdim on August 30, 2010, 03:44:01 AM
Use C++ templates. A bit hard to use at first, but it's really awesome. :)


Title: Re: Using Lua to create Game Entities on C++ Game Engine ?
Post by: Hima on August 30, 2010, 06:03:31 PM
Use C++ templates. A bit hard to use at first, but it's really awesome. :)
Could you please clarify this a bit? I'm interested to know how to tackle this problem too. Thanks! :)


Title: Re: Using Lua to create Game Entities on C++ Game Engine ?
Post by: RCIX on August 31, 2010, 12:21:05 AM
The best way is to, instead of having

Code:
   GameEntity =
    {
, change it to

Code:
   GameEntity {
That will call the GameEntity function with the table as the argument. Then you can export a C++ function that takes the table and constructs a game object class out of it.


Title: Re: Using Lua to create Game Entities on C++ Game Engine ?
Post by: vittorioromeo on September 01, 2010, 01:23:55 PM
I have the same problem, for a tile based game made in C#. How do I proceed? "Link" a single lua file to an Entity then call that file every update turn? But that won't be able to save variables in the lua files.

There has to be a way to put the "lua file" on hold and call a Think function every turn.


Title: Re: Using Lua to create Game Entities on C++ Game Engine ?
Post by: JOBA on September 02, 2010, 12:34:36 AM
I have the same problem, for a tile based game made in C#. How do I proceed? "Link" a single lua file to an Entity then call that file every update turn? But that won't be able to save variables in the lua files.

There has to be a way to put the "lua file" on hold and call a Think function every turn.
If you're using .NET you don't need to use lua.
You can compile and load .NET assemblies at runtime (which means you can script your game objects either using C#, VB.NET, F#, etc)

After you've compiled and loaded your assemblies, you can use reflection to enumerate all the classes in them, create instances of them and even edit the object properties using propertygrid(or your own widget that uses reflection).

It's actually ~ how Unity works.


Title: Re: Using Lua to create Game Entities on C++ Game Engine ?
Post by: dcarrigg on September 02, 2010, 07:43:09 AM
I have the same problem, for a tile based game made in C#. How do I proceed? "Link" a single lua file to an Entity then call that file every update turn? But that won't be able to save variables in the lua files.

There has to be a way to put the "lua file" on hold and call a Think function every turn.

If you do want to use lua, what you're saying is pretty close to how you would want to do it.

The general idea is that you have a lua state, and when the entity loads, you load the lua file in that state. Then you call the functions when needed.

If you create a new lua state per entity, you can load the file and call the "think" function every update of your game logic. In the lua script, you can use global variables and these will be saved between different calls of the think function.

If you use a single lua state and load all of the script files into it, you'll need to make sure that the each of the functions are uniquely named (ball_think, enemyTurtle_think, etc) and then know which function you should call based on what entity you are updating. The second part to this is that you won't be able to store global variables in your script. A common way to get around this is to have one global table, which uses your entity ids as keys to other tables. This way, every entity has its own table to store whatever data it needs.


Title: Re: Using Lua to create Game Entities on C++ Game Engine ?
Post by: vittorioromeo on September 02, 2010, 10:39:58 AM
I have the same problem, for a tile based game made in C#. How do I proceed? "Link" a single lua file to an Entity then call that file every update turn? But that won't be able to save variables in the lua files.

There has to be a way to put the "lua file" on hold and call a Think function every turn.
If you're using .NET you don't need to use lua.
You can compile and load .NET assemblies at runtime (which means you can script your game objects either using C#, VB.NET, F#, etc)

After you've compiled and loaded your assemblies, you can use reflection to enumerate all the classes in them, create instances of them and even edit the object properties using propertygrid(or your own widget that uses reflection).

It's actually ~ how Unity works.


Could you elaborate further? I've heard you could do stuff like this but I never understood how. Is there a guide or a tutorial?


Title: Re: Using Lua to create Game Entities on C++ Game Engine ?
Post by: dlan on September 05, 2010, 03:16:17 AM
@SuperV

You could use csharpscript http://www.csscript.net/, it allow you to use a regular cs file as a script file. Also, try to avoid lua with dotnet, I have used luainterface as a scripting engine before, and it perform terribly when used in a frame loop.