State Machine Tutorial: Latest Apricot Builds:FINISHED!!

Hi this is a short tut showing how to get more out of the state machine using the state actuator to invert states.
This will teach you how to make switches that can toggle on and off.

You can get the the latest apricot builds form here.
http://www.zoo-logique.org/3D.Blender/index.php3?zoo=com
–>Builds are very recent but come without the blender player.
http://www.graphicall.org/builds/index.php
–> Not as recent but comes with the blenderplayer.

1.Make simple scene with a ground plane and 3 cubes… like this:
http://blendershare.servebeer.com/newblenderforum/gallery/56_29_08_08_11_15_27.jpg

2.To make things easier… give each object a material with unique colours. You can turn on GLSL shading in the game menu if you wish.

Note:
You have to do this if you want see the GLSL shaders but you can use the statemachine without shaders as well.
I will be doing that in this step. Make sure you change the draw type to ‘textured’ to see the GLSL pixel shaders in the viewport.

I usually lower or turn off the specular setting at first… I also increase the emit level to begin with.
It lets me see quickly what’s going on in the scene.
http://blendershare.servebeer.com/newblenderforum/gallery/56_29_08_08_11_43_38.jpg

3.Next scale up the purple and green cubes.Make sure you apply the scale by pressing CTRL-A and selecting ‘Scale and Rotation to ObData’.
Always
do this if you change the scale of an object and BEFORE you run the game engine. If you don’t your bounds will be the wrong size.
http://blendershare.servebeer.com/newblenderforum/gallery/56_30_08_08_1_46_42.jpg

4.Next make the red cube a dynamic rigid body… by clicking dynamic and rigid body under the logic panel (the button that looks like pac man).
Make sure you set the bounds to ‘Convex Hull Polytope’…

You can use box bounds as well but it is less accurate but faster
It depends on the speed you want your game to run at. Convex Hull Polytope is slower but more accurate collisions.
Rename the red cube ‘player1’.
http://blendershare.servebeer.com/newblenderforum/gallery/56_30_08_08_1_54_36.jpg

5.Select the Purple and Green boxy pillar things and rename the purple pillar to ‘pickup_01’.
Rename the green boxy pillar to '‘pickup_02’.
In the game logic panel… make both of the pickups actors and ghosts, by clicking on the actor and ghost buttons.

Why make them ghosts? I did this so player one can easily move through the pickups.
You could make them solid as well… but for this demo it’s just easier to make them ghosts.

You don’t have to worry about setting bounds if the object is not dynamic… it usually defaults to the shape of the object.

If do have problems… go to the game menu… and choose turn on ’ Show Physics Visualisation’… then you can look at the objects to see if they have bounds the are the correct shape and size.
http://blendershare.servebeer.com/newblenderforum/gallery/56_30_08_08_2_09_18.jpg

6.a screen with the physics visualisation on… As you can see it appears that the bounds are all fitting snuggly around our objects.
http://blendershare.servebeer.com/newblenderforum/gallery/56_30_08_08_2_18_49.jpg

7.Next select the red cube/player1 object… and go to the logic panel.
I’m going to make him drive around using the arrow keys.
So add in a ‘keyboard sensor’… then add in an ‘and controller’ then add in a ‘motion actuator’… link them all together.

For the sake of the tut I’m just going to be moving the player on the global axis.
I want the player to move off to the right and hit the green pickup.
So… in the keyboard sensor… put in the up arrow key… in the motion actuator… add in 10 under the X input and uncheck local.
How do I know that putting in 10 will make my object move off to the right… like I want it to?

Well if you look at the objects axis… you will see the X axis going off to the right. If you wanted it to move left you would put in a minus number.
Note: This is just based off of my camera view so moving your camera around will change what is left and right…
So in a real game I would move the object on it’s local axis… or maybe not…
Depends what kinda affect you are going for.

Also because the object is a dynamic rigid body… you have to move it with Force or linV.
Moving it with Dloc seriously messes with the collision detection.
http://blendershare.servebeer.com/newblenderforum/gallery/56_30_08_08_2_40_16.jpg

8.Now if you run the game engine… and press the up arrow… which is going to move the player forward… off to the right…
Yes I know I chose the X axis as my front…
The player will slide along and through the green pick up.
http://blendershare.servebeer.com/newblenderforum/gallery/56_30_08_08_2_52_53.jpg

