How do I control players/enemies based on their state?

0 favourites
  • 2 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.
  • What's a good way to manage what players and enemies can do based on their state?

    Context - In my platformer, the 'player' character does many standard platformer actions - shoots, runs, jumps, crouches, aims, reloads, kicks, etc. I've been using a "state" instance variable to determine what the player can do (and what can be done to the player) based on their current state. E.g., when state = "good", the player can move, jump, and aim. If state=aiming, the player can fire. If state=good, or aiming, or reloading, or throwing, or kicking, the player can be hit by an enemy. I set the state variable when something happens - ie., the player fires, or an enemy is hit.

    At first this was manageable, but as I add capability to the player/enemies (and features to the game) my logic is starting to get unruly. My conditions are getting complicated, with lots of or's and inverted conditions, and whenever I want to make a change or add a new state in the code, something almost always breaks. It's brittle spaghetti code.

    This article https://gameprogrammingpatterns.com/state.html does a great job explaining the problem, and offers a fairly sophisticated set of solutions. Unfortunately a) it's beyond my ability, and b) I don’t see how this can be implemented with C3.

    Can anyone recommend an approach that works well with C3?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You need to spend time creating a system that allows you to add to the code without it becoming messy.

    Maybe make a list of effects all set to true. This is what your character can do in a normal status.

    If you go to jump it first runs a function check - can you jump? - jump came back true so jump else ignore command

    While crouch = true set jump false and MS 0.5

    Now if you jump the function will ignore cause it will fail the check.

    I’m the end it should just be -

    Player is crouching

    Player is sick

    Player is slowed

    Function

    Player can’t jump

    Player can’t run

    MS 25%

    Player can’t stand

    <everything else still true>

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