Adding Object States

Adding Object States Without Restrictions

Once you have a PRState derived object, right-click your object's Create event in the object editor and click "Inherit Event". At some point in this event code, perhaps towards the bottom, begin adding states to your object using the PRAddState() method. For example, a typical hero character may have the following states:

PRAddState("Idle", IdleMethod)
PRAddState("Walk", WalkMethod)
PRAddState("Run", RunMethod)
PRAddState("Jump", JumpMethod)
PRAddState("Shoot", ShootMethod)

When you add a state, you specify a method or function that will be responsible for implementing that state. For example, the Run state added above has a method called RunMethod assigned to it that will handle the Run state.

Adding Object States with Restrictions

When adding a state to an object, you have the option of specifying a list of "valid from states." These are states which the current state is allowed to legally transition from. This restriction is accomplished by including an array of state names in the 3rd parameter to the call to PRAddState() as in the following example:

PRAddState("PowerSlide", PowerSlideHandler, ["Walk", "Run"])

The above command adds a state called "PowerSlide" to the object and specifies that the "Walk" and "Run" states are the only states that can switch to the "PowerSlide" state. This makes sense as you might want to have the main character of a game switch into a power slide move only if the character is either walking or running but not, for example, if the character is jumping. Thus, the following command will fail if the current object state is "Jump" because "Jump" isn't one of the "valid from states" defined for the "PowerSlide" state:

// This command will fail if the current object state is "Jump".
// The state will not change.
PRSetState("PowerSlide")

This is a check restriction that is automatically imposed by PRState when calling PRSetState() and PRSetPreviousState(). Attempting to set a state that is illegal as per the "valid from states" list will result in failure and the current state will remain active.


© 2021 Prismatic Realms, Inc. Contact

Green Hosting Badge