9.Right now would be a good time to make a reset button for the game. So select something that won’t change much… like the ground plane…
Then add in a ‘keyboard sensor’… and an ‘and actuator’ and a ‘scene actuator’.
So set the Keyboard sensor to use the ‘r’ key
Set the Scene actuator to restart.
Hook them all together.

Now when you run the game… press ‘r’ at any point to restart the simulation.
This saves you hours of time when you are testing game mechanics.
The ground plane doesn’t have to be an actor unless you want it detected by a ray (I think)
http://blendershare.servebeer.com/newblenderforum/gallery/56_30_08_08_3_01_54.jpg

10.So now I’m going to make the cube move backwards and forwards and left and right… taking the postive X axis as my front facing direction.
You can choose any direction for the front in your games.

So add in the Keyboard sensors and actuators etc you need to do this.
Look in the pic below to see how I’ve done it.
I’ve also named all the sensors and actuators.
It’s good to get into the habit of doing this… but it is slow and boring.
It does help you later on when you start messing with things.

I also rotated the view so the camera is slightly more behind the player object so the controls make more sense.
I also added in a small arrow like object… made by scaling one face on the side of a cube smaller.
I then made it an actor and a ghost and parented to player1.

I did this so I can see where the front is supposed to be.
So as you can see in the pic… now we have the player driving around (with the arrow keys) moving through the two pickups.

http://blendershare.servebeer.com/newblenderforum/gallery/56_30_08_08_3_26_05.jpg

11.FIIIIIIIIINALY… now we can start playing with the state machine. Okay now we need to stop and think about this.
I want the following to happen… when the player drives into either of the pickups…
Something must happen to the player… maybe he jumps… maybe a filter turns on?
BUT I don’t want it to go on forever… I want it to turn off after some time.
I also don’t want it to turn on and on and on… every time the player moves through it, if it is already on.
I want it to turn on… do it’s thing… then turn off.

But first a few things about states:
I like to think of states as groups of controllers with sensors and actuators attached and that the sensors and actuators can be shared amongst these groups of controllers.

If you look at the picture below you will see that all the controllers are under a bar with little blue squares.
Each one of these squares is a state. There are 30 of them for each object.

If there is a dark dot in one of the squares… it means the state has some controllers, sensors and actuators attached. If it just an empty light blue square this means the state is empty.

If the square has a dot and is shaded dark blue(see image just below)… this means that this state is currently on.
This means that when you run the game, any sensors and actuators connected to the controllers of this state… will function.

If the square has a dot and is light blue(see the second pic just below for an example)… this means the state is currently off.
This also means that when you run the game any sensors and actuators connected to the controllers of this state will not function… until you turn this state ON!

How do you turn states on and off? You use the state actuator.

http://blendershare.servebeer.com/newblenderforum/gallery/56_30_08_08_3_50_51.jpg

so if you look in the image below you should see be able to few things:

  1. It has two states.
  2. state 1 is on.
  3. state 2 is off.
  4. You can see the sensors and actuators for state 2.
    http://blendershare.servebeer.com/newblenderforum/gallery/56_30_08_08_4_24_20.jpg

http://blenderartists.org/forum/%3Cimg%20src=%22http://blendershare.servebeer.com/newblenderforum/gallery/56_29_08_08_11_43_38.jpg%22%20/%3E

POST002: more about states
You will notice in the last image in the post above that you can see the sensors and actuators belonging to state 2… are appearing unlinked

-this is because you can actually share them between states ( ouch, yes that can get confusing)
THIS is really amazing because it lets you cut down on unnecessary sensors.
You can also use it to make cool game play mechanics etc.

You can make the view less confusing by hitting the State filter button for the sensors and actuators.

In the image below you will see I’ve selected state 1:
1.Notice the 1 appearing next to all the and controllers.
2.Notice that state 1 is on.
3.Notice that state 2 is off.
4. What does this mean?
5.When you run the game engine only state 1 will work.
http://blendershare.servebeer.com/newblenderforum/gallery/56_30_08_08_4_54_29.jpg

If you look in the image below you will see I’ve selected state 2:
1.Notice the 2 next to all the and controllers.
2.Notice that state 1 is off.
3.Notice that state 2 is on.
4. What does this mean?
5.When you run the game engine only state 2 will work.http://blendershare.servebeer.com/newblenderforum/gallery/56_30_08_08_4_58_20.jpg

