BinaryState behavior, for input tracking, state tracking, etc

0 favourites
  • 1 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.
  • Hey all!

    I have a behavior I made to solve a really simple problem. It tracks the state(s) of a binary toggle, flip, switch, or whatever you want to call it with extra benefits!

    The behavior automatically tracks last state, the time at which a state change occurs, the current state, and has conditions for entering/exiting (similar to input On key pressed, or On key Released).

    In addition, you can use it to track previous entry/exit times for states.

    The behavior uses an array of states, and accesses them by ID, but only updates the number you specify, keeping it lean and simple. The Id system allows you to determine what each state is. For example Forward = 0, Right = 1, Back = 3, and so on. States are also stored as numbers, so technically, you can have a state existing in a series of substates.

    I use it for character input (player or ai), and for tracking simple states, such as IsOnGround. In this case, I would have a variable on a data object called IsOnGround and set it to be the desired ID, then use it like so:

    Is entity overlapping water? -> Entity Set State(IsOnGround) to 1

    later on in the project:

    //True during the first frame a state is switched on from off

    entity enters state (IsOnGround) -> Do things

    //True during the first frame a state is switched off from on

    entity exits state (IsOnGround) -> Do Things

    You can combine them in nice ways like this (in this case to forgive/allow a player to jump when they pressed the input a fraction of a second too early):

    entity state (jump) was entered <= 0.1 seconds ago

    entity enters state (IsOnGround)

    ---> //call the jump behavior

    Would anyone else like access to this? I was hoping for feedback on naming conventions and ease of use, as well as use cases people find for it.

    ----

    Current ACES:

    Actions:

    //Given an id, sets a state to the value provided. Both are numbers

    -Set State(Id, State)

    //Called after all changes to states are made. This is used to compute new enter/exit times for any states that have changed. The time is calculated based on the instances dt

    -Confirm States

    //Set the total number of states to process (hard coded limit atm)

    -Set State Count

    //Resets all states and times to default/off state.

    -ResetAll

    -Reset(id)

    //Set the behavior to auto rollover input and advance the internal clock

    -Enable

    -Disable

    //Manually advance teh internal clock and roll over states from current to last

    -Advance frame (dt)

    Expressions:

    Returns the current value for a state, given the id

    -State(id)

    //Same as above, but for the last frame

    -LastState(id)

    //Returns 1 if the state at id was switched on during this frame

    -Enter(id)

    //Returns 1 if the state at id was switched off this frame

    -Exit(id)

    //Returns the time for a state entering or exiting or the previous one.

    -EnterTime(id)

    -ExitTime(id)

    -LastEnterTime(id)

    -LastExitTime(id)

    //Returns the number of states being processed

    -Count

    //Returns the Objects current time in seconds

    -Clock

    Conditions:

    //Returns true if state(id) is true,

    -State (id)

    -Last State (id)

    -Enters (id)

    -Exits (id)

    -Compare exit time (id)

    -Compare Last exit time (id)

    -Compare enter time (id)

    -Compare Last Enter Time (id)

    -Compare Clock

    -IsEnabled

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)