GameMaker Tutorial: Saving & Loading .INI Files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
alright guys welcome back so in our last saving game tutorial we looked at how to save the game using text files and we would just write the name of the room that we're in to the text file and then we would load that name back whenever we selected continue what we're going to be looking at in this tutorial is how to use any files to save our game which will allow you to save more than one piece of information at a time and generally just provide you with a different more interesting way of setting up your saved games and saving other information like options and all sorts of things like for those can confuse this is what on any file generally sort of looks like when it's got data in it it has a bunch of sections kept in square brackets and then different keys in those sections and then each of those keys has a value so here this is like a slightly modified version of the indie file from another perspective so here in options for example language is set to 0 which by default would be English in the game fullscreen is 0 so it wouldn't be fullscreen the lights are turned on the timer is turned off fps turned off music turned on and so on so forth and like you can store numeric values to these in ease or you can also store string this like I'd app down here in the profile section name Sean J s as a string so this is just one any looks like and I'll show you how to create these files in game maker and also how to read information from them so that how we're going to do that is we're actually going to use the same game that we had set up for the first saving game tutorial which is just this little thing with the little green dude walking around going through the levels but this time instead of just going through the levels and use this as this here's level 1 points equals zero I've added a point system and so these these little pink squares you can pick up and then give you points just like a bunch more level we're gonna pick up three and then carry on through the level and says this is little three points it's three we just have a global variable is tracking how many points we have that's the end of the game and what I'm going to have this game do is at the beginning of every level we're going to save how many points we had on what level we're on and whenever we hit continuing to load how many points we had on what level we're on and then hopefully you can use the system and apply to whatever type of loading or saving system you're trying to create so I've got two scripts set up to do this on a totally blank at the moment SCR save game and SCR on just got a load game SCR and just got a load game will happen when we select to continue from the menu and se our save game will happen at the very beginning of every room our player object exists in so the beginning of each room the player object will call the script and we'll save the game one or the moment because the script doesn't do anything so that's what we're going to do first those of you who did watch the previous a game tutorial will recognize most of this code it's all very similar it's just you know got a few minor alterations but we're using any files as time when we're going to save our points as well our points for those wondering is going to be tracked by a global variable called global underscore points which is already set up in the code for this game there's that numbers added to it and so on sleep well so we know our points are any given time we just need to save and load that value from our ini' file so the first thing I'm going to do is say if file underscore exists save dot sab we're just going to use that arbitrary sea Sava extension again doesn't really matter what you put that file delete save that's how so if the file already exists we're gonna get rid of it so that we can overwrite it with a new save game whether or not you want to apply some sort of safety check here and say like Oh player there's already a save file you sure you want to overwrite etc depends on what you're making santé both but here we're just we're just gonna if the file already exists we're going to do it we need to know if exit sorry so assuming we've done that we know that there is no file AB we're going to say any underscore open saved us Plus save don't sad now you might be confused because we've just ensured that there is no such file because if the file did exist and we deleted it so why are we trying to open saved us app while all this really does is it this file for writing and and reading and if the file doesn't exist a game maker creates it the file itself isn't actually saved and created until you finish with the file once you've like added some stuff to it and then you say enclose then it will save the file if it didn't already exist in the first place but for now having done any underscore open this file is open now for reading and writing them and we can write data to it into memory and then when we close the file it will create the file if it didn't already exist so the first thing we want to do is just add the room that we're in exactly it's done basically in the same way we added the save room to our previous save game file say so yeah var saved room is going to equal room so it's going to set this variable to equal the index of whatever room we're in right now and I say any write real which means we're going to write a real number to our save file and now instead of just identifying the file we're writing to and identifying the the value that we're going to write into it like we were writing to a text file we need to establish exactly where we're writing this information to the any file because if you remember has a slightly more complex structure it has all of this so like it has this we need to know what section were writing to what so I'll bring it up again you know what section we're writing to what key we're writing to and what value we're actually going to be writing into that key so the first thing that you can see down here it comes up down there the section so the section we're going to be writing to I'm just going to say like like giving the idea that when maybe we had multiple save slots I'm gonna say like save one press next one by mistake and save one so you could have multiple sections got save one save two and so on so forth in theory because each key would be unique to that section you can have the same key in different sections the key is going to be the room so the room name that we're currently in and the actual value we're putting in that should be obvious say room the variable that we just assigned a second ago Sam you go on so now we've stored the room room and the next thing we want to do is store how many points we currently so I'm going to use the same line again enlight real which again is to write a real number if you want to write string and using the right string as well to write a string into the loonie file but i'm just going to write use real uses same section again this time the key can be called points and the value of writing that's going to be global dot points the number of points we currently have in the game and then all I need to do is say any underscore close Open bracket close bracket semicolon and then it'll take all that stuff that's done in the memory will close and will actually finish creating that don't sound file and we'll close it so that we can no longer read or write to it so now that we've done that and to save that I'm going to run this and hit start I'm going to move through so level two I'm going to pick up two of these so we have exactly two points and then move to level three so now we're on level three and we have two points to start this room is save down information so that information should be saved in our any file now so if I go to the folder the working folder for this which is out data local saving to just happens to be the name this project you can see here our new files been made called save dot SAP and if I open that you can see here it's done as we expected as created section called save one the points equals two that's the number of points we have and the the index of the room is three it's it's the third room so that's the room it'll load excellent so we know we're saving all our information correctly now now all we need to do is make it so we can load the game so I'm going to go to SEO underscore load game and again right mostly the same code as from the previous tutorial if file exists save dot SAP all right save save dot so there we go so if that file exists oh I can't type today at all so if that file exists any open again save dot salve we is the same line of code whether or not we're reading or writing unlike text file so we just open up the file again and we're going to say var loaded room equals any underscore read under store real so same as I'll prove this line but we're reading instead of writing now and so we're going to get a get a value from our any file and put it into this variable so we need to declare the section that we're reading which is say 1 um the key that we're reading which is the room and then all you need to do is put in a default value so if there is no if some reason it's open this file but those like I can't find this value or there's nothing in the value the value doesn't exist for some reason like it got edited or corrupted in some way then you need to give it a default they will just have to assume and that obviously makes sense to say level 1 which is the room name of our first level so that's in that line then we need to we need to load in the number of points that we've got on our save game the global dot points we're just going to assign this to PE whatever we read from the file anyways go read real again save 1 the key is points and the default value would be 0 but guess any under store clothes say so we opened up a reading we're not written anything to it this time but we've just read this information from it and then wack we've done that we can use that information as a room go to a little bit and then we can put an else in here as well if the file doesn't exist at all and just not do anything in there maybe you want to have some things as all we can find just a file or whatever but for the sake of this tutorial I'm happy with it they don't do anything why is this alright of course yeah so you don't actually need to put in this sorry my mistake if you were writing along you don't actually need to put in the name of the file you opened with any underscore open you just need to write any underscore close as I'm pretty sure we did and I'll say game one yeah you just need to write only underscore clothes and it will already know what you want it's a little bit smarter and easier to use really than the the text file reading and writing so that's that now we should load in the the room which as we knew is three and the point which is two so we should load on to level three with two points when we run the script and there we go it works perfectly this is level three points two and if I move on to level four and I close the game reopen the game continue level four two points C remembers everything so that's that that's how you save and load using in any file I hope you guys enjoyed that oh I know what catch you guys next time see it you
Info
Channel: Shaun Spalding
Views: 75,862
Rating: undefined out of 5
Keywords: Game Maker (Video Game Engine), Game Development, Games, Tutorial, Information, Learning, Game Maker Studio, Windows, Making Games, Basics, INI File (File Format), saving, loading, save, load, GameMaker: Studio (Video Game Engine)
Id: NH9JxykJb6k
Channel Id: undefined
Length: 12min 41sec (761 seconds)
Published: Wed Jan 07 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.