Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
gdevelop5:tutorials:finite_state_machine [2020/06/12 16:05]
paulera [What is a state machine?] Some rewording for shorter phrases and paragraphs, as well as rephrased examples for player in 3rd person.
gdevelop5:tutorials:finite_state_machine [2021/11/22 23:06] (current)
Line 13: Line 13:
  
 ===== Getting started ===== ===== Getting started =====
-So let's get started by downloading the assets from the “[[gdevelop5:tutorials:platform-game:start|]]” tutorial. Create a player as a “platformer object” and some platforms (“platform” behavior) to walk and jump on as described in the above-mentioned tutorial.+So let's get started by downloading the assets from the “[[gdevelop5:tutorials:platformer:start|]]” tutorial. Create a player as a “platformer object” and some platforms (“platform” behavior) to walk and jump on as described in the above-mentioned tutorial.
 {{ :gdevelop5:tutorials:screenshot_scenes.png?800 |}} {{ :gdevelop5:tutorials:screenshot_scenes.png?800 |}}
 Open the properties of the player object and create the animations “idle”, "walking","jumping" and "falling". Then create a string variable on the player object and call it “direction” with the value “right”. Open the properties of the player object and create the animations “idle”, "walking","jumping" and "falling". Then create a string variable on the player object and call it “direction” with the value “right”.
 {{ :gdevelop5:tutorials:player_animations.png?600 |}} {{ :gdevelop5:tutorials:player_animations.png?600 |}}
-Switch to the event editor (“main” scene) and create these external events “playerstateinit”, “playerstatefalling”, “playerstateidle”, “playerstatewalking”, “playerstatejumping”. In the events of your main scene create a new event group from the drop down menu on the right-hand side. Name it “Player logic”. Now add a "At the beginning of the scene" condition. Add a sub event to the condition and link in the external event sheet “playerstateinit” via the “add”/”other” button. Do the same for all the other player states.+Switch to the event editor (“main” scene) and create these external events “playerstateinit”, “playerstatefalling”, “playerstateidle”, “playerstatewalking”, “playerstatejumping”. In the events of your main scene create a new event group from the drop-down menu on the right-hand side. Name it “Player logic”. Now add a "At the beginning of the scene" condition. Add a sub-event to the condition and link in the external event sheet “playerstateinit” via the “add”/”other” button. Do the same for all the other player states.
  
-<note tip>Instead of an animation we could also have chosen a dedicated "state" variable to control the players state. This would have given you a little more flexibility but the animation approach has the benefit that it allows you to bundle the logic with the corresponding animation which saves you some space in the event sheet and makes the logic look a little cleaner. Furthermore, you have access to the frames of the current animation/state and can trigger some logic at a certain frame inside the animation.</note>+<note tip>Instead of the animationwe could have created here a dedicated [[http://wiki.compilgames.net/doku.php/gdevelop5/all-features/variables#declare_variables_using_the_editors|Scene Variable]] "state" to control the player state. This is more advanced but allows for better flexibility. However, the approach presented here has the benefit of bundling the logic with the corresponding animation, allowing for clarity and simplicity in the event sheet. Furthermore, you have access to the frames of the current animation/state and can trigger some logic at a certain frame inside the animation.</note>
  
 {{ :gdevelop5:tutorials:main_events_state-linking.png?600 |}} {{ :gdevelop5:tutorials:main_events_state-linking.png?600 |}}
 ===== Debugging information ===== ===== Debugging information =====
-In order to determine in which state the player currently is we will add a text object and call it “debug_state”. Add it to the main scene and create the following logic at the end of main scenes logic list in order to display the current player state right above it'head for debugging purposesSo whenever something doesn't function the way we want we will always know in which state we have to look for the error.+In order to know in which state the player currently is during gameplay, create a text object and call it “debug_state”. Add it to the main scene and create the actions as below (with an empty condition), in order to display the current player state right above its head. Whenever something doesn't function the way it should, we will know which state we have to investigate to find the error.
  
 {{ :gdevelop5:tutorials:main_events_debug.png?600 |}} {{ :gdevelop5:tutorials:main_events_debug.png?600 |}}