Class Object Mesh Vector Array

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
LunaRebirth
Posts: 385
Joined: Sun May 11, 2014 12:13 am

Class Object Mesh Vector Array

Post by LunaRebirth »

Hello!
I have a, probably simple, problem.

What I'm trying to accomplish is creating a class object named Player, which will handle all player code (of course).
So basically, I've created this Player class to have a vector (array) which has a total size of 8. This vector will have IAnimatedMeshSceneNode*'s with the player's Head, Body, Feet, Hands, etc.
So vector[0] is the skeleton, vector[1] is the head, vector[2] is the body, and so on.

Like a class with this vector array:

Code: Select all

class Player {
  public:
    std::vector<IAnimatedMeshSceneNode*> playerNodes;
}
and this in the main file:

Code: Select all

std::vector<Player*> player;
//...
player.push_back(new Player());
player.push_back(new Player());
And then if I change player[0]'s rotation, this also changes player[1]'s rotation.
I cannot make the playerNodes vector variable private, because I need it to be accessible by other classes.
I assumed this was because I was using a pointer for IAnimatedMeshSceneNode* but I don't think it's possible to do otherwise.

Is there a way I can have two Player objects, without the playerNodes variables "interfering"?

I've also tried looking at the playerNodes size, and it seems to be 8*<Amount of players created> (in this case, 16 total) when it should only be 8 per player.

I tried explaining this problem as well as I could.
Thanks for the help!
CuteAlien
Admin
Posts: 9846
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Class Object Mesh Vector Array

Post by CuteAlien »

They should not interfere at all. when you have 2 pointers pointing to different places. Meaning you have a bug somewhere.

Let's get the public and private out of the way first as it has nothing to do with this :-) That stuff is just there to help you as programmer to hide stuff from yourself (or other people using your code). Internally (after compiling) it no longer matters - there's no change to runtime. You make things private so if you want to figure out where a variable is changed you don't have to look everwhere in your sources but only in a single file of that class. The trick to make a variable like playerNodes private is - just do it. And then write access functions. If you really need full access (including adding/removing things directly to the vector) - then you can give a reference to it:
std::vector<IAnimatedMeshSceneNode*>& MyPlayerNodes() { return playerNodes; }
Thought nearly always you reduce access further. For example only giving access to single elements.
Or only returning a const reference (so you can access the whole array but not modify it from the outside).

But that's not your bug. Without seeing any other code I'd guess you give each player the same IAnimatedMeshSceneNode* accidentally instead of each one having his own playerNodes.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
LunaRebirth
Posts: 385
Joined: Sun May 11, 2014 12:13 am

Re: Class Object Mesh Vector Array

Post by LunaRebirth »

So, I'll try to make this code as similar as possible.
I am at class right now.

Player.h

Code: Select all

#include "../includes"
class Player {
  public:
    std::vector <IAnimatedMeshSceneNode* playerNodes;
    void Run(); //Player's main loop
};
Player.cpp

Code: Select all

#include "../includes.h"
#include "Player.h"
std::vector <IAnimatedMeshSceneNode* playerNodes;
bool ranOnce=false;
void Player::Run() {
  if (!ranOnce) {
    playerNodes.push_back(0);
    ranOnce=true;
  }
}
Main.cpp

Code: Select all

#include "includes.h"
#include "Player.h"
 
std::vector <Player*>players;
void main() {
  while (device->run()) {
    if (players.size()==0) {
      players.push_back(new Player());
      players.push_back(new Player());
    }
    for (int x=0; x<players.size(); x++)
      players[x]->Run();
  }
}
Strangely, in Player.cpp, this will push back playerNodes to size 2 when it should be size 1. The more "new Player()" I add, the more the vector pushes back, as if the playerNodes vector is being shared between all new Players.

*Typed on my phone. Sorry if there are any spelling errors and what-not
CuteAlien
Admin
Posts: 9846
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Class Object Mesh Vector Array

Post by CuteAlien »

Why have you created a second vector in Player.cpp also called playerNodes? That one is global. And has nothing to do with teh one in the class Player - except unfortunately having the same name. Though as long as you access it inside member-functions the member playerNodes should be used and not the other one.

Please get rid of the global one - it's probably just been added to work around a bug and does nothing but confuse you.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply