Jump to content
  • Advertisement
Sign in to follow this  
deadlydog

Read entire file in vs read as you go

This topic is 4211 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Guest 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?.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I would put the whole file in a string array and read from there. That doesnt cost too much memory, does it?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!