Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How to create/use enumerated types/const values in Lua

Credits go to Trent Gamblin for helping out!
This thread is locked; no one can reply to it. rss feed Print
How to create/use enumerated types/const values in Lua
Don Freeman
Member #5,110
October 2004
avatar

Is it possible to define enumerated types in Lua? If so (and I'm sure it is), then how do I do it? I want to basically do something like:

This is in c/c++:

enum UnitType
{
   UnitType_SomeUnit1,
   UnitType_SomeUnit2,
   UnitType_SomeUnit3,
};

This would be in Lua:

game = GetCurrentGame()
player = GetCurrentPlayer(game)
unit = CreateUnit(UnitType_SomeUnit)
AddUnitToPlayer(unit,player)

[Edit]
Also, I did have another question...hehehe. ::) Is it possible to statically link Lua? I thought of switching to AngelScript instead because the code is pretty much just like c/c++ and I can use objects and types, but I've already started learning to interface Lua and c/c++ and I really don't want to get distracted just yet.
[/Edit]

As always, thanks in advance! :D

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Trent Gamblin
Member #261
April 2000
avatar

Lua doesn't really have enumerations... I would probably just define them one after another

UnitType_SomeUnit1 = 0;
UnitType_SomeUnit2 = 1;
...

Or maybe

UnitTypes = {
SomeUnit1 = 0,
SomeUnit2 = 1,
SomeUnit3 = 2
}

And yes, you can static link lua.

Don Freeman
Member #5,110
October 2004
avatar

So, how would I keep them synchronized between the program and the script? Would I have to define both in the c/c++ program and the script then? How do you do some of the scripting in Monster/Monster 2? I don't want to steel you code or game, but I would like some examples of how to properly add the script engine to the game engine.

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Arthur Kalliokoski
Second in Command
February 2005
avatar

Doesn't Lua allow #includes? Or the syntax is too different? You could have a simple text file with the actual values.

They all watch too much MSNBC... they get ideas.

Trent Gamblin
Member #261
April 2000
avatar

You can set values from C/C++. First set up your lua state. Then the process is something like this:

lua_pushnumber(lua_state, "0");
lua_setglobal(lua_state, "UnitType_SomeValue1");
...

Don Freeman
Member #5,110
October 2004
avatar

Oh, okay. :) So by doing that, UnitType_SomeValue1 would be available to the script.

Not really programming related, but more of a design issue: Do you think it would be a good idea to create a generic object (such as unit) and have the script(s) build more advanced objects from them:

#SelectExpand
1// C/C++ 2enum MoveTypes 3{ 4 MoveType_Land, 5 MoveType_Air, 6 MoveType_Naval 7}; 8class Unit 9{ 10 String m_Name; 11 String m_Description; 12 UInt m_MaxHealth; 13 Float m_Health; 14 UInt m_MovePerTurn; 15 UInt m_BaseCost; 16 UInt m_VisonRange; 17 UInt m_MaxFuel; 18 UInt m_Fuel; 19 enum MoveTypes m_MoveType; 20};

Then in the script you could make an Infantry or Tank (or whatever) unit by adjusting the different values.

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Trent Gamblin
Member #261
April 2000
avatar

Maybe. I like to do as much logic as possible in my scripts. In my games the entities are always scripted. So I might have NPCs, doors, portals, etc instead of ships and planes and stuff. I like to define the object interface in C++ and the object logic in lua. I usually go for something simple like this:

function start()
  -- initialize entities and do setup
  some_npc = add_npc("farmer", x, y)
end

function update()
  if (crops_need_harvesting()) then
    harvest_crops(some_npc) -- some_npc is a handle which maps to the entity in C++
                            -- this call could run pathfinding to get the farmer to the crop
  end
end

That's a simplified version of it. In my latest game I have collide and uncollide lua callbacks and a few others.

