So I am making a Multiplayer Game with Classes, I have my Class player. And I want to call the instances Player.PLAYERNAME. Like for example a Player is called John and I want to access his stats, I would write:
class player {
private:
int Stats;
};
string PLAYERNAME = "John";
player Player.PLAYERNAME;
if (Player.John.Stats < 4) {
print "John sucks at this";
};
name is a part of the players identity, so put it inside the class as a data member and as your class members are private you'll need appropriate getters. something on the following lines:
Problem is, Amount of Players can differ and if i understood this correctly, I can't change the instance name. So how can I continue this ( p1 , p2 , p3 .. p16 ) and that only for the amount of players online on the server.
#include <map>
class player
{
public:
player(int val) : Stats(val) {}
private:
int Stats;
};
std::map<std::string, player> Player;
...
// Put the "John" player into the map
Player.insert("John, player(1);
...
string PLAYERNAME = "John";
// Note: not quite the syntax you want, but close
if (Player[PLAYERNAME].Stats < 4) {
print "John sucks at this";
}