Reading and Saving Game Data

Previous topic - Next topic

aroldo

I wrote subs to read and save my game data.
If it is the first time the LoadGame will load default data, because there is not brick.ini file.
When the game is over I save the game data.
The second time I start the game it tries to read the brick.ini file and fails with the  error : No file!

What is the problem?
I am running GLBasic IDE, Version: 10.118 on Windows.
Code (glbasic) Select

// ------------------------------------------------------------- //
// ---  SAVEGAME  ---
// ------------------------------------------------------------- //
SUB saveGame:
LOCAL st=FALSE
LOCAL info$ = PLATFORMINFO$("DOCUMENTS")
LOCAL gameconfig$ = info$+"/brick.ini"
DEBUG " ****FILE:"+gameconfig$
DEBUG " ****name5:"+name5$+" top5:"+top5%
DEBUG " name4:"+name4$+" top4:"+top4%
DEBUG " name3:"+name3$+" top3:"+top3%
DEBUG " name2:"+name2$+" top2:"+top2%
DEBUG " name1:"+name1$+" top1:"+top1%
st=OPENFILE (1,gameconfig$,FALSE)

IF (st)
DEBUG " SAVEGAME"
WRITELINE 1,control$
WRITELINE 1,name5$
WRITELINE 1,name4$
WRITELINE 1,name3$
WRITELINE 1,name2$
WRITELINE 1,name1$
WRITELONG 1,top5%
WRITELONG 1,top4%
WRITELONG 1,top3%
WRITELONG 1,top2%
WRITELONG 1,top1%
WRITELONG 1,hiscore%
WRITEBYTE 1,sound%
WRITEBYTE 1,balls%
WRITEBYTE 1,level%
CLOSEFILE 1
ENDIF

ENDSUB // SAVEGAME




// ------------------------------------------------------------- //
// ---  LOADGAME  ---
// ------------------------------------------------------------- //
SUB loadGame:
LOCAL st=FALSE
LOCAL info$ = PLATFORMINFO$("DOCUMENTS")
LOCAL gameconfig$ = info$+"/brick.ini"

st=OPENFILE (1,gameconfig$,TRUE)
DEBUG " LOADGAME st="+st


IF st = TRUE
DEBUG "Reading File"
READLINE 1,speed$
READLINE 1,control$
READLINE 1,name5$
READLINE 1,name4$
READLINE 1,name3$
READLINE 1,name2$
READLINE 1,name1$
READLONG 1,top5%
READLONG 1,top4%
READLONG 1,top3%
READLONG 1,top2%
READLONG 1,top1%
READLONG 1,hiscore%
READBYTE 1,sound%
READBYTE 1,balls%
READBYTE 1,level%
CLOSEFILE 1
ELSE
DEBUG "Default Data"
top1%=80;top2%=140;top3%=200;top4%=240;top5%=380
name1$="Sampa";name2$="Daniel";name3$="Carol";name4$="Gabriel";name5$="Tico"


ENDIF

DEBUG " ---- name5:"+name5$+" top5:"+top5%
DEBUG " name4:"+name4$+" top4:"+top4%
DEBUG " name3:"+name3$+" top3:"+top3%
DEBUG " name2:"+name2$+" top2:"+top2%
DEBUG " name1:"+name1$+" top1:"+top1%
ENDSUB // LOADGAME