I say try and separate what the script needs from what the C++ code needs. For example, I have "tweens" (I think that comes from flash), which are seen from C++ as nothing but callback functions. From lua though, each tween is defined by a table with all sorts of data, including type, and data needed by that type. One I have is a jump tween, because my player can only jump at certain points to certain other points. When the player presses the jump key while colliding with some unseen entity (which is tested for in lua logic, and the jump point is marked somehow visually), I create a table with the jump start position, jump end position, etc, etc, and then register the tween id with C++. All C++ does is call a function, run_tween(id), which looks up that tween table by id and then calls its callback with the table as a parameter. It runs each tick, updating the players position, animation, and so forth. When the function returns a certain value, it's removed from the list of tweens.

The point is, all of this logic that can be done in the script, probably should be, while the low level and cpu intensive stuff can be done in C++. Lua lends itself really nicely to scripted events. In my example, the jump tween would be twice as many lines of C++ as Lua, I think, or more. You don't have to deal with the strictness of the C++ language to code your logic. I like it, anyway.

bamccaig
Member #7,536
July 2006
avatar

If you could show a practical example or real code then I think that it would be appreciated by many. :) I know I would appreciate it.

Trent Gamblin
Member #261
April 2000
avatar

Here's a slighly stripped down version of one of my area scripts:

#SelectExpand
1 2function start() 3 ryori_house_interior_exitmask = add_entity( 4 "ryori_house_interior_exitmask", 5 1, 750, 990 6 ) 7 mother = add_npc("mother", 1, 716, 680) 8 9 local mother_idle_tween_1 = create_idle_tween(5) 10 local mother_idle_tween_2 = create_idle_tween(5) 11 local mother_astar_tween_1 = create_astar_tween(mother, 659, 615) 12 local mother_astar_tween_2 = create_astar_tween(mother, 756, 734) 13 14 mother_astar_tween_1.next_tween = mother_idle_tween_1 15 mother_idle_tween_1.next_tween = mother_astar_tween_2 16 mother_astar_tween_2.next_tween = mother_idle_tween_2 17 mother_idle_tween_2.next_tween = mother_astar_tween_1 18 19 new_tween(mother_astar_tween_1) 20end 21 22function update() 23end 24 25function collide(id1, id2) 26 if (colliding(id1, id2, 0, ryori_house_interior_exitmask)) then 27 change_areas("ryori_house_top", DIR_S, 948, 560) 28 end 29end 30 31function uncollide(id1, id2) 32end 33 34function stop() 35end

That's enough that when you walk to the exit, the area changes, and the mother paces back and forth between two spots.

Don Freeman
Member #5,110
October 2004
avatar

Thanks Trent! I almost forgot Monster2 was on Google Code. ::) I'm not gonna steal your code, but will study how you integrate Lua and your engine. This is my first attempt at adding scripting to my games, and I do have to admit it is pretty easy...or at least easier than I thought it would be. If I ever get this damn war game done, I plan on working on an rpg next...maybe update monday to work with the new API and who knows, maybe finish it up as well. ::) I appreciate you explaining how you add scripting into your game. It's kinda hard to go from a strict typed language like c/c++ to something that doesn't really care what the object type is. It does make for easier coding, and when you remember that there is no real type checks, you can do some pretty advanced stuff with a lot less code than c/c++. I've already got my mouth watering with some possibilities. 8-)

Trent: Do you plan on making an updated version of Monster 2 for the PC? I mean more like the one on the iPhone...I don't have an iPhone, but the graphics and everything look and seem like they are a lot better. I've tried the PC version, and I liked it. :D If so, I would be willing to buy it. 8-) I don't know who you have as an artist, but I really like the old school look...oh the days of FF1-4 (I never could really get into ff after 5).

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Trent Gamblin
Member #261
April 2000
avatar

I don't plan on porting M2 back to PC. If you're talking about the PC version, then I had a lot of help from people around here with the artwork, and did a lot myself. The iphone artwork was done by two people, who I am still working with on our new game (though one of them is now doing music -- he's multi-talented :P).

Go to: