Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Proper management with class objects  (Read 2045 times)

0 Members and 1 Guest are viewing this topic.

noct

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • nctdev.pl
Proper management with class objects
« on: June 05, 2015, 10:41:06 pm »
Hello there,
I've got a little problem with vectors containing class objects.

I want to create two std::vectors containing:
1. objects of class:
class Mobs_normal
{
public:

int id;
string name;
int lvl;
int dmg_min;
int dmg_max;
int hp;
int critChance;
};

Which would be a kind of mob templates.

2. objects of class:
class Entity
{
public:
vector<Mobs_normal> mobsNormal;

void load()
{
// data base
fstream file_mobs_normal;
file_mobs_normal.open("entity/mobs_normal.txt", ios::in );

if (!file_mobs_normal.good() )  {error("mobs_normal.txt"); } // 404

// mobs were an int array, but i want them to be vectors
for (int i=0; i < 11; i++ )
    {
 
       mobsNormal.name.push_back() // ?
       file_mobs_normal >> mobsStack[i].name;
       file_mobs_normal >> mobsStack[i].lvl;
       file_mobs_normal >> mobsStack[i].dmg_min;
       file_mobs_normal >> mobsStack[i].dmg_max;
       file_mobs_normal >> mobsStack[i].hp;
       file_mobs_normal >> mobsStack[i].critChance;
    }
}

void drawMobs(sf::RenderWindow & okno, Textures & cTex)
{
  // temp drawing
  for (int i=0; i < cTex.sprEntity.size(); i++)
  {
   cTex.sprEntity[ i ].setPosition((1+i)*50, 50);
   okno.draw(cTex.sprEntity[ i ] );
  }
}

void enableAiMovement(sf::RenderWindow & okno, Textures & cTex)
{
// moving objects on map and switches for "do something" methods
}

};

Which would be templates handling ai of objects drawn at the time on your screen, containing their movement and other actions on map.

I wonder if it's the best solution for this problem.
How should I create a vector containing all information of objects on map (to interact with Player)?
One for all those stats (id, name, lvl, hp) or one for every single stat? I would connect them with "id" iterator.
I want to achieve a quick access to those stats and sprite drawing methods by their int id;

Attachment: database with stats

noct

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • nctdev.pl
Re: Proper management with class objects
« Reply #1 on: June 08, 2015, 07:49:57 pm »
Solved.

// vector of class objects

vector<Mobs_normal> mobsNormal;

// loading data
Mobs_normal newMob;
       mobsNormal.push_back(newMob);
       file_mobs_normal >> mobsNormal[i].name;
       file_mobs_normal >> mobsNormal[i].lvl;
//and so on

// e. g. use in main function

cout << cEntity.mobsNormal[ i ].name << endl;
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Proper management with class objects
« Reply #2 on: June 08, 2015, 11:30:31 pm »
If you want to access the recently 'pushed back' element, it can be much safer to use vector.back() than vector[i].
back() accesses the last element - the previously pushed back element.
e.g.
Mobs_normal newMob;
mobsNormal.push_back(newMob);
file_mobs_normal >> mobsNormal.back().name;
file_mobs_normal >> mobsNormal.back().lvl;

Another option would be to modify all the 'fields' of the temporary storage (newMob) and then push it back fully formed.
e.g.
Mobs_normal newMob;
file_mobs_normal >> newMob.name;
file_mobs_normal >> newMob.lvl;
mobsNormal.push_back(newMob);
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

noct

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • nctdev.pl
Re: Proper management with class objects
« Reply #3 on: June 09, 2015, 12:30:34 pm »
I was wondering if the second option of yours wouldn't be better. Could you tell me why it is, or why isn't? This function is used once, to load all data.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Proper management with class objects
« Reply #4 on: June 10, 2015, 09:11:56 pm »
I don't think either is particularly better than the other. I'd probably prefer the second version. The only reason I wrote the first version was because it was similar to your original code and wanted to show only the [i] to .back() difference.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything