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 / Writing Array to a File

Author
Message
D a r k
22
Years of Service
User Offline
Joined: 21st Jun 2004
Location: In ur fridge, eatin ur foodz.
Posted: 22nd Oct 2007 03:34
I am trying to read a map file so I can load the tiles to my grid accordingly. But I seem to get into trouble when writing it. (The problem could be when I read the file but I dout it.)

Here is my code to write the map data (Stored in more than one array)



The problem I am having is that it seems to write in only the first four data spots.

So when I read I get something like this:

40
133
10
12
Then 846 0's

And the data is always of the last tile placed. I want it to write each set of numbers, not just overwrite the first four over and over again. How can I do this. Or, is there a better way of going about this?
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 22nd Oct 2007 18:05 Edited at: 22nd Oct 2007 18:06
Hello,

Even though it's hard to tell from your post whether you are dealing with a 3d matrix or a custom 2d grid, the method of approach I'll demonstrate should still be applicable.

One thing that stuck out immediately when I looked at your code was that you were writing BYTES. This is fine as long as the values are between 0 and 255. If your x and y are in screen coordinates, then 255 (a byte) is generally not going to be a high enough number, the same goes for an image number (up to 65535) and your tile numbers which your loop suggests goes up to 850. Now if this was 3d, then x and y could be float values (ex 123.456) which also wouldn't work when writing only a BYTE to a file.

Again, without knowing the format or the types of your data, I can only make some assumptions. First thing, is to make sure you are writing the same data type to the file as your program uses. If your x and y are float values, write a FLOAT to the file. If your image number is larger than 255, then write that as a WORD to your file. Here's an example. I assume that x and y are the number of tiles across and up so I'll leave that data type as a byte (you could have a grid size of 255 x 255). I assume the max image number is 65535 and that the maximum number of tiles is 255 * 255 so they can both be written as WORDs.

The example only has ImgNo() stored as an array and the other values are just determined by the loop. This is just one way of doing it:



When you read the data back, you have to read it with the same data types, but you can assign the values to whatever arrays or variables you want.

Enjoy your day.
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 23rd Oct 2007 19:18
Haven't read through Latch's answer so sorry if you've already said this.
Arrays are perfect for maps, because they themselves have coordinates you can use. You should only have one array to store your entire map, like this: map(x,y).
If you are only storing the image used for the tile then you don't need to specify a 3rd dimension, you can store your image data in the appropriate coordinates like this: map(1,5)=3, so now the tile at 1,5 will use image 3.
If you want to store more than one piece of data on each tile, you'll need a 3rd dimension.

Lets say that this is a strategy game and we want to store how much stone, wood and food each tile contains; We'd do this: DIM map(100,100,3). We will use map(x,y,0) to store the image for the tile, map(x,y,1) to store the amount of stone, map(x,y,2) to store the amount of wood and map(x,y,3) to store the amount of food.

Bluestar4
20
Years of Service
User Offline
Joined: 19th Dec 2005
Location: USA
Posted: 1st Dec 2007 20:53
I recomend after studying up on arrays in the manual that you use the save array command - its a very nice command that will allow you to save your arrays with only one line. Loading the arrays back are just as simple using the "load array" command. These commands also work with multi-dimension arrays as well. The only thing you have to remember when using arrays is that arrays in db are zero based like in c++ - the first element is 0 and the last you dont want to write to or it will corrupt any data.

TDK
Retired Moderator
23
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 1st Dec 2007 21:51
There are two down-sides to using Save Array (ignoring the fact that in the past it has occasionally failed to work for me).

The first is that you can only save a single array in a file. If you have multiple arrays and other data, you can't save them in a single file.

The second is that other than using Load Array, you can't easily check what you have written to a file. If Load Array doesn't work then there's no way of knowing whether the Save or the Load part is the problem.

I would suggest that at first, you use the same method you are using, but use WRITE STRING for every piece of data.

You can then open up the file with Windows Notepad and check to see if the data matches with what your loading routine expects to be there. At the moment, I tend to agree with what latch said.

So, your save routine would be:



As an aside, I would be tempted to instead use:



but the end result is the same - it's just a matter of preference. The only advantage is that all the data of the same type is written together in blocks.

You can then add comments before each block stating what the block of data is:



OK, it's longer, but if it works or helps solve the problem...

To read the file back in you would use something like:



Once it's all working, you can remove the comments.

To be honest, I load and save all my data files this way as it's no slower and much quicker to track down any problems which occur.

TDK_Man

Bluestar4
20
Years of Service
User Offline
Joined: 19th Dec 2005
Location: USA
Posted: 2nd Dec 2007 01:55
@ tdk
I have never had load or save array commands fail except when I mistyped something.

now , as tdk pointed out, there are downsides , but there are equally good reasons to use this command - firstly as tdk pointed out , you can only save one array to a file. This array can be as big or little as you wish and at your discretion can also be a multi-dimentional array. since each file would only contain one array it makes good for debugging as there is no onscreen variable debugger in dbc - in short , it allows you to deal with one arrray per a file at a time. The second advantage is that its one line ,not 5,10 or 20. that said, I now point out that both ways to save imformation are equally good so long as you as the programmer are comfortable with it. There may be cases when you want to only save an array in which case this would be the ultimate command, however, there may be times when you want to store more than just the array , but all kinds of data and you want it all in one file. In this case, you wouldn't want to use the save array command. In any event , I hope that tdk & my responses to your questions have in some way helped you to be a better programmer.

TrueDB
18
Years of Service
User Offline
Joined: 13th Nov 2007
Location:
Posted: 30th Dec 2007 03:56
if you use save array, then save array again to another file you have 2 temp files, then write a little code to join them into a perminent filesave(multiarray) then delete temp arrays

Now write the reverse code to unjoin them and load 2 temp arrays, then delete temp arrays.

ok hope it helps

MAIN PROGRAM http://www.thewargamer.com/grognard/avalompro.zip GAME MODULES http://www.thewargamer.com/grognard/avittybitty.zip http://www.thewargamer.com/grognard/avalamo.zip
Pixelator
18
Years of Service
User Offline
Joined: 8th Jul 2007
Location: here
Posted: 31st Dec 2007 04:43
one last problem to add to the LONG list of problems for save and load array - you can only save in .txt files!!!


Maryville Game Developers
Visit our website at http://www.freewebs.com/maryville-game-developers/index.htm
Bluestar4
20
Years of Service
User Offline
Joined: 19th Dec 2005
Location: USA
Posted: 31st Dec 2007 09:02
@pixelator
nope - save to .dat i.e....


save array "positions.dat",positions#(110)
save array "names.dat",names$(17)
save array "hitpoints.dat",health(17)

Login to post a reply

Server time is: 2026-07-05 12:33:32
Your offset time is: 2026-07-05 12:33:32