Basics: States

States are special functions and loops that are used to make AIs behave in a certain way according to its current "state" (mood, level of experience, the time of day, ...).

Table of Contents

Introduction to States

AIs in ShiVa can be programmed in a way commonly referred to by computer scientists as Finite State Machines (FSM). For instance, a monster AIModel could have 2 states: 'roam' and 'attack', and switch between these 2 states to have a dedicated animation, movement speed, etc. The machine can only be in one state at a time; the state it is in at any given time is called the current state. It can change from one state to another when initiated by a triggering event or condition; this is called a transition. A particular FSM is defined by a list of its states, and the triggering condition for each transition.

States are organized as packs of 3 functions: onEnter, onLoop, and onLeave.

State Transitions

To change the current state of the AIModel, use this.theStateName ( ) or this.sendStateChange ( "theStateName" ). To change states with a delay, use this.postStateChange ( nDelay, "theStateName" ) with the delay in seconds. An AI can be in only one state at a given time.

When the AIModel state changes, the onLeave script of the current state is executed, then the onEnter script of the new stated. After that - and given that the AI continues to be active - the onLoop script of the new/now current state is executed each frame. Place your new movement/interaction/... code into onLoop.