Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I need to store which spells a character knows. While the game is executing I have them stored in a vector. Obviously storing this many values is either going to take a lot of computing to get the file size smaller or simply a large file size.

My best bet was to convert the vector into a single long int variable to store in the file. This will work out fine until there are too many spells to contain in a single variable.

I could split it up into multiple values but I was just wondering if there was a better way to accomplish that or if I should simply store the full list (strings, ints, etc.) in a text file and deal with the slightly large file size.

I'm using c++.

share|improve this question
2  
"going to take a lot of computing"... How many spells we talking about here? –  Byte56 May 17 '13 at 0:23
    
Honestly no idea at this point. I just don't want to start off with a method that is going to require an exponential amount of work to keep going. I'm not sure what it's actually called but when you sum all the values after 2 has been raised to that power. Like if the array contains 1, 2, and 3 it would be 2^1(2) + 2^2(4) + 2^3(8) which would result in 14 and could be later broken down into the originals. That would be quick to overflow but would save on space. Saving each to a file individually would be easy to read in but would result in large space. Hopefully that got the point across. –  Kethaias May 17 '13 at 0:31
    
Like if the array contains 1, 2, and 3 it would be 2^1(2) + 2^2(4) + 2^3(8) which would result in 14.... Uh... What?? Not even close to 14... Oh you're not multiplying by the number in the parenthesis. Makes sense now. Either way, what does this accomplish? –  Brendan May 17 '13 at 0:59
2  
Saving 10,000 spells wouldn't even take up that much space. I think you're drastically overestimating the amount of space required or drastically underestimating the power of a modern processor. Don't prematurely optimize. Code it up in a way that makes sense to you can come back if there actually is a performance issue. –  Byte56 May 17 '13 at 1:02
    
I had a feeling someone would think that. But yes you are correct. That number will reach a very high number very quickly. Maximum possible storage of about 63 spells using a unsigned long long. –  Kethaias May 17 '13 at 1:04

1 Answer 1

up vote 2 down vote accepted

If storage size is your main concern, using vector<bool> is probably actually your best bet. This is because vector<bool> is optimized to use one bit per bool instead of one byte (see reference here). From there for file read/write just make sure to write the capacity of the vector<bool> to file then use the same one bit per bool/spell (there's probably a good way to do this as a block).

If you go this route you can check if a spell is known just by knowing it's index in the vector, which will just be a matter of storing an unsigned variable of sufficient size. The vector operation should handle the bitmasking to get that one bit for you.

  • char: 255
  • unsigned short: 65535
  • unsigned int (32 bit): 4294967295
  • unsigned int (64 bit): 18446744073709551615

Really if you find yourself needing over an unsigned short for spell indexes you're probably going to find yourself running into other problems, but at very least this part of the system will expand fairly easily.

share|improve this answer
    
I think you and byte56 really hit the nail on the head. I was so worried about optimizing that I was making things way too complicated. I thought storing say 1,000 values in a file impact performance would take forever to actually read in. Upon testing it's not all that bad. Since it only has to be done once per character it should cause a huge performance issue unless the number of characters loaded gets huge. At that point I could just optimize the number of characters loaded instead. Thanks for all the help. I really just could not think of a good way to accomplish what I wanted to. –  Kethaias May 17 '13 at 1:09
    
Just to clarify I want to store these values in a .txt file or something similar for loading in later on. Some answers seemed to point towards storing them during run time. I had no issue with that and was already using a vector of bools. –  Kethaias May 17 '13 at 1:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.