1 / 33

ProjectFUN Editor State Machines

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

arnav
Download Presentation

ProjectFUN Editor State Machines

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ProjectFUN EditorState Machines R. J. Smith ProjectFUN Computer Science Technology Academy Online

  2. 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.

  3. Example State Machine • State Machines can be used to keep track of the status of a program, such as a networking application.

  4. 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.

  5. ProjectFUN Editor State Machines

  6. 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

  7. 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!

  8. 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.

  9. A Simple Fighting Game SM • Assuming a player can only throw a fireball when nothing else is happening…

  10. Example Fighting Game SM

  11. 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!)

  12. But It’s Even More Complex… • You can get hit while throwing your own punches, kicks, fireballs, or jumps, of course!

  13. 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

  14. Initial Actions

  15. Actions

  16. Creating a State Machine • Click the Create State button to create a state.

  17. New State

  18. Creating an Edge • Click the Create Edge button to begin creating an edge.

  19. 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!

  20. Using the Select Arrow

  21. Renaming the State

  22. 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.

  23. 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”

  24. Edge Returns a True/False Value

  25. Writing Your Own Code

  26. 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”?

  27. 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.

  28. Edge Priority • If the player gets an invincibility powerup and hits an enemy at exactly the same time, we give priority to the powerup.

  29. Return Edges • Don’t forget to create return edges from your special status states.

  30. 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.

  31. Another Return Edge Example • Assumes you have a variable named “InvincibleCounter” you have been incrementing.

  32. 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.

  33. 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.

More Related