GameMaker Studio 2: Complete Platformer Tutorial (Part 11: Save & Load)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome to part 11 where we're gonna look at loading and saving your game we're gonna do this and probably one of the simplest ways possible not quite the simplest way we're not gonna use the built-in game save game load of functions that are built into GML we're gonna do it manually by creating our own file and writing information in that file because teaching you how to do that the basic level then lets you let you go from there and learn how to do more complex things in the future and you might be able to imagine how you do more complex so you can leave it so ting and leaving saving and loading but what we're going to do is going to be very simple we're gonna save the name of the room they are in at the start of every level so a really quick autosave system and then when we hit continue we'll load up that file and it'll tell us what room we need to go to and we'll go to that room okay it's gonna be simple so I've made a couple of changes to the game as is so if I run it at the moment you can see this is all this is all normal what we expect for I've added another I've changed up the second level a bit and added a third level as well so this is our second level just so you can see I screwed the tiling a bit over there but just so you can tell that it's very different from the first level because it's gonna be important for us to see that we've arrived and 11 we think and then this is the third level which is obviously very different okay it's not exactly great level design or anything I've just done it so that it looks different so that we can tell when we've changed levels and everything okay so we know that this is definitely level 3 the end level of the game nowhere else to go okay so before I do anything else for setting this up I'm gonna go to ol menu first of all in the create event because this is like the first object we have it's kind of I mean it's good idea to have like a dedicated initialization script or initialization object but this is going to be good enough for the purposes of our game we know that this is the first object it gets made I'm gonna create what's called a macro okay by typing hash and macro at the start of things see that tense all bold and yellow and alright save file in all caps okay macros are kind of like kind of they're not exactly so I don't want to say they are constants but they are similar to constants in the sense that they're a good way to make variables that are one bürol one thing and never ever change okay so you can just write this word in your code from this point onwards and know that it will always mean a given thing okay and that given thing is going to be saved okay it's just the name the file name that we're gonna use as our save game okay it's just going to be a text file whether that we write the name of the room that we're into but by by putting it in this macro it means I can just write a save file anywhere game code it'll turn red like that because it knows it's a macro and it'll contain this information so yeah I didn't put an equal sign in here either you just put a space and you also don't end with a semicolon okay which I know is a bit confusing but just macros specifically work differently so the variables okay so you don't use an equal sign you just write whatever you're assigning it to with a space and don't put a semicolon on the end either it's to do with how macros work how they actually work internally is like they copy and paste essentially this info wherever you put this so if you put a semicolon on the end of it like cause problems you know Co because this would include the semicolon and so on so forth okay that's why I didn't want to say they were strictly constants cuz they don't work exactly the same way but it's handy because it means if I want to change the name of the save file or whatever I can just do that once here and don't need to do it anywhere else okay it's global in scope so even though I've done in menu I can access that from anywhere okay so now I've done that I'm going to close out menu now and I'm gonna open up a player okay I've already actually created this event I've gotta put in a above create the room start event okay you just go to an event other room start that's an event that triggers for any instance of this object at the start of a room so okay since we've got one player objects in every room where the player has any kind of control right we don't have one in menu but that's it it makes it a pretty decent place to do our autosave right makes sense so how are we going to always say well the first thing I'm gonna do is check to see if our save file actually already exists like we already have one and if it does we're just gonna delete it we're gonna just overwrite it essentially and that's the simplest way to overwrite a file entirely it's just to delete it and make it again so I'm gonna first of all write comment that just says overwrite old save okay the next thing I'm gonna do is write if file exists okay you can see where this is going already it's very simple commands open brackets save file it's the name of the file the exact file name close bracket and what do we want to do if that does exist wanna do a file delete simple as that now you might be thinking where is this actually happening like where is it looking for and finding these files so when you're just reading files you can read them from wherever your game bundle is so like if it's just in a folder on your desktop you can you can do file exists and it will check for files that exist just around the executable and so on like that but if you want to be able to write to files as well as read from them the only place you can do that and it'll check checks both places whenever you do anything to read files but will be in on Windows at least in a special directory specifically for this app that gets created when you run it which will be app data forward slash like the name of your game and so on well I'll show you the exact address of where this one gets created in a little bit so that's where I'll save files are gonna be created and that's where it's gonna check for them so now we've checked done over and Aaliya to save let's actually create a new say to create new save var file it's literally just going to be a temporary variable that we use to store the ID of a file that we've opened and that's gonna be file equals file underscore texts underscore open I'm just going right okay so that's opening a file a text file specifically we're opening it for the purposes of writing to it okay and that's gonna be saved file okay and that's that's the file name that we needed to put in there okay and then that returns an ID so that we can reference this specific file that we have opened in order to do stuff with it okay which is what we're gonna you know file.txt right real okay so I'm going to take a real number so that's like a lot a number with decimal places think of it that way like the opposite of an integer so not a whole number necessarily and we're gonna write the the real number that we get well the file we're gonna write to is file obviously and the real number that we're gonna write in is room now you might be thinking well rooms not a real number room is like our menu r1 r2 r3 and so on well here's a fun fact everything game maker is basically a real number okay these do have names associated with them and I think you can find them with like room get name or something like that every one fact every resource is a way to get its name but they all just have index like like when you use something like room or room go to and so on so forth they all just have indexes which are just real numbers everything is basically a real number you would think they were integers like not one two three and they are ordered like that like this our menu will be or 0 R 1 will be 1 2 will be 2 and so on right but they're actually naught point naught naught naught 1 point no not not to point naught naught naught 3 point 9 etc ok it actually basically everything in game maker is actually stored as a real number and can be read as one as well so I'm gonna write that real number to the file room so if we're in room 1 and alright one if we're into 2 right 2 and so on it's worth knowing that they'd only worked out that way exactly because our menu is at the first end which would be room 0 ok so if we were to save our menu for some reason that will be 0 that's 1 that's 2 that's 3 okay so once we've written that we can close the file again so don't need it anymore so file txt close Open bracket file is the ID that we want to close and then that's all there is to it so just to demonstrate that this is working it won't seem like it's doing anything right away but if I to start the game hit new game I'm in here now let's go to level 2 so that we should see room ID of 2 in the file ok so I'm in this room now and now I'm just going to navigate cuz I've already done it at this point I'm just going to navigate to that file and show you what it contains ok ok so here I am cforce ice uses Ranjha amp date a local super platform at 11:00 that's the name of the game right and that's more or less where your folders gonna be okay there's a bunch of it you see a bunch of like projects and names and stuff like that and games and things and so on that gets their data stored in here it's just a normal way applications generally work with Windows now and you can see in here the file saved on staff that's been created for if I open that it's opened up my other mono but if I drag it over here it's - okay if I now were to go in fact I can yeah if I go all the way your room three pictures jump over yeah okay then I close this and reopen it again we should see there's three now okay okay simple as that so we know that we're saving the data correctly now we just need to be able to actually load it back out okay how do we do that okay so open up Oh menu let's go to the step event 400 menu and you remember in here this is where let's maximize this this is where we control what happens when we commit to a menu option okay remember this client recently before and so we have case two and default for new game and currently also continue because we don't have a case for continuing yet that's organ and you know in case 0 for quit okay so let's make case 1 so we know option 1 was our middle menu item which was continue and by writing a case for arm it means we no longer run in fact we can get rid of the default one actually at this point just in case but it would no longer of run it anyway it's the one of dinner it wouldn't have ever needed to go to default it would have had a specific case to go to instead so case 1 what we want to do is I'm gonna open and close brackets let me bring this one down as well keep it consistent with how we styled this I'm gonna write break here as well okay so cuz we're going to need a bit more room than just the one-liners that we had for those other two cases so we just open this block and put the break at the end here as well as that completes the case statement I'm gonna check to see first of all if we have a save file okay if in fact they're right not so if file exists save file okay because I put it assassination mark it's gonna return true if it doesn't if there is no save file okay so file doesn't exist essentially that's why I've written there then what I want to do is essentially the exact same thing as what I just did here so I'm gonna copy this and put it in there so if we don't have a save file we'll treat it as if what you meant to do is select a new game okay because there's nothing to continue from else we can assume we do have a save file and we can load it okay so the bar file again I'm just gonna store the idea the file we have open file hence open read okay so I'm going to open a file for the purposes of reading it and a text file specifically save file is the file name of that file it's C save dots at right bar target's equals file on the score of text underscore read underscore real so this is gonna read a real number in from from the file that we have open okay it gets more complicated if you've got multiple thing multiple lines of text in a text file you have to do stuff like moving from line to line and checking the AR the end of a line and so on so forth since we just have this literal one piece of data in a text file it's really really easy when you just read it straight back out and so we just read real from the very start of the file and we know that that's going to be the number that contains the room that we want to go to okay then file X close open bracket file close bracket semicolon so they're not just closes the file again so we have in target the name of the room while the index of the room that we want to go to so then all we need to do is slide transition solve way of moving between rooms right trans mode don't go to ok so roll and just go into the next room we go to a specific one we add that in as our second argument which is targets close bracket semicolon that I believe is all there is to it you have close that close that break yeah we should be good so if I run this now we know that are safe I lower is already at three so I've got to continue I mean see it loads is in at room number three okay so let me restart the game and do a new game now so I'm in room one let's go to room two this will already have overwritten it with room one though which is a thing you'll probably want to do something in your game this like hey check for overwriting files but as I say we're keeping this really really simple for now just so that you get the idea of how it works we might come back in to do some tile over write checks and stuff later on but for now we're just keeping a simple server in room two so that's that that's actually close the game down that's a good extra test as well so I'm just checking that file again now actually as well see if else have here open it up - okay so it's got a number two in it that's what we expected okay one thing you might notice as well you know as I ought ematic legal files opening with notepad or whatever but it because I've got dots have you'll probably have to specify like open with text box it's not the usual file extension for a text file out of B txt right I've just done has a very like very low barrier kind of way to stop people editing the save file right and that's another thing you all wanna bear in mind obviously this is a simplest way of saving data and obviously people could just edit this date or and just set it to like 10 or whatever and skip a bunch of levels in your game now B no good right there's only ever so much you can do to kind of prevent that kind of thing but we III have another episode on kind of encryption and other people have done tutorials on encrypting or save files and stuff like that so look around YouTube look around Google there's plenty of stuff for work now now if that's really really important to you as well but keep in mind even just with a system either someone has got to come all the way to out data open your safe are to open with and change these numbers around and so on which is kind of although I work and you know if someone is really that desperate to cheat in your game maybe just let them I don't know oh do you but as I say if that is a concern to you there are other toriel's and things that cover that kind of thing okay but anyway this is the value that we wanted it to be outside to let's run the game again now and we should see that we can one through the level if I go to continue with the big Hills continue yes the big hill going down there this is room number two as expected I got a new game again this is room one the flat ground if I restart then go to continue it'll put as the same place right this is just a room one okay so we can see it works that is that's the simplest way to create a basic saving and loading system I'll catch you guys next time hope you enjoyed this on thank you as always to my amazing patreon supporters for making this work a reality thank you in particular to Dan in a mule Giles Montgomery Herald Katrina Fanueil Walsh Louis R Pereira Nick slavish Stephen Hagen Jason McMillan oh and Morgan Bowser the dog and John Grimshaw that list gets longer every month and it's really awesome if you want to join these cool kids you can do so by heading over to patreon.com forward slash Sean J yes people who support me there get all sorts of cool perks that are probably were probably on your screen a second ago anyway thanks for watching I'll catch you guys next week
Info
Channel: Shaun Spalding
Views: 82,334
Rating: undefined out of 5
Keywords: Game Maker (Video Game Engine), Tutorial, GameMaker Tutorial, GameMaker, Game Development, Indie Games, Tutorial Series, Game Maker Studio, Making Games, How to make games, GameMaker Studio 2, GMS, GMS2
Id: c1gx7NMZ0Uc
Channel Id: undefined
Length: 16min 37sec (997 seconds)
Published: Fri Dec 08 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.