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 / Confused about saving/loading arrays Please Help !!

Author
Message
Zaxtor99
8
Years of Service
User Offline
Joined: 27th Jul 2011
Location: Boise, Idaho, USA
Posted: 28th Jul 2011 02:13
Okay, I've looked all over and just can't figure out the answer to my problem.. and I have a feeling that it is something super simple...

I have created a game using DarkBasic (not Pro).

I am using about 5 arrays to save/load the players name, high score, money acquired, and other stats from the game.

I have Dim'd the arrays at the beginning. I load the arrays at the beginning as well. Such as one of the five is
Dim Hiscore(10)

for z=1 to 10
load array "Scores.arr",Hiscore(z)
next z

After the game is over, I use for/next loops to check their score against the ten high scores that I loaded. Then I use another for next loop to update all the high scores if the players score places in the top ten.

All of that works perfectly when it then prints the updated high scores at the end of the gamme.

My problem is that when I quit the game and reload the game, nothing actually saves even though I use a for next loop to save all the same arrays I loaded and update the top ten list if the player placed in the top 10

What's funny is if execute the program from DarkBasic itself, and use F12 to stop then re-run the game from DarkBasic again, the High Score list is properly saved and displayed every time.

I figured it was simply making sure my final exe file was included in the same folder with the 5 .arr arrays. but that didn't work either. Then I tried putting the array files in the same folder with the project file of the game BEFORE I built the final again (after deleting the other exe).. but still no dice.

It is EXACTLY the same code that works saving/loading the arrays directly from DarkBasic. I am clueless as to what I am doing wrong.

Thanks in advance for any help. Like I say, I have a strong feeling this is a super simple problem. It's just my first time ever trying to save/load arrays using DB.


- Zaxx
BN2 Productions
15
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 28th Jul 2011 07:39
First off, unless I am misinterpreting things, you shouldn't need a for-next loop to save/load the array data. The SAVE ARRAY command will save all parts of the array, so you are basically saving the same thing to multiple files and loading them back 10 times, but each one is exactly the same and contains all of the data.

