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 Professional Discussion / Saving the contents of an array

Author
Message
FieldDoc
23
Years of Service
User Offline
Joined: 30th May 2003
Location: London, UK
Posted: 15th Jun 2003 23:33
I have an array (500, 500) and I need to be able to save it to a save game file of some sort. How do I go about this (I know nothing of save games!) and how would I load it at the start of the game? The array contains strings
Life is like a penis:
When it's soft you can't beat it, when it's hard you get screwed.
Shadow
23
Years of Service
User Offline
Joined: 17th Oct 2002
Location: In the shadows
Posted: 15th Jun 2003 23:54 Edited at: 16th Jun 2003 19:17
use save array Filename$, array_name(0)

(doh!)
FieldDoc
23
Years of Service
User Offline
Joined: 30th May 2003
Location: London, UK
Posted: 16th Jun 2003 02:00
Thanks for the speedy reply Shadow.
Where does that save the info to? How would I retrieve it the next time I play?

Life is like a penis:
When it's soft you can't beat it, when it's hard you get screwed.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 16th Jun 2003 02:05
It's SAVE ARRAY filename$, arrayname()
FieldDoc
23
Years of Service
User Offline
Joined: 30th May 2003
Location: London, UK
Posted: 16th Jun 2003 10:57
Cheers IanM....so to load the contents of the array when the player 'loads' a new game...would I use:
LOAD ARRAY filename$, arrayname() ?
Can you only save one array to a file?

Life is like a penis:
When it's soft you can't beat it, when it's hard you get screwed.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 16th Jun 2003 13:32
Yes
FieldDoc
23
Years of Service
User Offline
Joined: 30th May 2003
Location: London, UK
Posted: 16th Jun 2003 16:22 Edited at: 16th Jun 2003 16:23
Thanks. Does that mean that most people's save games when created using DB will consist of multiple files? I figured save game files were usually just a single file

Can I also take this oppurtunity to ask you about your dll for DBPro. I can't figure out the INSTR function. Does it return a 1 if the search string is present in the source string and a 0 if it is not? Whe I put in in my code...it said something like the variable INSTR was not defined?

Life is like a penis:
When it's soft you can't beat it, when it's hard you get screwed.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 16th Jun 2003 18:03
Most people would probably design their own file format for saved games. The problem with SAVE ARRAY is that you can only have one type of data in the array you are saving, and most games have a varying amount of integer, floats and strings.

Nice to see you're trying my plugin

The problem you are having sounds like you have not put the DLL into the correct folder. The location (assuming you install DBPro to the default place) is : C:\Program Files\Dark Basic Software\Dark Basic Professional\Compiler\plugins-user

Instr returns the position that it located the search string at, or 0 if it dod not find a match.
Matto
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location:
Posted: 17th Jun 2003 04:18
So taking into account that most people would need to save multiple types of data, IE; Strings, Integ and Real Numbers, would you be able to give a good starting point to look at please with regards to saving various things at once and being able to load these?

Would there be a problem with saving multiple arrays of various kinds with the save array command and then having a load section that initialises or checks for saved files before loading other parts of the game or on command(in-game menu based command)?

Thanks

AMD XP 2500+,512 DDR RAM 2700, GF FX 5600, 256DDR,SBLive 5.1,Win XP
Best Upcoming MMOG - http://WWW.Atriarch.com
Project: Cluckie Egg
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 17th Jun 2003 05:44
Here's a quick snippet. It draws a coloured box, saves it to a file using various variable types, then loads it back and displays some information extracted from the file.

There are a few other ways to do this as well:

You could keep an array for each type of data (like you've already suggested).
You could maintain the mixed data in a memblock and load/save the memblock in a single operation (read memblock/write memblock).
FieldDoc
23
Years of Service
User Offline
Joined: 30th May 2003
Location: London, UK
Posted: 17th Jun 2003 11:01
I have a few more questions IanM, since you're on a roll.

1. Does the file you save to have to end in .dat or can it have any extension (perhaps a cutoms one for your game)?

2. Why do you have to delete a file before you save it?

3. The information that I need to save in my game is quite large...it willc consist of several arrays of different data types. You said earlier that I could save each array to a separate file, but that would mean potentially having 30 files per save game! In this instance, would using the memblock idea be the best approach? For instance, should I create a memblock at the point of saving, copy all the arrays to it (can you, how???!!) and then save the memblock to a file. Would it then be possible to copy the contents of the loaded memblock into the respective arrays come loading? Lastly, how do I determine the size of the memblock I would need? I would have to somehow find out how many bytes were in all the arrays?

Life is like a penis:
When it's soft you can't beat it, when it's hard you get screwed.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 17th Jun 2003 14:20
1. You can call the file anything you want.

2. Try it and you'll see It should only take a few lines of code to see.

3. I said that you *could* use a memblock, not should. If you are already storing your data in arrays, there's no point in switching now unless you have a really good reason (maybe make it harder to crack your saved games?)

A suggestion for the file format for you:

a. The array size
b. All the values in that array in order

Repeat a to b for each array before closing the file

Maybe (initially) save your games completely in string format - that way you'll be able to read what has been written and maybe even modify it in notepad.

Later, maybe you can get a little more clever with the file format, but I'd leave that until you've got everything else working correctly first.
Bloodshot
23
Years of Service
User Offline
Joined: 7th Apr 2003
Location: United Kingdom
Posted: 18th Jun 2003 16:16 Edited at: 20th Jun 2003 20:14
You could create your own arrays using the MemBlock commands.

Example: (Updated: {Last Update 17:05 GMT - 20/06/2003} See post below)
FieldDoc
23
Years of Service
User Offline
Joined: 30th May 2003
Location: London, UK
Posted: 19th Jun 2003 16:29
Thankyou bloodshot...wonderful code snippet! Many of my arrays are 2d so i decided that when the user wants to save, a new 3d array is created and the contents of each each 2d array (i like the page analogy) are copied into the 3d array (with each 2d array being a 'page'). Hopefully it should work. I can then just save the 1 large 3d array to a file

Life is like a penis:
When it's soft you can't beat it, when it's hard you get screwed.
Bloodshot
23
Years of Service
User Offline
Joined: 7th Apr 2003
Location: United Kingdom
Posted: 20th Jun 2003 17:35 Edited at: 20th Jun 2003 20:12
UPDATE!

I've edited my above post and added a couple of extra lines of code to the Code Snippet example to prevent an error occuring when writing the file:

Rem This will simply delete the test file if it already exists
If File Exist(FILENAME$)
Delete File FILENAME$
EndIf


Apparently you will get an error when using the "Open to Write" command if the file already exists. So, you would have received an error code if you ran my example code more than once.

I've also incorporated the Read File command into the example too and a "FAST" switch to disable the loop that prints out the contents of the array to screen, incase you want to test a much larger array size than the one provided. (i.e. devilman's XMAX=1000 : YMAX=25 )


Here it is again: (Last Update 17:05 GMT - 20/06/2003)
Guyon
23
Years of Service
User Offline
Joined: 6th Jun 2003
Location: United States
Posted: 21st Jun 2003 06:38
Bloodshot, First let me say that is a nice piece of code. Very well document and easy to real. You are good.

While I do not fully understand the Read and Write MemBlock, it seems to me that the
save array Filename$, array_name(0)
command is much easier to implement as your example just saves byte integers. So you end up with four times the code to do the same thing.

Did I miss something? Is MemBlock that much better? If so why?
Thanks

Login to post a reply

Server time is: 2026-07-12 00:48:22
Your offset time is: 2026-07-12 00:48:22