[a http://apd-games.com/][img http://apd-games.com/images/APDGames135.png][/a]
MacBook Pro OS X El Capitan
XCode Version 7
iPhone 6 running  iOS 9
iPad Mini running  iOS 7.1
Galaxy S5
Dell Latitude Windows 8 Enterprise
Palm Pre, Palm Pre2

ByteByter

Quote from: aroldo on 2011-Sep-29
   IF (st)
Here is your problem. This should be:
Code (glbasic) Select

if (st=true)

Otherwise, there is no boolean evaluation, and therefore the if statement will never be executed.

Falstaff

Quote from: ByteByter on 2011-Sep-29
Quote from: aroldo on 2011-Sep-29
   IF (st)
Here is your problem. This should be:
Code (glbasic) Select

if (st=true)

Otherwise, there is no boolean evaluation, and therefore the if statement will never be executed.

Actually IF (st) *is* the same thing as IF (st = true).. it's sort of like shorthand.

Anyway aroldo the problem was you added an extra line in your loadGame sub, at the top: READLINE 1,speed$. This line isn't saved in the saveGame sub, so I guess when it got to the last ReadLine call it probably read the rest of the file. Then the next call to read more from the file crashed.

I tried your code with a WRITELINE 1,speed$ at the top of the saveGame sub and it worked fine.

I wonder if your approach is cross-platform friendly with iphone and android? (Anyone know if we should save stuff to PLATFORMINFO$("DOCUMENTS") on those platforms?)

Ian Price

PLATFORMINFO$("DOCUMENTS") is perfectly fine for iOS and webOS. Don't know about Android, but I suspect that this is fine.
I came. I saw. I played.

Kitty Hello

Check if the file was written. DOCUMENTS is something like "C:\users\you\My Documents".

"IF a" equals to "IF a <> FALSE" - TRUE is a value of "1". Whereas "IF a" triggers also if a would be 2 or -1.


spicypixel

This works for me in my game

Code (glbasic) Select

// .-------------------------------------------------------------------------------------.
// |                                                                                     |
// | Load Variables and Initialise Settings                                              |
// |                                                                                     |
// '-------------------------------------------------------------------------------------'

// Initially Set Stage Unlocked, High Score and Music/SFX Status
GLOBAL Unlocked% ; Unlocked% = 1
GLOBAL HighScore% ; HighScore% = 25000
GLOBAL SFX_On% ; SFX_On% = TRUE
GLOBAL Music_On% ; Music_On% = TRUE

// Load Settings (overwrite variable of the settings defined above)
GLOBAL path$, settings_file_exists
path$ = PLATFORMINFO$("DOCUMENTS")+"/"
settings_file_exists = DOESFILEEXIST(path$ + "plebz_config.bin")
IF settings_file_exists
OPENFILE (1, path$ + "plebz_config.bin", TRUE)
READUBYTE 1, Unlocked%
READULONG 1, HighScore%
READUBYTE 1, SFX_On%
READUBYTE 1, Music_On%
CLOSEFILE 1
ENDIF


And on quit

Code (glbasic) Select

// .----------------------------------------------------------------------.
// |                                                                      |
// | Game QUIT                                                            |
// |                                                                      |
// '----------------------------------------------------------------------'

SUB GLB_ON_QUIT:
// Save the Settings
OPENFILE(1, path$ + "plebz_config.bin", FALSE)
WRITEUBYTE 1, Unlocked%
WRITEULONG 1, HighScore%
WRITEUBYTE 1, SFX_On%
WRITEUBYTE 1, Music_On%
CLOSEFILE 1
ENDSUB
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

aroldo

Team,

Thank you, the problem was the missing "WRITELINE 1,speed$" in the save game sub as Falstaff.
In any case I cahnged the If (st) to if(st=true) just to be consistent.

O another note I enjoy how fast the developers on this forum respond! I did not have this experience on the WebOS forum.

Thank you all :good:
[a http://apd-games.com/][img http://apd-games.com/images/APDGames135.png][/a]
MacBook Pro OS X El Capitan
XCode Version 7
iPhone 6 running  iOS 9
iPad Mini running  iOS 7.1
Galaxy S5
Dell Latitude Windows 8 Enterprise
Palm Pre, Palm Pre2

Kitty Hello

Quote from: aroldo on 2011-Sep-29
Thank you, the problem was the missing "WRITELINE 1,speed$" in the save game sub as Falstaff.
Awesome bug! That's the sort of bug you search for ages. Good find.
Someone should write a game with altered pictures, but instead of pictures, you have to find programming errors...