Skip to content

State Machine #7

@SimonNitzsche

Description

@SimonNitzsche

HOW TO USE STATES

Declare a state by calling State.create() and assigning it
to your state.
Ex: attackState = State.create()

Call HandleEvent. Every state should define HandleEvent.
This is how you do it.

attackState = State.create()
attackState.HandleEvent = function(msg)
if(msg.name == "Hit")
  DoSomethingAboutIt()
  return 1
else if(msg.name == "Dance")
		...
else
   return 0

Only handle the messages you really care about.
Return 1 if you handled a message, return 0 if you did not

NOTE: you can also define HandleEvent like this:

someGenericFunction(msg)
...
end
attackState.HandleEvent = someGenericFunction

You can optionally add the functions Enter and Exit
These will be called when a state enters or exits

SETTING UP THE STATE MACHINE ---

First, create all the states that you will need (see above on how to do that)

State Machines are created in onStartup

-- Call self:UseStateMachine{}
-- Then assign the sates as such
 	self:AddState { stateName = "stateIdle" }
	self:AddState { stateName = "stateAttack" }
	self:AddSubState { stateName = "stateWin" }  -- stateWin is now a substae of stateAttack

When you are done adding states, call SetState and pass in the name of the first state you
want to start in.

	self:SetState { stateName = "stateIdle" }

What we know: RE

Todo.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions