Loading An Array

Started by
12 comments, last by Bezzant 23 years, 2 months ago
Hi all, Im making a tile based rpg and I want to be able to load the array that stores the tile info from a text file that is created with my level editor, This has been causing me problems for ages and I can''t get it to work. Could anybody help me out? My level editor saves the data like this: { {4,4,4,8,7,4,4,4,4,4}, {3,3,4,8,7,4,1,1,6,4}, {3,3,4,8,0,4,6,1,1,4}, {2,2,4,8,7,4,1,1,1,4}, {2,2,4,8,7,4,1,1,6,4}, {6,2,4,8,7,4,6,1,1,4}, {6,2,4,8,7,4,1,1,1,4}, {6,2,4,8,7,4,1,1,6,4}, {2,2,4,8,7,4,6,1,1,4}, }; Any help would be very helpful. Many Thanks Alan IF YA SMELL... WHAT THE BEZZ IS COOKIN'' Check Out My Site #Programming
Advertisement
Hi.

Presumably your writing the array out to file in binary mode using somthing like this

// write the X size of the map
file.write(&lngXSize, sizeof(lngXSize));
// write the Y size of the map
file.write(&lngYSize, sizeof(lngYSize));
// write the array out
file.write(array, sizeof(array));

For this sort of 2d map lngXSize * lngYSize == sizeof(array) / sizeof(array[0])

You can thus read it back in by doing this

// read in the X size of the map
file.read(&lngXSize, sizeof(lngXSize));
// read in the Y size of the map
file.read(&lngYSize, sizeof(lngYSize));
// read in the array
file.read(array, lngXSize * lngYSize * sizeof(array[0]));

Hope this helps

Pete
Sorry. That doesn''t help. I have written the level editor in VB it just creates a normal text file that contains the info for the tiles. I can read a file in a console app by using the fstream header file to read and display the file. Is there a way in which this method can be used in my opengl game. Heres how I load a text file in a dos window:

#include
main()
{
char ch;
char filename[20]="a:level.txt"; //location on disk
int mode = ios::in;
fstream fin( filename, mode);
if(!fin)
cout<<"Unable to open file!";
while (fin.get(ch))
{
cout< }
fin.close();
return(0);
}

Is there a way to use this method to load a text file as the array?

Many Thanks

Alan

IF YA SMELL... WHAT THE BEZZ IS COOKIN''

Check Out My Site #Programming

Oh, ok. I think I get you.

Which file statements are you using in VB? Are you wirting binary data or does the array come out a a string of numbers? Can you post the VB file code?
void Map::load(string filename)
{
int mode = ios::in;
fstream fin( filename, mode);
if(!fin)
logError("Unable to open file!");

// in your global section, or class
// you should have an array type thing,
// i am calling it m_array
m_array.initialize(width, height);
int count1=0;
int count2=0
while (fin.get(ch))
{
if(!(ch==''{''||ch==''}''||ch=='','')){
m_array[count1,count2]=ch;
if(++count2>width){
count1++;count2=0;
}
}
fin.close();
}


AHHHHHHHHHHHH, headaches!! Ouch, I hate array''s.

well basically, filter out the ''{ }'' brackets and , commas,
keep track of the width and height of your map, and fill up your array with only the data.

Sorry if that made little sense.

PS, if there is a bug in my code, sorry, but that is the general idea of things.
type entity[dynamic cmd_interface: cmd c, object o=0 [ if:c call_remote[c,o]; ]void;]
You mean that

{
{4,4,4,8,7,4,4,4,4,4},
{3,3,4,8,7,4,1,1,6,4},
{3,3,4,8,0,4,6,1,1,4},
{2,2,4,8,7,4,1,1,1,4},
{2,2,4,8,7,4,1,1,6,4},
{6,2,4,8,7,4,6,1,1,4},
{6,2,4,8,7,4,1,1,1,4},
{6,2,4,8,7,4,1,1,6,4},
{2,2,4,8,7,4,6,1,1,4},
};

is how the data is saved to the file? Including all the brackets and commas?

Now thats what I call making life hard for yourself! haha.

In this case, do what archetype said. (Or just write the data out in binary form. Nice and easy to read it back in then!)

Pete
Thanks everyone,
I''ll have a good look at all the good provided. It seems as though a text file was not the best choice so I have just made a quick program to convert the text files my VB level editor makes and this lil'' program converts them to binary for me, so that will make things easier. I''ll try testing out everything tommorrow....I need sleep.

1 quick question. When I write the binary file should I leave out all the comma''s and braces?

Many Thanks For The Help

Alan

IF YA SMELL... WHAT THE BEZZ IS COOKIN''

Check Out My Site #Programming

yes in binary u normally leave out the commas

personally im a great believer in txt files i try to use them as much as possible they save u so much coding time (very important when youve developing someit)

if i was u ill stick to using the txt format

http://members.xoom.com/myBollux
Hi again.

I know that the binary vs Text file debate has been done to death but I have to ask some questions.

Zedzeek : You say that using textfiles saves you coding time. How? The code to utilise a specifically formatted text file could be fairly simple but the power of text files come from using a Property=Value like format. You query the file class for a value and it parses the file for the value associated with that Property. This is how ini files work.

The code to write this (without using os functions) is much more obese that a nice standard binary file. The same can be said for a binary file.

Which is easier to code, a text file parser using fscanf or a set of ordered binary reads from the file? I say the second option.

Which is easier to change, or to eyeball? The text file and this does have great advantages while debugging.

This brings me to another point. Debugging of a file loader should be fairly simple. Once its working, you dont care what format your files are in. So why not write it in the most simple way.

Also, reading binary files is generally faster than parsing a text file.

Just my opinion

Pete
Why not have a combination text/binary parser. It recognises which so you can use text files for development (easy to mess with) then change these to binary files for release (faster to load).
You only have to write the parser once, and then you can re-use the same code.
Gee Brain, what we gonna do tonight?

This topic is closed to new replies.

Advertisement