Lets Create Flappy Bird! #11 High Score - Godot Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in this tutorial we will implement high scores into our game we will learn how to save data and also how to load data from a file let's get started okay let's go inside the world script and let's create a high score variable this is going to be zero as default and let's create a constant to store the path that we'll use to store our save file we're gonna call this one save file path it's gonna be a string and just like we have a res folder to store our project files we have a user folder that godot defines for us that we can use to store anything that we want to save on the player system in this case we're going to create a save file i'm going to say user and the name of the file is going to be save data dot save this user folder the location of it is going to depend on your operating system but i can show you where it is on windows just go to your file browser and in here you need to go to app data which you can go by typing percentage sign app data lower case and percentage sign and this will take you to app data roaming and in here you want to go to godot app user data and from here you need to find the prod the name of your project in this case it's flappy bird tutorial and anything that you save or create from godot and anything that you save into the user folder is gonna appear here okay this is gonna be our high score and every time we die we wanna check our score and if our score is greater than this high score we want that score to be the new high score so let's go inside our game over method and just before we call in its game over menu from the menu layer it's very important that you do this before you call this otherwise you're going to get some weird results just before that let's say if our score is greater than the high score let's set the high score to the score okay now let's talk about saving and loading this into this high score variable we're going to need two different methods which i'm going to create at the bottom here the first one is going to be save high score and the next one is going to be load high score to save we're going to have to create a new file and we will use the path constant that we created to open our save file save data dot open save file path and we want to write to this file the way this open method works is if the path you provided doesn't exist meaning there's no file named you save data.save on inside the user folder this method is just going to create a new file for us so even though at the start of the game we're not going to have a save data file yet the first time we save this file this method is going to create one for us let's store our high score variable using the save data's store var method and this is it so let's close it up so that we're done with this file now let's take a look at the load method and this is going to be used for loading the high score at the start of the game just like saving let's create a new file but this time before we open the path we wanna check if the file exists or not so i'll say if save data dot file exists save file path if it exists let's say save data dot open save file path and this time i'm not gonna write but we're gonna read from this file the reason we have to check if the file exists in the load method is there's basically no point in trying to load data from a file that's empty so if you don't check if the file exists or not and if it doesn't this open is just going to create a new file and that's going to be empty so and we're going to get some weird results trying to read some variable from a file that's empty okay now let's set our high score to the variable that's inside this file that we just opened and this getvar method is going to read the first variable that we saved inside this file and in this case we only have one variable anyway that we stored here and finally let's close this file file and now we have to call these methods from the rights places so let's go back to the ready method and let's get rid of this comment here and after we connect the obstacle spawner let's say load high score because we want the high score to be set before we start playing our game and ready gets called at the very beginning of the game and let's go to game over and if we died and if we have a new high score let's also say save high score we don't want to call this if the high score didn't change because there's no point saving it if it's the same but if we have a new high score let's go ahead and call save high score this should work fine but we also want to see our high score on the game over menu but right now we're only passing a score variable to the menu layer let's also pass our high score and let's go to the menu script to add the high score to the list of parameters here and let's set the high score label to this variable that we just passed okay great let's run the game and see if it works at the start of the game i'm just gonna die and our high score is zero as we expect now let's try to get some points one two that's enough okay so score two best two let's restart and let's die and our best is two looks like it's working now let's try to beat our best to see if it's updating or not three yes so now the best is three and just for good measure i'm just gonna play one more time and i'm gonna get like oops i'm not gonna attend points because i was too slow okay let's see if i can get to two and then die let's see okay my score was two the best is still three seems like it's working this is a really nice feature for our game and i hope that this was useful for you in the next tutorial we will take a look at sound effects which are really gonna bring our game to life if you liked this video make sure to comment subscribe and leave a like and thank you for watching i'll see you on the next one you
Info
Channel: Kaan Alpar
Views: 1,535
Rating: undefined out of 5
Keywords: godot, godot tutorial, kaan alpar, learn godot, godot 2d, indie game, indie game development, game development, game dev, gamedev, game developer, learn game development, learn gamedev, flappy bird, how to make flappy bird, flappy bird tutorial, flappy bird unity, godot game engine, godot engine, flappy bird godot, godot mobile game, godot 2d game, godot engine tutorial
Id: Lf3b7U6H2Mw
Channel Id: undefined
Length: 9min 29sec (569 seconds)
Published: Thu Oct 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.