Here is an example of both states on at the same time (you can turn any number of states on by holding down shift and selecting the states):
1.Notice the 1 and the 2 next to the controllers.
2.Notice that state 1 is on.
3.Notice that state 2 is on.
4. What does this mean?
5.When you run the game engine both state 1 and state 2 will work.
http://blendershare.servebeer.com/newblenderforum/gallery/56_30_08_08_5_09_40.jpg

If the states you’ve selected to be on or off etc are not working as they should when you run the game engine… click on the State button
Choose “Store init State”.
This will store the on and off values of your currently selected to states to run in this format when you start the game engine.
So if you then change the on and off values of the states… and then run the game engine, it will still use the previously stored states values.

In the image below you will see I have state 1 and 2 selected to be on.
I’ve stored the state values by clicking the ‘Store init state’ button.
So when I run the engine… both these states will be start out being on even if I change the values of the states.
eg. I could change state 1:to off and state 2: to on… in the interface but when I run the game, it will still run state 1 and 2 as being on.

If I want to change how the states start off… I select them again in the order I want… and hit the state button… and select ‘Store init state’.

Explore what the other options do under the state button.
http://blendershare.servebeer.com/newblenderforum/gallery/56_30_08_08_5_36_28.jpg

So by now your probably pretty confused…and thinking what’s the whole point of being able to turn states on and off?
I will show you in my next post.

POST003: Building the pickups using states
Okay a quick refresh of the goals:
1.I want to make two pickups.
2.They must both turn on and affect the red player1.
3.They must affect red player 1… when player1 moves through both of the pickups.
4. One of the pickups, must not keep getting reset… every time the player moves through it.
5.It must have a delay… so If the player moves through the pickup when the delay is counting down the pickup must stay off until it has completed it’s effect.
6. The other pickup can act like a switch that you can keep flipping to reset the effect.

lets get on with it…
click here toDLD the blend file with the player and the pickups… so you can follow along.
Thanks to blenderfile forge for the hosting!

1.Open the file and delete state two from the red player object.Do this by clicking on the state two and remove the sensors,controllers and actuators.
Your logic bricks should now look like this, with only one state… notice state two is now gone.
http://blendershare.servebeer.com/newblenderforum/gallery/56_01_09_08_3_37_50.jpg

2.Select the green pickup02 AND red player1.
Add in a collision sensors only for green pickup02.
http://blendershare.servebeer.com/newblenderforum/gallery/56_01_09_08_3_44_22.jpg

3.Now add in a ‘and’ controller to red Players1, state 1… and link the collision sensor from green pickup_02 to the new controller.
Then add in a state actuator for red Player1 and link this to the new and controller like in the pic below.
http://blendershare.servebeer.com/newblenderforum/gallery/56_01_09_08_3_52_02.jpg

4.Select red player1… and give it the property, ‘player1’.
Wait, how do I tell if I have only player1 selected?.. Blender is showing me all the connected logic bricks. Well you can play with the filter buttons.

You will see I have only the selection filter button on for the sensors… so I can see I have only red player 1 selected.
If you look at the controller column you will still see the controller that is connected to the green pickup01, sitting there, with no connection.
This is because I have the selection and link filter buttons on etc. Don’t worry it is still connected.

It’s really important to understand how to use these filters so you can tell what’s connected to what. It makes life a lot easier when you start making complicated logic bricks layouts.
http://blendershare.servebeer.com/newblenderforum/gallery/56_01_09_08_4_00_15.jpg

5.Deselect everything…
First select green pickup_02… then shift select red player 1.
I do it in this order so I can see the property for red player1.

Copy and paste
red players1’s property name, into the collision sensor of pickup_02.
This means that green pickups’s_01… collision sensor will fire every time red player1 gets detected.
It will also run the state actuator on redplayer1.
http://blendershare.servebeer.com/newblenderforum/gallery/56_01_09_08_4_19_29.jpg

6.Turn the state filter back on for the sensor column.
Under the controller column… look for player1
Under player1… add in a new state, at position 2… ie. next to the existing state, by adding in 2 new ‘and’ controllers.

