speed question: "if"s or arrays? (SOLVED)

Started by EnterTheStory (aka tolworthy), Mon 28/04/2008 11:19:53

Previous topic - Next topic

EnterTheStory (aka tolworthy)

I need to chop up a very long text (several hundred pages) into thousands of strings. Each string is assigned two numbers for search purposes. Which is the fastest way to do this?

Make a huge array of strings? This feels most elegant, but the array would need to be filled when the game started, which might add a delay.

Or make a huge function with a lot of "if" statements? This makes messier code and feels clunky.

And I take it there are no limits to string length (except the AGS editor 500 character limit) and arrays can have several thousand items?

GarageGothic

Filling a large array with text wouldn't take more than 10 seconds at most, I think. I had a function which read in a short story from a .txt file, and it managed to do it every cycle without a framerate drop (though I eventually optimized the function to only read/draw the text once). You could even code a progress bar to show the user that the computer hasn't frozen.
However, a large array will take up memory space and increase the size (and save/restore time) of the player's savegames. It may also be possible for the user to read spoiler-filled cleartext from the game if they open a savegame in notepad (I often do this as a means of cheating if I'm stuck in a game).

Personally I use one long if/else sequence for my custom dialog system, but that's mainly because I can easily run other code from within the dialog. It doesn't need to be messy if you structure it properly, and in my opinion it's easier to troubleshoot - and less prone to cause problems in the first place, than trying to keep track of a huge bunch of array indexes.

But if your main concern is the speed, you shouldn't let that keep you from using arrays.

EnterTheStory (aka tolworthy)

Quote from: GarageGothic on Mon 28/04/2008 12:39:57However, a large array will take up memory space and increase the size (and save/restore time) of the player's savegames.
I hadn't thought of that. Good point. I hate it when games have unnecessarily bloated save files. So "ifs" it is then. Thanks!

SMF spam blocked by CleanTalk