Secondly, it sounds like you are running Windows Vista/Windows 7. If this is the case, try right-clicking on the executable that you created (that isn't working right) and choosing "Run as an Administrator". See if that fixes it. If it does, it is because of UAC (User Account Control). You can adjust this in control panel.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Zaxtor99
8
Years of Service
User Offline
Joined: 27th Jul 2011
Location: Boise, Idaho, USA
Posted: 28th Jul 2011 10:36
Okay thanks.

So I don't need to use a for/next loop to save array "hiscore.att",(10) ???

I was looking at something online that made me think I needed a for/next variable to save every part of that array ie save array "hiscore.att",z (for z=1 to 10)

Secondly, I am not using Win 7 or vista. I am running XP.

Now I fixed the problem by using write and read data loops with a single filename string vs using the arrays.

I was assuming that using load array and save array was all you needed by themselves and that you didn't need to write and read any other files to use the load and save array commands. Maybe I assumed wrong and I also needed to use the write and read commands in conjunction with the save/load array commands??

..Or perhaps it was the saving the arrays in my for/next loop at the end that messed it all up. With the for next loop, it would have saved array "hiscore.att",10 as the last step?

Even though I got it working, I guess I would still like to understand the load/save array commands better. I haven't seen any COMPLETE code that uses arrays to load and save stats. I know how to use the variables in the arrays and how to dim them and all that, and even how to update them once they are loading and need to be updated (such as in a high score top 10 update). I guess I'm just wondering if there is more to saving and loading them then the single load array and save array commands that I might be missing.

Thanks again.


- Zaxx
BN2 Productions
15
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 29th Jul 2011 05:28
Ask and you shall receive :



Important Note: Do NOT load an array into one that is dimensioned differently as this leads to really annoying behavior. For instance:

Loading this array:
TestArray(10)

Into this one:
TestArray2(3,3)

It will probably fill it, but it should be avoided. Also if you have this one:

TestArray(3,2)

It will NOT load evenly into:
TestArray2(3,4)

What WAS (3,1) will probably be somewhere around (2,2) or something. In general, just load an array into it's specific holder. If you are in an instance where the size could change, have the limits saved to another file, then read those numbers first, then load. That's a bit more advanced, though, so you probably won't need to worry about that.

Hope this helps!

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Zaxtor99
8
Years of Service
User Offline
Joined: 27th Jul 2011
Location: Boise, Idaho, USA
Posted: 29th Jul 2011 09:56
ARGH> I was using a Write/Read routine that was successfully saving all my high scores to a single DAT file. it worked for a little over a day, then all of a sudden just stopped working. it stopped reading or writing the DAT file somehow, even though NO CODE HAS BEEN CHANGED!

I tried the program on two other computers (3 total), and it won't save a DAT file on any of them now. So the game will show the high score at the end of the game now, but then when you exit, it won't pull them back up because the is no longer a DAT file. (I deleted ALL of the DAT files with the same filename on my PC, including in the recycle bin to try and resolve it somehow, but now the game won't even make a DAT file anymore.

Makes NO sense how it works perfectly for 30 games then all of a sudden just stopped saving and reading the DAT file.

Here is what I have at the beginning of my program:



- And then at the end of the game I have this which sorts and saves the data after deleting the old file...




I'm pulling my hair out here. I am running windows XP on this PC, and Windows 7 on the other two computers I tried it on, and I clicked the run as admin on the properties as well on the Win 7 computers.

I have tripled checked to code to make sure it isn't skipping it somehow, but it is reading all the data, and printing the players score as the top one now.

..Like I said, it WAS working for about 30 games or so, then just magically stopped with no code changes to the program.

Any help or ideas would be appreciated.. mostly to help me understand what might be wrong and lower my frustration with DarkBasic at the moment.


- Zaxx
BN2 Productions
15
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 30th Jul 2011 07:56 Edited at: 30th Jul 2011 07:56
Hmmm not sure exactly what the issue with the magically broken code, it looks straightforward enough. Here's some tips to help you resolve it:

1) Indent your code. What this means is that every code "structure" (if-thens, for-nexts, do-loops, subroutines, etc) you indent like so:


This is mostly to help make it a bit easier to read and it will help you catch errors when you forget an endif (if you haven't, you will, everyone does eventually).

2).
Quote: "if spot=0 then goto showscores:"
Gotos are usually a big no no for programming (also, I don't believe you need the :, but I'm not 100% sure). The reason they are bad practice is because they make the code hard to follow, debug, and just plain messy. Someone once said that if you've coded yourself into a corner and goto is the only way out, restructure your code to avoid it. If you want some tips, I can give you some pointers. For now, try turning it into a subroutine (it's LIKE using GOTO but you have a RETURN statement at the end of it, it goes AFTER the END command in the program, you call it with GOSUB, and it will return to the line after the GOSUB call once it's done processing). These are nice because they allow you to keep your code modular but you don't end up with spaghetti code.

3) This one might help you find the error. Put an END command within the IF->THENS or FOR->NEXTS that include the OPEN TO WRITE command then run your program, if it closes, then the code has reached it, if not, then your code has skipped it (perhaps a mistyped variable name, forgot a $ or a # sign, or similar). Also, try opening a new file and just make a file, save the data, then close the file and read the data out (make sense?). If it works, copy the code straight into your real program (copy/paste it). Run the code and make sure it is still working (it doesn't have to be highscores, just SOMETHING to show that it can make the file and fill it, then read it back). If it is, adjust the file name and contents and see where the breakdown occurs.

Hope this helps!

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Zaxtor99
8
Years of Service
User Offline
Joined: 27th Jul 2011
Location: Boise, Idaho, USA
Posted: 1st Aug 2011 09:08
Thanks for your help BN2. I resolved it by changing the filename$ at the beginning to a new name. {shrugs}

Anyhow, all is good now. About the indention's and all, yes I can see how that would make a program easier to read and debug. I learned how to make my own games and program back in the early 80s on the Commodore 64 Basic, so I find that DarkBasic is real similar.

Login to post a reply

Server time is: 2019-08-18 04:15:49
Your offset time is: 2019-08-18 04:15:49