Array of objects

Started by
10 comments, last by CrazyCdn 17 years, 9 months ago
Well as discussed in the other thread, unit information and map information are seperate things and should really be maintained seperately until they are drawn.

I stand by my suggestion in the other thread that you use an offscreen buffer to draw your map to, then iterate through your units sequentially and draw them onto the buffer, then draw the buffer to the screen.

Post here or there if the way I explained it (in the other post) does not make sense.
Advertisement
Hey again, and thanks for your reply.
I actually used a mix of your idea and Zahlman's.
I'm using your way of storing and drawing the map (the first one you showed on the other topic) but I am using a vector to store the unit inf which proves to work quite well.
I managed to add a couple of things, such as movement, but I have one problem which might seem totally stupid.

As Zahlman showed, I used a vector to store the unit info:
vector<Unit> UnitLoad() {  fstream fin("units.txt");  vector<Unit> result;  string line;  while (getline(fin, line)) {    istringstream ss(line);    Unit u;    ss >> u;    result.push_back(u);  }  return result;}


Now, the problem is that each time I want to access that data, I access it by calling that function: vector<Unit> Unit = UnitLoad();
So basically each time the info is recovered from the file.
The problem is, if I do movement for example, I am forced to rewrite to the file after each movement, so that I can use the modified coordinates somewhere else (that's what I'm doing right now, it workd quite well, but looks quite dodgy).
Anyway of retrieving the info from that vector without taking it from the file each time?
My idea is to rewrite everything to files only at the end of each turn!
Thanks a lot to all of you ;)
Each turn? That is madness dude. You only want to be loading it at the start of the game, and maintain it in memory, not in a file.

The easiest way to do this would be to have a global vector which you load into at the start, then just use throughout the rest of your code:

vector<Unit> Units;void at_the_start(){    Units=LoadTheUnits();}void during_the_game(){    for(int i=0;i<Units.size();++i) HandleUnit(Units); // just use it}


I should also point out that your current method of loading creates a local vector in the load function, then copies the entire thing into another vector when it returns. If you have a global Units vector, you would be better to rewrite the load function to just load into it directly instead of a temporary:

vector<Unit> Units;bool UnitLoad() {  fstream fin("units.txt");  if(!fin.open()) return false; // note you should be checking this in                                // case the file open fails for some reason  Units.clear(); // not strictly needed if only called once but good practice  string line;  while (getline(fin, line)) {    istringstream ss(line);    Unit u;    ss >> u;    Units.push_back(u);  }  return true;}


People may start shouting at me now for suggesting using a global variable, but if this is a simple project, and the vector is local to just one translation unit, I don't think it would be worth complicating things at this stage.

HTH
Thanks, it works very well.
So you suggest to write info to file only when the user quits the game?
Well anyway thank you, I'm gonna try and get more stuff done like movement, resource collection etc...
Thank You
I have another question.
I can now load and display a map on screen, display units, "select" them, and move them around with a limited number of movements per turn.
Now, I'm going for buildings. I wonder how I should do that. Should I create another text file and access it as I did for the units? Starting with an empty file and writing into it each time a building is created?
Or simply store all building info into a vector and then only in the end output everything to a file?
Since I'm limited (for the moment) to one symbol per tile I don't want to mess with more than one object for each tile.
I guess it depends. If your buildings are dynamic i.e. can be created and destroyed, there might be an argument for storing them in an array like the units.

Then again, if the map became quite large and had a lot of buildings on it, it might be more efficient to have the buildings stored as part of the map, since you could just examine the map directly for collision detection etc rather than having to iterate through the list.

I'd just do whatever feels the most natural for the type of program you are writing. You seem to be doing fine so far.
As a topic about being introduced to arrays, this thread is probably more appropriate in For Beginners, so I'll move it there.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
This is also a prime candidate for std::vector too.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

Quote:Original post by EasilyConfused
I guess it depends. If your buildings are dynamic i.e. can be created and destroyed, there might be an argument for storing them in an array like the units.

Then again, if the map became quite large and had a lot of buildings on it, it might be more efficient to have the buildings stored as part of the map, since you could just examine the map directly for collision detection etc rather than having to iterate through the list.

I'd just do whatever feels the most natural for the type of program you are writing. You seem to be doing fine so far.


Hmmm but if I want to store the building info in the map, I should look for another way to store the map right?
Cos for the moment I can't have more than one object per tile the way the map is stored.
Anyway I think I'm gonna stick to a 5*5 map for the moment and implement all the stuff in there before going more graphical (which should make some things easier to show on screen rather than going through symbols and stuff).
But I want to get the game logic right under simple text mode before moving to graphics stuff.
What do you think?

This topic is closed to new replies.

Advertisement