330 likes | 501 Views
ProjectFUN Editor State Machines. R. J. Smith ProjectFUN Computer Science Technology Academy Online. What is a State Machine?. A state machine is a software structure used to keep track of the current status or “state” of an object
E N D
ProjectFUN EditorState Machines R. J. Smith ProjectFUN Computer Science Technology Academy Online
What is a State Machine? • A state machine is a software structure used to keep track of the current status or “state” of an object • State machines make it easier to organize sequential actions and states, rather than relying on dozens of variables • A game character might have “states” such as standing, running, jumping, falling, dead, etc.
Example State Machine • State Machines can be used to keep track of the status of a program, such as a networking application.
ProjectFUN Editor State Machines • Each state is represented by a yellow circle • States are connected by arrows • The arrows between states are called “edges” • The initial state for an object is circled in red • Like Object Functions, State Machines are behaviors that can be used by Sprites, Texts, Levels, Game, Maps, etc.
Uses for PFE State Machines • There are many uses for State Machines in ProjectFUN Editor: • Setting animations at proper times • Resetting sprites and levels when the player has reached the losing condition • Setting a special state for a sprite, such as an “invincibility” or “dead” state
Uses for PFE State Machines • If you find yourself writing object functions with global variables and code like this:if( myHeroStanding == true ) // codeelse if( myHeroRunning == true ) // codeelse if( myHeroJumping == true ) // codeelse if….. • It’s a good candidate to be turned into a State Machine!
Example State Machine Usage • Think about your average 2-on-2 fighting game like Street Fighter II • The game logic might be like this: • If the player pressed the fireball button, throw a fireball… • IF the player isn’t in the air… • IF the player isn’t getting hit… • IF the player isn’t blocking… • IF the player isn’t currently executing another fighting move… • IF the player has enough energy…. • A State Machine would be a much easier solution.
A Simple Fighting Game SM • Assuming a player can only throw a fireball when nothing else is happening…
Scenarios from Fighting Game SM • You can throw a punch or kick from jumping, but… • … you can’t jump if you are already punching or kicking • If you fall, you can get up to standing or you may be dizzy • If you are dizzy, the opponent can hit you again (you can’t block)… • … but if your opponent doesn’t hit you, you may go back to standing directly from dizzy. • If you are hit, you may fall or keep standing • When you fall, you might be dead/defeated • You can double-jump (note the edge goingfrom the jump state back to itself!)
But It’s Even More Complex… • You can get hit while throwing your own punches, kicks, fireballs, or jumps, of course!
States in ProjectFUN Editor • States in ProjectFUN Editor have two code areas: • Initial Actions: • The code here is executed ONCE, upon entering the state. • You might change an animation, for example. • Actions: • The code here is executed once EVERY GAME LOOP, until the state is exited. • You might write code to guide an enemy character’s AI, for example
Creating a State Machine • Click the Create State button to create a state.
Creating an Edge • Click the Create Edge button to begin creating an edge.
Creating an Edge • When you click the “Create Edge” button, the cursor changes and reads “From” when you point to a state. • Left-Click the “From” state once • Point to the second state, and the cursor changes and reads “To” • Left-Click the “To” state once • The Edge is created!
Opening the Edge • To open an Edge, use the Select Arrow to double-click the arrow head of the Edge. • You may need to move around the states if they are in the way of the arrowhead.
Coding the Edges • An Edge in ProjectFUN actually calls a function that must return a BOOLEAN value • This means two things: • You must use “return” to return a value • That value must be “true” or “false” • By default, an Edge simply returns “true”
States With Multiple Exit Edges • Sometimes a state will have multiple exits • Example: In Super Mario Bros., from the standing state, you can jump or walk • But… what happens if BOTH of the conditions for following the Edges are true?? • What if you press “jump” and “walk” at *exactly* the same time? • Which Edge will “win”?
Edge Priority • You can use an Edge Priority to determine which “wins” if more than one valid Edge is possible. • Higher Priority Edges will be taken first. • Equal Priority Edges are taken in the order they were created.
Edge Priority • If the player gets an invincibility powerup and hits an enemy at exactly the same time, we give priority to the powerup.
Return Edges • Don’t forget to create return edges from your special status states.
Example Return Edge • You could also write code to let the player be invincible for, say, 5 seconds. • At 60 FPS, you would need to create a counter variable, increment it once per game loop, and return true when it exceeds 300.
Another Return Edge Example • Assumes you have a variable named “InvincibleCounter” you have been incrementing.
States with Edges to Themselves • A State can have a valid Edge that returns to the current state. • It can be very useful! • Returning to the current state from an Edge re-executes the Initial Actions of the state.
States with Edges to Themselves • To create such an Edge, click the Create Edge icon and click the same state twice for the From/To clicks.