Then add in an always sensor, delay sensor, motion actuator and a state actuator.
Hook the always sensor and the motion actuator to the first ‘and’ controller.
Then hook up the delay sensor and state actuator to the second ‘and’ controller.
http://blendershare.servebeer.com/newblenderforum/gallery/56_01_09_08_4_36_15.jpghttp://blendershare.servebeer.com/newblenderforum/gallery/56_01_09_08_4_47_01.jpg

7.In the motion actuator… select the Z force… uncheck use local and enter the value 10.
Under the delay sensor add in a delay of 300 frames

This makes the actuator fire the state actuator after 300 frames have past.
Leave the Duration to 0 and REP button off.

So basically when this state is on… it will do the following…
It will move red player1 with force on the global Z axis.
It will keep doing this until the 300 frames have passed… then the state actuator will do it’s thing.
http://blendershare.servebeer.com/newblenderforum/gallery/56_01_09_08_5_05_44.jpg

8.Now to make the states turn on and off.
Go back to state one… Under the state actuator… select Inv. This will invert any states you choose.
So select state 2.

So when red player1 collides with the green pickup_02… the state actuator will invert state 2 from off to on… making the player jump in the air.
Now I still need to make sure state 2 turns off after the delay.
http://blendershare.servebeer.com/newblenderforum/gallery/56_01_09_08_5_16_53.jpg

9.Now go back to state 2.
Go
to the state actuator… and select invert… then set it to use state 2.
So after 300 frames the State 2 will be inverted from on to off.
http://blendershare.servebeer.com/newblenderforum/gallery/56_01_09_08_5_30_24.jpg

10.Another way to do it, would be to use subtract in the state actuator.
In the State Actuator Select state 2… and select SUB… sub will turn off the state.

After some testing, I lowered the delay to 60 frames.
I also increased the z-force on the motion actuator to 15.
I also turned on some shadows so you can see more easily in the screenshot that when the player1 moves through green pickup_02… he get pushed into the air.

Also note that if you can manage to move the red player1 through the green pickup_02 ,twice in quick succession… you will get thrown higher.

This means that every time you move on to the sensor it turns on.
Move off of it… then back on again, and it will fire straight away. and move you higher if you are already flying through the air.

I will show you how to avoid this behaviour when I make the purple pickup_01.

a link to the file with everything made up till this point.

http://blendershare.servebeer.com/newblenderforum/gallery/56_03_09_08_3_29_49.jpg
fff

Hey MAAAAN, Nice post here. this is really good… Im going to have to read it like 3 more times though!!!

SweetNESS!!
Im getting the idea…

This reminds me of a game I once played called Carnage Heart… It was a graphical logic brick how-you-doin except with robots.

Im totally awesome at that, I think with these states we can really push the creation of our enemies and even players to the next limit.

Imagine pre-programming like an attack state setup and an idle state setup for creatures in a world then you wouldnt have ALL the logic briks at once…

Its getting clearer and clearer as I read it.

Thanks for the TUT
Terry

@Nickadimos… hey thanks… yeah the new state machine is the bomb…it’s sooooo insanely powerfull when you start to realise how you can use them… soon I’m going to want more than 30 states ;-).

It’s a very exciting time for the BGE… I think we will (and have allready started) to see some great games being made. Props to all the devs.

If I have time later today I will work on finishing this.

POST004: Building the purple pickup-01 using states
Note:
I make a mistake in one of the steps but I do correct it.

First a bit of a recap of what I’ve made so far. We have the red player1 object.
It drives through the green pickup_02 and gets pushed into the air.
After a delay of 60 frames the effect ends… but
if you drive through the green pick up again and again quickly, the player gets pushed up and up… higher and higher.

So now I’m going to make the purple pickup_01.
It will have an effect like green pickup_02… but you won’t be able to keep turning it on and on
It will stay on for a time, do it’s thing… while it’s doing it’s thing you won’t be able to reset it.
It will then end the effect and return to its ready state.
So lets get on with it.

click here to get the file up to this point

1.Select purple pickup_01… then shift select red player1.
Under the controller column for red player1 select state 3 and add in an ‘and’ controller.
Under the sensor column for purple pickup_01… add in a collision sensor.
Copy and paste the player1 property into the collision sensor (this is why I selected the purple one first so I could see the property to copy and paste it).

Add in a 2d filter… set to erosion.
Link the Collision sensor and the 2d filter to the ‘and’ controller belonging to state 3.

