Saving and loading system in game?
Blitz3D Forums/Blitz3D Programming/Saving and loading system in game?
| ||
For my game, I'm trying to implement as simple "save point" system where you'd save in-game via statues (fairly commom trope) and it would save the level's number, player's position and other things like points and health. Another thing is, I'm trying to make a system where once you press start on the title screen, the game will automatically load that saved data from last time, but if the game can't find the save, it would start as if you'd never played it before (everything is default). I'm only looking for help with the saving and loading. I've got the entire system of the statues and the title screen set and clear, but saving and loading the data is where I need help in. Could someone please help me out? I hope my explaination of my problem wasn't too badly put:/ |
| ||
I try to help. You must create a file on the hard disk that holds the positions of the player and everything about the game, however the game Begin, that file will be read and if there continue. That file can be created with a button "Save Game" or that it automatically each time interval. |
| ||
Hi Yue I'm very thankful for your willingness to help me, but I think you misunderstood my question. I want to know how to use the blitz functions of saving and loading. I'm still very grateful for your efforts to aid me:) |
| ||
TaGames>> This is a record+replay example but it shows how to write datas to a file, and then how to read datas from a file. http://blitzbasic.com/codearcs/codearcs.php?code=2944 There are several others examples in the code archives, http://www.google.fr/#q=write+read+file+site:blitzbasic.com |
| ||
hi see: http://blitzbasic.com/codearcs/codearcs.php?code=2863 Juan |
| ||
One more point: If you want a greater level of detail in your saves, consider BlitzXML: http://www.blitzbasic.com/Community/posts.php?topic=46647 I notice the actual file wasn't at the link in the thread anymore, but if you want it I think I have it. |
| ||
Okay, tell me if I'm doing this right or not ( just a brief genearalizition; not made to actually "run" ) I'm VERY sure I'm not understanding this... |
| ||
I don't think you need all those variables. Try something simpler:Function TitleScreen() ;Render whatever title\menu graphics you want. DrawTitleScreen() ;If any of the keys assigned to "Start" are pressed, start the game. If (KeyHit(...) + KeyHit(...) + ...) Then If FileType("savedgame.dat") = 0 Then Return BEGIN_NEW_GAME ;No save-game file was found, so start the game from scratch. Else Return LOAD_GAME ;The file was found, so rebuild whatever level the player was in, down to the last details. EndIf EndIf Return TITLE_SCREEN End Function In your app's main loop... Select currentState Case TITLE_SCREEN currentState = TitleScreen() Case BEGIN_NEW_GAME currentState = BeginNewGame() Case BUILD_LEVEL_1 currentState = BuildLevel1() Case PLAY_LEVEL_1 currentState = PlayLevel1() Case BUILD_LEVEL_2 currentState = BuildLevel2() Case PLAY_LEVEL_2 currentState = PlayLevel2() Case LOAD_GAME currentState = LoadGame() End SelectInside LoadGame() you'd load the file (ReadFile() is fine) and rebuild the state the game was in as it will be described in the file: all actors, props, objects etc. with current healths, frames of animation, positions. When the loading function is done, at the end of it you set the currentState to whatever level the player was in, so that Select block runs the appropriate level function. Thing is, the load function will have already built the level you're going to restore and repositioned everything as it was, so there's no need for the load function to change state to BUILD_LEVEL_#, but rather to PLAY_LEVEL_# so you continue straight into gameplay. Consider the BUILD_LEVEL_# functions as code that sets up the levels: loads the environment mesh, creates objects, positions the player, shows level intro cutscenes etc. - These won't be needed if you're loading a game that has a level being played; that's why the saved-game loading function already heads to one of those PLAY_LEVEL_# functions that only let the game run. |
| ||
I notice the actual file wasn't at the link in the thread anymore http://web.archive.org/web/20100126022607/http://www.alsbonsai.com/john/BlitzXML_v1.71.zip |
| ||
To the rescue! Thanks. |
| ||
I'm wondering how to store my save data and read it in banks, so there IS no save file. |
| ||
Thundros wrote "I'm wondering how to store my save data and read it..." I assume that you want to use save-files sometimes, but not all the time. In case you'd want to send the save-file into a bank then you can do something like this: ;;------------------------------------------------------------------------ ;Creates and reads a bank from filename$ ;size=file size, pos=file position ;Returns a bank Function FiBankFromFile(filename$, size = 0, pos = 0) Local bank, file If size = 0 Then size = FileSize(filename$) bank = CreateBank(size) file = ReadFile(filename$) If file SeekFile(file, pos) ReadBytes(bank, file, 0, size) CloseFile(file) EndIf Return bank End Function ;Appends a bank to filename$ ;size=file size, pos=file position ;Returns True if succeeds or False if fails Function FiBankToFile(bank, filename$, size = 0, pos = 0) Local file If bank = 0 Then Return 0 ;No bank If size = 0 Then size = FileSize(filename$) file = OpenFile(filename$) ;Existing file If file = 0 Then file = WriteFile(filename$) ;New file If file SeekFile(file, pos) WriteBytes(bank, file, 0, size) CloseFile(file) file = 1 EndIf Return file End Function ;;------------------------------------------------------------------------ The above functions were taken from the FreeImage.BB package: ( Has excellent image functions, along with some functions that don't involve images as well. ) FreeImage link: http://freeimage.sourceforge.net/ Sorry, the black background hurts my eyes so sometimes I'll just show stuff without using [codebox]. ![]() |
| ||
Sorry, the black background hurts my eyes so sometimes I'll just show stuff without using [codebox]. Try a different theme, I am using default or something, whites and greys, boring to look at but easy on the eyes regarding text. Although I do agree that at some point a bank would need to be saved to a file, unless of course you are making more of a checkpoint, in that regard no need to save it if it is a temp and RAM would suffice. |
| ||
No, actually, I want to save the memory to a memory bank, and somehow have it there next time I open the app so I can load it from memory. |
| ||
That's what a save file is for. Yes I see post #11 where you also specify "no save file". What you are asking for is impossible and defies the entire concept of a filesystem. Memory doesn't work that way. It is reset every time the program runs. This is why your earlier attempt to save entity handles didn't work: the entities stop existing between program runs, and so does everything else that isn't a file. Note that you can save the entire contents of a bank in one operation with WriteBytes, and load the entire contents of a file to a bank using CreateBank(FileSize(...)) + ReadBytes. But there's no magic solution that can keep memory valid between program runs without you doing at least that much. |
| ||
just for clarification, do you mean saving a memory bank to the hard drive and then retrieving said file upon start up or just assuming that nothing like a pc restart will happen between you exiting the game and then later reentering, cos I'm pretty sure most programs have a house clearance freeing up ram on exit. basically for the first option you just need it to auto load a specific file upon start up, basically a save game file ... 2nd option, fools errand. |
| ||
Ok, relax guys. I was just curious. |
| ||
@TaGames... First off, and this is only personal opinion, but I would forget XML and INI files for this as I think it complicates the task. The easiest way, is something like this... consider this pseudocode as my B3D is years rusty and it's from memory. type tAlien field x,y end type function writeAlien(alien.tAlien) writeInt(alien\x) writeInt(alien\y) end function function readAlien.tAlien() alien.tAlien=new tAlien alien\x=readInt() alien\y=readInt() end function So if you have a type tAlien, this should write its fields to disc, and recreate it by reading the fields back into a new tAlien. Got a whole bunch of aliens? Write the number of aliens to disc, then call writeAlien for each one. To Recreate them, first read a number from file (this tells you how many aliens to create), then call readAlien that many times. The key here is to ensure you read everything back in the same order you wrote it. Do this by keeping your read and write function next to each other so you can compare orders. function readgame() readaliens() readplayers() end function function writegame() writealiens() writeplayers() end function Using this method, you can rebuild all your types. Stuff that isn't based on types can use a similar method. Obviously I've missed a few bits out like opening and closing files, but I'm sure you get the idea. The advantage of INI files and XML is human readability, but it's all just bloat for a game load/save feature. |