Game Development Reference
sets a value to return. We will do that now. Add the following code to
If World.DetectsPlayers And World.GoodHealth Then
The NextState variable is declared in the parent class, BasicTransition , and is
made available to this child class because we marked it Protected in the parent.
Note that this transition class does not know explicitly what state it transitions to.
Not only can any state that wants to use this transition do so, it can point it at any
other state. The state that creates the transition will tell the transition what the
next state should be. Coding this way makes the transition reusable.
Our code for ShouldTransition has access to the world, but it makes no calls to
the Say function. Right now, our monster speaks only when it is doing some-
thing. It does not talk about the thinking process itself. But since each transition
has full access to the world, it could also speak. If your code does not work right
the first time you run it, one of the ways to see what the monster is thinking is to
have all the transitions say that they are running and whether they are valid.
Now that the transition is defined, the states that use it can load it into their
transition collections. This should happen only one time: when the state is
created. The place to do this is in the state's New() function. Scroll to the top of
HidingState.vb and click the word HidingState. VB changes the contents of the
drop-down lists at the top of the Editing pane based on where you clicked. The
left drop-down list now says HidingState, and the right one says (Declarations).
Click (Declarations). All the routines in the class are in this list, plus New and
Finalize (whether they have been added or not). Select New from the list. VB adds
the skeleton for you.
New() runs once, when an object is created, and it is the perfect place for our State
objects to load their transitions. Add code to the New() routine as follows:
Public Sub New()
Dim Txn As BasicTransition
'Create a specific transition
Txn = New SeePlayerHighHealthTxn()
'Set the next state name of that transition