![]() |
|
How to create/use enumerated types/const values in Lua |
Don Freeman
Member #5,110
October 2004
![]() |
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] As always, thanks in advance! -- |
Trent Gamblin
Member #261
April 2000
![]() |
Lua doesn't really have enumerations... I would probably just define them one after another UnitType_SomeUnit1 = 0; Or maybe UnitTypes = { And yes, you can static link lua.
|
Don Freeman
Member #5,110
October 2004
![]() |
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. -- |
Arthur Kalliokoski
Second in Command
February 2005
![]() |
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
![]() |
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
![]() |
Oh, okay. 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: 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. -- |
Trent Gamblin
Member #261
April 2000
![]() |
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
![]() |
If you could show a practical example or real code then I think that it would be appreciated by many. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Trent Gamblin
Member #261
April 2000
![]() |
Here's a slighly stripped down version of one of my area scripts: 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
![]() |
Thanks Trent! I almost forgot Monster2 was on Google Code. 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. -- |
Trent Gamblin
Member #261
April 2000
![]() |
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
|
|