Now to get this to work we need to turn off state 3’s collision sensing after the first hit.
So just add in a state actuator.
Link it up to the same ‘and’ controller.

In the state actuator… select states 3 and 4 (state 4 is empty right now but I will add to it next).Select INV for invert.

So what will happen is this… the collision gets detected… the 2d filter gets turned on…then it inverts states… so state 3 which was on turns off… state 4 which was off turns on.

I just need to mention that if you use an motion actuator you would have to do it a bit differently because the 2d filter won’t turn off if you turn off the state it in.
A motion actuator would… so you would have to put the motion actuator in state 4 to get the same effect.
http://blendershare.servebeer.com/newblenderforum/gallery/56_04_09_08_7_32_24.jpg

2.Under the red player1 controller column.Select state 4. Add in an 'and' controller. Under the red player1 sensor column.. add in a delay sensor. Put in a delay of 300 frames.

Add in a 2d filter… choose disable filter. This should turn off the filter.
Make sure the pass number matches the pass number filter you want to turn off/affect.

Add in a state actuator.Select State 3 and 4.
Select INV for invert. You should be able to guess what effect this will have.
Connect the Delay sensor and the 2d filter actuator and the state actuator to the ‘and’ controller belonging to state 4.

So what happens is this:
This state gets turned on via the collision sensor form the previous state.
It then turns itself off after a delay of 300 frames.

Because the collision sensor is in a state that is now off it should not register any collisions… preventing the effect turning on and on etc.
http://blendershare.servebeer.com/newblenderforum/gallery/56_04_09_08_7_47_52.jpg

3.So to test the game… we need to make sure that the states that need to be on from the start are setup to be on.
States 1 and 3 must be on.
So select both of these and select the state button.

Choose store init state.
This sets these states to be on at the beginning of the game.
If I’ve done it right, then the other states will be turned on and off as the red player1 moves through the pickups.

Note that you can see the all the sensors and actuators connected to the controllers belonging to states 1 and 3.
Run
the game.
http://blendershare.servebeer.com/newblenderforum/gallery/56_04_09_08_8_04_42.jpg

4.So if you are on the ball you would’ve noticed that I’ve connected both collisions sensors to green pickup_01. DOH!
So fix that by deleting the extra collision sensor on green pickup_01.

Then add in a collision sensor onto the CORRECT purple pickup like in the pic below.
I also changed the filter to invert so I can see if it’s working.
Select state the button and select store init state.
I always do this after I mess about with the states.

Run the game.
http://blendershare.servebeer.com/newblenderforum/gallery/56_04_09_08_8_20_44.jpg

5.Now I need to be 100% sure that the collision sensor attached to purple pickup_01 doesn’t fire… even when the state connected to it is off.

Select the collision sensor belonging to purple pickup_01 and press the Lvl button.
This connects the collision sensor to the state change.

So when the state it belongs to is off it also makes the sensor off.
Remember you can share sensors between states… and between objects.

So if you were to use the same collision sensor again… say in another state and you left
the Lvl button off, you would get strange behaviour.
http://blendershare.servebeer.com/newblenderforum/gallery/56_04_09_08_8_37_22.jpg

6.So if you run the game now… you will see the screen colours invert whenever you drive into the purple pickup_01…

It doesn’t matter how many times you keep driving into it.
It will always invert the colours for a delay of 300 frames.

here’s link to the file up to this point.

http://blendershare.servebeer.com/newblenderforum/gallery/56_04_09_08_8_43_48.jpg

7.So now experiment with the new controllers.
-You can combine the new controllers to make interesting state changes.
-You can even make events happen after an ipo has played.
-You can use the actuator sensor to detect a change in an actuator and change state.
-You can chain states together.
-You can turn them on and off by using invert or subtract.
-You can even change state if nothing has been detected.

There are endless combinations of events and effects you can create using the state machine. Just try to think of state as groups or layers of controllers and it becomes a lot easier to visualise what’s going on.

You can end up with complicated logic bricks with wires all over the show… so USE the filters (sel,act,link,state) to make things easier for yourself. This is my first tut and it was quite long… so crits and corrections are welcome.

I would also recommend doing this tut which has another way of doing things.
http://iluvblender.googlepages.com/blenderprojects%3Asimplestatemachinesimula