Advertisement

Naming Instances of a Class

Started by April 23, 2000 09:37 PM
4 comments, last by Deathlord 24 years, 7 months ago
Currently I'm trying to program the basis for an online RPG. I'm using a class to hold all of a player's data, but I have a question on naming each instance dynamically when a new player joins. I don't want to use a set struct. In order to have a separate name for every player, I wanted to be able to use their username as the identifier for the player class. I.e. if I entered my name as Deathlord, I want to declare it with Player Deathlord() .... Deathlord.Setup() etc. Is this possible to do? If not how could I go about setting it up so each run would set it to a new player name? I tried an array of players but I couldn't figure out how to do it. I'm guessing it uses pointers but I need a little help with that. This sounds a bit confusing the way I explained it but if anyone could help I would be grateful. Thanks. -Deathlord Edited by - Deathlord on 4/23/00 9:42:21 PM
-Deathlord
Hm... I''m not too sure I know what you''re trying to do but I think you''re confused. Why would you want to name an instance of an object dynamically (assuming it was possible which it isn''t) that no one playing the game would see? Once you build an executable all those nice names you pick for variable names/object names, are turned into a goop of binary. What you''re asking to do doesn''t make sense. What you''re going to need to do if I understand the problem you''re trying to solve, is to use a linked list of player objects. You could use an array also, but you''d be possibly wasting space, and you''d have to limit the number of players to the size of the array. Or, if you''re just trying to keep track of a players name, a char *name member is all that''s needed. Again, I''m not sure about what you''re talking about, maybe you could elaborate?
Dan
Advertisement
Well ok I guess you're right, but I just wanted to use it for text save files (a command log) so it would be easier to read instead of "player1.Setup()" it would tell me it executed a command for "thisplayer.Setup()"... but I guess just having a *char would be good enough. How would I make a linked list for this kind of class? The book I'm using doesn't really explain it well enough.

-Deathlord

Edited by - Deathlord on 4/23/00 10:06:24 PM

Edited by - Deathlord on 4/23/00 10:50:46 PM
-Deathlord
Well, in that case, you could just have a string for each function, and a string for each player name and concatenate the strings to make a command string. So in cheesball psuedo code:
class player
{
private:
int blah;
int blag;
char *playerName;
int etc;
public:
Setup();
}
player::Setup()
{
// string holding this functions name
char *funcName = ".Setup()";

char *logString;
logString = ( char * )malloc( sizeof( char ) * ( strlen ( playerName ) + 1 ) );
strcpy( logString, playerName );

// Concatenate the 2 strings
strcat( logString, funcName );

// if player name is "dude", logString will now be
// "dude.Setup()"
dumpToLog( logString );
}

Well, I hope that helped some. As for the linked list thing... I''d have to write a tutorial for something like that, and if I did, there''s no guarantee you''d understand my take on it better than whatever book you have.
Dan
quote: Original post by Deathlord
just having a *char would be good enough. How would I make a linked list for this kind of class? The book I''m using doesn''t really explain it well enough.


Obviously that book doesn''t have STL in it

Just do a lookup for the vector class in the STL. If you are new to this sort of programming, sounds like you are, this is the quickest way to get it going.

Later down the road, when you rea up on linked lists and the like, you can easily swap out the vector class from the STL for your own.







quote: Original post by Deathlord

I.e. if I entered my name as Deathlord, I want to declare it with

Player Deathlord()
....
Deathlord.Setup()
etc.

Is this possible to do?


Well, no. Not in C++. You have to write the code before the program executes. Therefore you need to know the names of your objects/variables/etc. Which means you can't do something which was like;
String name;cout << "Enter your name: ";cin >> name;Player name;

The compiler would assume you wanted a Player object called 'name' and you already have a String called name.

So, a different approach is needed. You could make your Player class have a string or a char* member called name, and initialise it in the constructor like so:
Player player1(name);

Then, you could grab that whenever you do the logging. Example:
void Player::Setup(){    char buf[100];    sprintf(buf, "%s::Setup()", name);}

Which would write "DeathLord::Setup()" or whatever the name was.

As for a linked list of these things, I'm with Joviex - the STL has some nice container stuff (lists, sets, etc), and since you probably won't be adding/removing players very often, a vector should be fine. You may want to work with pointers to Players, so this is how I would do it:
#include typedef std::vector PlayerVector;// My main list of playersPlayerVector allPlayers;// Add someone to the listallPlayers.push_back(new Player("DeathLord"));allPlayers.push_back(new Player("Kylotan"));
Etc. Add your own error checking if needed. Better still, perhaps, would be to use an STL map. Then you can use your player names as if they were an index into an array! So you could do things that look like:
allPlayers["Kylotan"]->IncreaseScore(10);

Which is a little more advanced, but I'm sure you can see how natural it looks (it looks up the right player by name for you) and therefore why it would be a good reason to investigate it further

Edited by - Kylotan on 4/24/00 7:48:26 AM

This topic is closed to new replies.

Advertisement