Read entire file in vs read as you go

Started by
15 comments, last by deadlydog 17 years, 1 month ago
Hey, just a quick question. Right now I am making a 2D starfox type of game where enemies come from the top of the screen down, and I am using a txt file to hold the level information, which is basically just a text file with a list of enemies, what time they should appear at, and where they should be positioned on the screen. Now, as I am currently doing it, I am reading the entire file before the level starts and storing the information in an enemy queue, sorted by the time they should appear at, then each frame I just check if it's time to add an enemy or not. I was thinking that it would be more beneficial to just read the script file as I went, and just not using an enemy queue. This way I could store extra commands in the script file, and just use a switch statement to perform the appropriate actions based on what command was given. This way I could do other things, such as storing when items should appear, when something in the level should change, or whatever. Now, I'm just wondering if this would cause any problems or if there are any downsides to this method over my original, since the txt file would either need to be kept open for the entire duration of the level (it's read-only, but I'm not sure if keeping it open would cause problems or not), so that I wouldn't lose my position within the file, or I would have to save my position within the file and open and close the txt file every frame (which could be very costly). Which method would be best to use? I just want to make sure there's not some big "no no" that I would be doing. Thanks. Dan
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA
Advertisement
Id probably load the whole file one time, interpret the level/script, and save the interpreted commands into a big array or some other internal level structure in the program's stack. Not keep the file open for the duration; that sounds... problematic, suppose you get a read error, how do you handle it without interrupting the game?.

Quote:Original post by Anonymous Poster
Id probably load the whole file one time, interpret the level/script, and save the interpreted commands into a big array or some other internal level structure in the program's stack. Not keep the file open for the duration; that sounds... problematic, suppose you get a read error, how do you handle it without interrupting the game?.


Yeah, this is pretty much what I'm doing now. Keeping the file open for the duration would definitely be the easiest to do though. Good point with the read error, but right now I do not have any read error handling anyways so whether the error occurred before the level started or during the level wouldn't make much of a difference. Either way I would likely just display the error in a msg window and exit the game. Anyone else have any comments or see any other problems with this method? Thanks.
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA
I would put the whole file in a string array and read from there. That doesnt cost too much memory, does it?
-----------------------------------"After you finish the first 90% of a project, you have to finish the other 90%." - Michael Abrashstickman.hu <=my game (please tell me your opinion about it)
Quote:Original post by Gagyi
I would put the whole file in a string array and read from there. That doesnt cost too much memory, does it?


That would depend entirely on the size of the file.
File IO is usually not very fast, it could slow your game down if reading as the game is running. However, you could make a stream of some sort so the read position is ahead of the game, that way the game is not waiting on the file io to read what's coming up.
Quote:Original post by tstrimp
Quote:Original post by Gagyi
I would put the whole file in a string array and read from there. That doesnt cost too much memory, does it?


That would depend entirely on the size of the file.


Hmmm, this idea is interesting. And while it would take a little bit more memory, I can't see it taking too much more than I'm using right now, since currently I am storing all of the information in a big list of structures. Does anybody see any drawbacks to this method? or can you think of any other methods that might be better? Oh, and right now my level files are around 500 - 750 words long, but that could increase once I'm able to store other things in the file besides just which enemies should appear when, but it should stay under 1000 words (by words I mostly mean 1-6 digit numbers).

I assumed that this would be a fairly common problem, as lots of games use scripts to read in game info, so I thought there would be a common, well known solution/method to this problem. Thanks for the replies so far everyone!
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA
Quote:Original post by deadlydog
Hmmm, this idea is interesting. And while it would take a little bit more memory, I can't see it taking too much more than I'm using right now, since currently I am storing all of the information in a big list of structures. Does anybody see any drawbacks to this method?


Your current approach of storing the script information in special structures is better than storying the literal file strings in a giant array.

For one, as you noticed, its less memory. For another, the literal strings need to be interpreted so they can be used in the game. While your structures method (hopefully) is designed to pre-process all that work and instead is adapted for direct use in gameplay (ie, the structs contain numeric values, function pters, etc, stuff that will directly be accessed in your map).


It sounds like what you were already doing is correct...
Quote:Original post by Anonymous Poster
It sounds like what you were already doing is correct...

Seconded.
First, abstract the problem away. A Raw_Level is a stream of lines. A Processed_Level is a stream of processed level lines.

Then you can implement a simple and easy Raw_Level and Processed_Level implementation -- whichever is easiest and still correct -- and worry about what kind of buffering you should do later.

This topic is closed to new replies.

Advertisement