Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Saving high scores to a file that DarkBASIC can read back

Author
Message
Yodaman Jer
User Banned
Posted: 5th Mar 2009 22:27
Hey all, I'm trying to learn how to save a high score variable into a file that DB can read back.

I've looked at the DB example program 'Open to Write' (Code:
), but I have no idea how I would implement it into my game. The question I have is, how do I save my variable ('Score') to a file? What commands would I use? I've tried searching the help files...I don't really understand the process that well. Does anyone know of any online tutorials that show you how to save a high score file? And help/comments or whatever is appreciated!



Check out my programming blog!
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 5th Mar 2009 22:31
You could store the scores in an array and then use SAVE ARRAY. That way you just save it once, with no loops or anything, and then you can load it once, with no loops, and it will just load it straight in.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Yodaman Jer
User Banned
Posted: 5th Mar 2009 22:41
OK! So, I make an array and name it Dim Score(1,1) and then just use that variable in the game, then in the final winning routine just save the array? Like so...



I'm kind of at a loss on how to display the contents of the score array to the screen so that the player can see it...



Check out my programming blog!
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 5th Mar 2009 22:54
Quote: "I'm kind of at a loss on how to display the contents of the score array to the screen so that the player can see it..."


I think a few months ago there was a DBC challenge for making a creative high score table, you could check out that section for inspiration.

For the SAVE ARRAY command, I believe it gives you an error if you use your entire array.

Instead of:

SAVE ARRAY "Score.arr",scores(1,1)

you would use

SAVE ARRAY "Score.arr",Scores(1)

However, what you can do is save ALL scores at once, using an array size 10 like so:

Dim HighScores(10)

Then you can use a for->next loop to compare and see if the player's score places and then you can just delete the file if it exists and then save the array again. That way, you only have a single file for your high scores and it will still be able to place the person. (Actually, you should probably use 2 arrays, one for the score, and one for the names).

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Yodaman Jer
User Banned
Posted: 5th Mar 2009 23:04 Edited at: 5th Mar 2009 23:06
I'm silly. Lol, I just checked out TDKs tutorials on file access and found the following code, pretty much matching what you said BN2:

This goes at the beginning of the program:



And this goes at the end:



How come I never think to check TDKs tutorials until after I post threads?

lol. Does that code match what you were thinking, BN2?

EDIT: Wouldn't I need to add a bit of code in the beginning to get the player's name to save in the high score table though?



Check out my programming blog!
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 5th Mar 2009 23:27
Quote: "lol. Does that code match what you were thinking, BN2?"


+/- I was thinking more along the lines of the SAVE ARRAY command, but that works just as well.

Quote: "EDIT: Wouldn't I need to add a bit of code in the beginning to get the player's name to save in the high score table though?"


Yeah, you will need a way to get the player's name.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Yodaman Jer
User Banned
Posted: 6th Mar 2009 00:27
OK, I tried searching on how to load an array and print its contents to the screen, but I found nothing.


Here's some of my code that shows what I'm trying to do...It's sort of messy, so I'll try and clean it up:

This is for viewing High Scores:



I dunno what I'd do to print the info in the file to the screen. Here's the saving code:



I just have no idea how I would save multiple strings of data and then read them back again. I'm really a n00b when it comes to file saving. :-\



Check out my programming blog!
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 6th Mar 2009 00:47
Once you load it, treat it like any other array situation.

So to print it to the screen you could do this:



Obviously, you will need more than that to make it look nice, but you can access the data just like it is. You have to declare the array in the program that loads it (not sure exactly what the syntax is for loading though). I would also suggest using the text command, on account of being able to put the text anywhere.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Yodaman Jer
User Banned
Posted: 6th Mar 2009 00:51

I forgot that an array was like almost any other variable....Gah...

Thanks BN2. I'll try that out and see what happens.



Check out my programming blog!
Ed222
18
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: Calgary
Posted: 6th Mar 2009 00:52 Edited at: 6th Mar 2009 00:55
Quote: "I dunno what I'd do to print the info in the file to the screen"


Quote: "Center Text 500,500,"Your final score is: "+STR$(Score)"


To print the content of your arrays just replace the variable "score" with the array where your score is stored



This would print the score stored at HighScore$(1,1)


[Edit]
Looks like I took 8 Minutes too long to write this.
Yodaman Jer
User Banned
Posted: 6th Mar 2009 01:20
OK, this is weird...

I can save the array. The only problem is, it only saves the digit '0'.

Here's my modified code:



Every time I run that, it only reads '0'. Also, at the end of the game, even though in-game the score is high, it will only print '0'.

So, at the end of the game you may have had 65895 points, but after you lose, you apparently only had '0' the whole time.

Here's the saving code...



And here's the line of code where I declare ALL of my variables, whether they're arrays or not...



Any ideas why it only prints '0' to the screen, even after just losing?



Check out my programming blog!
Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 6th Mar 2009 06:04 Edited at: 6th Mar 2009 06:05
Why have you dimensioned the Score array as a 2D array,

Dim Score(10,10)

Shouldn't this be a 1D array ?

Dim Score(10)

That1Smart Guy
17
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 6th Mar 2009 15:55
ya im not seeing what u would need for 2d, xcept maybe names, but even that would only be (10,2)
Caleb1994
17
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 6th Mar 2009 16:38
if he needed names then it would need to be a string and he would have to you Variable=Val(Scores$(1,1)) to use it
Yodaman Jer
User Banned
Posted: 6th Mar 2009 19:23
Oops, that's the wrong code. lol. Sorry, in the program I actually did dimension it to only 1D, but it still didn't work. I'm giving up for now, there's a few more important things that I need to do in my code before I try and implement anything new. I'll come back towards the end of the project and try and get the Hiscore Table to work then.



Check out my programming blog!
Yodaman Jer
User Banned
Posted: 6th Mar 2009 20:26
I think I just figured out what the problem is...

At the beginning of my game I declare the variables. This includes the DIM HiScore(10) command, and then I use the HiScore variable IN GAME. What I mean is, if the player earns points like so...

If object collision(1,4) then HiScore=HiScore+100

...instead of just using a different variable that gets checked at the end of the game...like so...

If Score=>HiScore then GoSub WRITEFILE, else GoSub END

So, I shouldn't use the actual DIM HiScore variable in game like I was, should I? I'm guessing that's the problem.



Check out my programming blog!
Caleb1994
17
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 6th Mar 2009 20:48
No you should not use it ingame lol that is used only for saving. thats probly all it was.

Login to post a reply

Server time is: 2026-07-05 00:12:42
Your offset time is: 2026-07-05 00:12:42