How do I Create a Finite State Machine

0 favourites
  • 7 posts
From the Asset Store
State Machine
$10.99 USD
State Machine is a great tool for managing the state of anything in your game.
  • Hello guys

    How have you solved making a simple State Machine in Construct 2?

    I'm not talking big advanced ones, only minor ones to create some AI-feeling.

    All ideas you may have are appreciated. But please keep it simple So I learn.

    I've experimented with this working with booleans, I'm wondering if there are other ways you could achieve this?

    Right now I'm working on making a "Worker" kind of like in Empire Earth, that gather resources and then unload it in a town house. I've managed, so far, to make it work pretty nicely... However, I'm having problems when I have more than 1 worker active.

    ...So if I have a condition that this specific worker, identified with a unique id - triggers when overlapping this specific resource.

    When I perform actions when these conditions are met - other workers also sometimes do the actions.

    Have I missed something here, on how the mechanics work in Construct 2. On how conditions are met, and actions are performed. What have I been missing? Any suggestions on what may be in conflict would be very helpful.

    How would you design a basic wandering enemy - that has some basic states, like "Roaming" - "Targeting" - "Attacking" - "Evading" ? Using states...

    Ok - that's the question's.

    I now will describe a bit more the problems I have, it can be a bit long to read - but it's not needed for you to read to answer my question, only if you want to.

    -

    ---

    My programming friends says they are using classes when they create State Machines

    sometimes they do it with Switches. Mostly they work in C or Java.

    Booleans or Switches are the only way I can think of right now of using to achieve this in Construct.

    I don't wanna use families, cause I haven't afford to buy the full version of Construct yet.

    There seems to be some conflict in separating the objects in Construct.

    To explain...

    Say you have a state "Found Resource"

    then you want the worker to go to the resource. When he arrived You perhaps have a state like "At resource"

    And later "Extracting Resource". Something kind a like that

    I make this work perfectly using only one instance of my Worker. The states on the worker seems to work good. But when I create a second Worker, or more, some actions are performed on all workers (this I don't wanna).

    Even though I have a unique identification of each worker - to make construct separate different instances. Somehow sometimes, construct perform some actions on all the workers.

    I don't know which mechanic that is in play here and that is the problem.

    For example, if I have a boolean "ExtractWood"

    And then I have a condition like "If worker is ExtractWood"

    it should do certain actions.

    The problem is that Construct perform actions to other workers, even though they are not "ExtractWood"

    I may have other conditions as well, like sensing if the Worker are overlapping the correct resource, and so on...

    So lets say my worker have targeted a resource - it is on its way there - then arrives.

    Then a condition comes in to play when the worker are overlapping that specific resource.

    What we have now is a specific worker (alone) overlapping a specific sprite (resource).

    So if I have a condition that this specific worker, identified with a unique id - triggers when overlapping this specific resource.

    When I perform actions when these conditions are met - other workers also sometimes do the actions.

    Have I missed something here, on how the mechanics work in Construct 2. On how conditions are met, and actions are performed. What have I been missing? Any suggestions on what may be in conflict would be very helpful.

    You can do so much designing a game without any programming at all. There are basic functions and methods to achieve stuff that is common regardless of programming language or editor. That's exactly what I'm interested in to know more about but with some practical examples that I can try in Construct.

    This post is exactly what I'm talking about:

    ...and right now I'm interested in creating a finite-state-machine, and later implement that in Construct 2.

    And in the future start working on a Real Time Strategy game.

    -

    That's THE END of my little novel here Sorry if I've written too much.

    Regards!

    ///Soulmachine!

  • For one, you can use 'For each', then check your condition. That forces C2 to look at each of your objects individually, rather than lumping them together. Second, a simple state machine typically uses an integer, which you set to constants. So have const: ROAMING=1, FOUND=2, etc. Then you just check each of these values to see what state you are in, and process accordingly. This is similar to the Switch concept.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • It sounds like you (not uncommon) get a bit confused about how picking works in C2.

    But the way you can look at picking of objects is like a sorting or reduction machine.

    Imagine you have 100 workers with the following attributes.

    100 Workers in total

    20 Workers are wearing a red shirt.

    40 Workers are wearing a blue shirt.

    30 Workers are wearing a yellow shirt.

    10 workers are not wearing a shirt.

    30 Workers are between 40 and 50 years old.

    50 Workers are between 20 and 30 years old,

    20 Workers are between 60 and 80 years old (No pension for these poor bastards!)

    Lets say you want all workers wearing blue shirts and that are above 60 year old to either go home or go to lunch.

    So what happens is:

    Pick all Workers : You pick all workers that match the condition that they are workers, in this case all are so its 100. (You don't really need this, its only for the explanation.)

    Workers must have blue shirts : This will reduce the number to 40 workers.

    Workers must be older than 60 : Depending on how many of those 40 workers matching this you will get a new number that further reduce or sort the amount of workers matching the former condition. Lets assume that its 15 workers.

    Now we cant reduce the amount anymore and we have found all the workers that match all our conditions. And since we want them to either go home or to lunch, and don't care how many goes where. We can do the following.

    For each Worker : This will go through each of the 15 remaining workers and tell each of them to either go home or to lunch.

    Go home or Go to Lunch

    So that's pretty much how it works whenever you pick objects, that you keep reducing or sorting them until those that remains fit whatever you want. And then if you want them to do different things you throw a "For each" at the end and then the options they have afterwards.

  • Thanks very much guys

    Very interesting... it helps a lot with the explanation on the picking in C2. I will do some hands on work, to learn more about it.

    Right now I'm researching finite state machine and I would really like to learn how I can create a simple one in construct. I want to learn the mechanics so perhaps in the future I would be able to make some sort of AI opponent in a small RealTimeStrategy-game.

    I'm not a programmer, just a creative person that's been working in and around games, and game making. My main function is sound - but I'm interested in all aspects of creating games.

    Any suggestions on websites that explains State Machine from the beginning are appreciated.

  • Don't know if there is a website that explain it, probably is. But basically a state machine is just a "Flag/Trigger" machine with behaviours, which you can make with Booleans.

    Example:

    Object: A Worker

    Int Hungry = 100

    Int Tired = 100

    And what else you need.

    Behaviours:

      Behaviour_Go_Eat = false Behaviour_Go_Sleep = false Behaviour_Go_Work = false Behaviour_Working = false Behaviour_Defend = false Behaviour_Guard = false Behaviour_Flee = false

    Each behaviour is just true or false and control what behaviour the unit should do or are doing.

    For each of them you make a function that you call whenever one of them are true.

    You can do that by making a behaviour machine:

    For each Worker

      If Behavior_Go_eat = true then Function("Go_Eat", Worker.UID) If Behavior_Go_Sleep = true then Function("Go_Sleep", Worker.UID) and so on.

    To trigger the behaviours you just use the variables for the worker.

    So could be that every 4 second you subtract 1 from Hungry.

    And then you make events that activate the behaviours based on certain conditions:

    If Worker.Hungry < than 30 Set Behaviour_Go_Eat = True

    If Enemy is within 200 pixel of worker Set Behaviour_Flee = True

    Then you ofc have to make it so if Worker is working and suddenly an enemy shows up, you set behaviour_working = false and Flee to true.

    This is just a rough idea of how to do it. Then you can expand it, with priority system, so Eat will make the worker stop working, or a worker prefer to gather wood over mining and so forth.

  • Hi again Guys! <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile" />

    With a mix of your help, the internet and some friends I managed to make something close to a FSM.

    You guys pointed me to the right direction to find more info and search for the right things. THANKS! <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile" /> <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile" />

    Also the FOR EACH is a big solution, and I've figured out a way to separate objects even though they are the same sprite but just different instances.

    Nimos100

    I kind of used a similar way that you describe and I have used functions.

    Here is what I've managed to accomplish so far: lindsjael.se/soulgames/BrainOfAnts_v0.4

    I have divided the way construct should check the states of my "Ants" and what to do if they should transition to another state.

    What I did was to make a function I named "FSM"

    That function gets called every tick

    Inside FSM I have a condition-CHECK that with the help of a For Each, checks different conditions and if one are met, sets a "state" for the Ant.

    for example: If Ant has a distance lower than 10 pixels to Leaf - the state "goHome" will be set.

    next inside FSM

    I have another For Each that checks the "states" of all ants and then call a Function.

    ...like If Ant are "goHome" then call Function (goHome)

    This way I have kind of a CHECK - DO combination.

    First there is the CHECK for conditions, and after that comes the DO.

    A downside are that there will be alot of events used pretty fast but on the plus-side are a code that is reasonable easy to manage, understand and modify.

    When a function for a "state" gets called I pass along a parameter... and that is the Ant.UID... This way the function knows WHICH ant to be affected.

    However in the function I always need a "Pick by comparison" -event. ...so the amount of events get pretty large after a while.

    But it works so far. I will work more on this, try different approaches and to see if I can make the events even more cleanly.

    Right now I'm trying to figure out how I could make a "Stack-based" FSM.

    A way to store a state the Ant were in before the active state. So it can go back to that state just by popping the topmost state on the stack.

    like... if the ant are "goHome" but then the ant-eater comes near and the ant need to "runAway"

    when the ant-eater are not a threat no more, the ant needs to get back to the last state it was doing "goHome"

    If I can do this, I don't have to have a condition check for that - the ant could just pop (remove) the active state on the stack, and go back to the "goHome"

    I'm gonna use an Array for this - but I haven't figured out exactly yet where I should put it

    It's easy if you just have only one instance of an object to work with. But when I have a For Each and lots of instances there are a bit more tricky <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile" />

    In my example game there are still some bugs. It's because I ran out of events. I'm at exactly 100 events <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile" /> So I could not fix the last bugs.

    But it works most of the time, and if it is bugging just reload the site.

    I will continue to work on this now. If you like I can post some links that has been useful to me to understand the FSM mechanics.

    However almost ALL refer to the C programming language and working with classes. It's a bit confusing for a non-programmer like me, even though I've made some C# programming in the past. But I'm far away from any programmer <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile" /> I'm just creative.

    If anyone are interested in the CAPX i will upload that one too.

  • This version is better :

    lindsjael.se/soulgames/BrainOfAnts_v0.6

    That version has a stack based FSM and I fixed several bugs. <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile" />

    -

    I managed to figure out how to make a stack with the states and it really helps. The transitions gets much better.

    There's just 1 bug left, it's when the ant that GROWS gets destroyed too soon after it has finished growing. Some variables that should be reset at the moment the ant finishes grow, DONT get time to reset, for some reason...

    What I have found out so far - most of the mechanics works now.

    At least the normal states: FindLeaf, GoHome and RunAway works great...

    ...It didn't bug with over 200 instances of ants, <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile" /> <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile" /> coool.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)