0

I realise the question isn't very clear so I will expand what I mean (if anyone can think of a better way to phrase it then that would be greatly appreciated).

I have a particular class type (Attribute) that any instance of another class (Character) will need several of (e.g. if there are Strength, Intelligence and Luck in the game then each Character will need 3 instances of the Attribute class).

The information for each of the Attributes is stored in AttributesList.txt.

Is it better to read this file once at the beginning of the program to create a global const vector<Attributes> and have each Character create a copy of the vector of for their own use for as long as they exist, or to read the file every time a Character is created to create their Attributes?

Note: creating one instance of each Attribute and using pointers to reference it where needed isn't an option since each Character's Attributes have different values.

Kvothe
  • 1,819
  • 2
  • 23
  • 37
  • Given the disk speed and memory capacity of modern computers, I think either approach will work fine. Also note that the filesystem will likely cache the contents of the file in RAM the first time you read it, so the second and subsequent times your read the file, you're probably going to be just "reading" in-RAM data anyway, not waiting for the disk drive. Given that, I say go with whichever approach you feel more comfortable with. – Jeremy Friesner Jan 19 '15 at 00:34
  • 1
    Smells like you need a database, either light or heavy. If you don't need one now, you will need one soon. Don't create your own, but use existing ones. Check out this question on when to use a database:http://stackoverflow.com/questions/2648802/at-what-point-is-it-worth-using-a-database/2649916#2649916 – Thomas Matthews Jan 19 '15 at 00:36
  • Let's say you have 10 attributes of int size and 10 characters. That could be 400 bytes. Is it really worth worrying about that? I've assumed you won't have tons of characters at the same time. – Neil Kirk Jan 19 '15 at 01:53
  • @ThomasMatthews When I started this I had intended to use a database (for many of the reasons listed in the question you linked). However, I have a few friends who want to try this program when it is finished and it appeared that any database option would require them to install the database software on their computer. Is it possible to package the database with the final .exe file so they don't have to have anything extra on their computer other than the program I'm making? – Kvothe Jan 19 '15 at 18:31
  • Packaging depends on the "installer" application you use. You could also provide your friends with instructions on how to download and install the database. – Thomas Matthews Jan 19 '15 at 18:53

3 Answers3

2

The answer to your question highly depends on your code. How large is this vector? How often do you need to get a copy of it? How large is the memory used by your application in general?

For example, if the size of this vector is a few percents of your total memory footprint and you copy it frequently then I will choose speed over memory footprint optimization (getting a copy of an object in memory is much faster than all the IO overhead you would need to read the get it from disk).

While intuition can help you decide with which to go. The definitive answer is by testing each case and profiling.

Mustafa
  • 1,814
  • 3
  • 17
  • 25
2

This is one of those situations where you should evaluate the trade off between "what's easier to code?" vs "will it really make a difference?".

One one hand, since I/O is typically "slow" (compared to memory accesses) and a source of errors/exceptions, reading once when your game starts is not a bad idea.

However, if it's easier to code and there is little risk to the AttributesList.txt file getting corrupted, and it doesn't generate any measurable performance issues, re-reading the file each time an instance of your character class might work as well. How often does a new character get created?

When I worked in a game studio, we had compiled object resources saved (serialized) on disk. Each time a new game object was created, we'd re-read the struct from file. I think we did this as an async task off the main game/render thread so it wouldn't impact performance.

selbie
  • 100,020
  • 15
  • 103
  • 173
1

The question should be, "how often do I update or access the attributes?"

In many games (such as Wii and Playstation), they update the memory version of the attributes, then provide a command for saving the attributes to a file.

If the attributes do not occupy a gigantic amount of memory, then keep them in memory. This will speed up access times whenever your program is accessing them. Anything external to your program will take more time to fetch.

Remember, profile before optimizing. Also, get the program working correctly and robust before considering profiling.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • Thanks, that's a great point. Worrying about this stuff now was bogging me down, I was just trying to avoid making a load of edits later down the line. – Kvothe Jan 19 '15 at 18:32