0

I want to create FSM in Lua (using Luabind). Let's say I have GameObject, in lua file I do write this:

State1 = {}
State1["Start"] = function()
end
State1["Update"] = function()
    if (blah blah blah) then
        ChangeState(State2);
    end
end
State1["End"] = function()
end
... then states 2, 3 and etc...
gameObject.fsm = CreateFSMComponent(name, State1);

The question is - how to create the SAME object behavior with a DIFFERENT properties? Like.. if I would like to create 2 patrolling units with the same behavior but each of them would have the different start position. But in Lua - when I define external functions to describe these states within it - it doesn't work.

Alexey Teplyakov
  • 316
  • 3
  • 10

1 Answers1

2

Are you asking how to create two objects in the same state (behaviour), but have different properties (like start position). If so, you can take either a functional approach, where you assume that the States just represent a series of functions operating on your objects (like PatrolUnits) like:

PatrolState = {}

PatrolState.Start = function(unit)
    if(SomeChecker(unit.startPos)) then
        DoSomething() -- like unit.curPos = unit.startPos, for example            
    end
end

PatrolState.Update = function(unit)
    if(IsNearSomething(unit.curPos)) then
        DoSomethingElse() -- like ChangeState(State2)
    end
end

, or an object oriented approach. In this case, you will have to make objects of type State1,2 ... and keep them around and you will be able to save properties like startPos in the state objects. For more info on how checkout this tutorial.

I prefer the former, since the States stay 'pure' and don't save unit's properties. But that's just a question of style.

-arun

edit: If your unit's position take special meaning based on what it's values are that logic can be embedded in the state machine code :

PatrolState.Update = function(unit)
    if(InsideDeathCircle(unit.position)) then
        ChangeState (KillUnitState)
    end
end
Arun R
  • 873
  • 6
  • 10
  • Thanks for the answer, but what if unit has no startPos property? I mean - unit has, maybe, position component(which is written in c++), but the startPos or deathPos or smth - parameters related to the logic. is it necessary to have all kind of properties in the c++ component? I feel it's bad. How should I keep (and pass to the functions) these optional properties – Alexey Teplyakov Jul 26 '13 at 16:24
  • I changed my answer w.r.to your question. In general, if you think something is truly the unit's property - like position, count etc - I don't see anything with adding them as properties. But if they're actual logic you can make Lua functions in the state. If you're uncomfortable with this design, and feel adding properties to Lua state is more intuitive, you can always make the States as classes and associate unique objects of the state types to your units. The link about Object Oriented development in Lua, that I posted in my answer, has more more info on this. – Arun R Jul 26 '13 at 17:58