-
Users and characters
Hi all.
I have a User class with two member variables, m_sUsername and m_sPassword. I created this class only a few minutes ago, and before I know it i'm stumped. How do I link a User to the character created by it? Say I just registered and logged in. I've now created a character and want to logout. I logout and log back in again and I want to play as the same character. How would I go about saving the character to that specific User?
-
Keep a vector with (pointers to) characters in the user class?
-
There are several methods, but if a user ALWAYS has ONE character, then you would store the character (or better, some form of ID or reference) inside the user. In this case, it may still be a good idea to store the user ID inside the character.
If there are multiple characters per user, you definitely HAVE to store the user-ID inside the character.
--
Mats
-
So I would create a reference to a character created by the user and store it in the User class? Sounds good, thanks!
-
No, you would create an instance of a character class and store a pointer (best bet is a smart pointer, std::tr1::shared_ptr) in the appropriate user class.
Pointers and references aren't the same thing in C++ - you had better remember that! A reference just won't work here!
Just remember that you have to create instances and populate that array everytime a user logs in.
For performance, you might consider storing IDs and creating instances of character classes as they are first needed and then cache them.
-
Hmmm.. What if I create a reference in my User class to a pointer that points to character that's in my Character class? Or would that not work either?
-
What would be the point of that?
You would want a vector of your character objects (not pointers).
You could start with that and see if you get a big performance hit. It's good not to do premature optimization.
Code:
class User
{
std::vector<Character> m_Characters;
// ...
};
-
I would advise against storing a pointer or reference to any instances of a character inside of a user class. I would store some type of character identifier that might return a ref counted pointer or perhaps a boost shared ptr to the user class from some type of character manager class.
-
Ohhhhhh that looks and sounds better than my idea :)
I'm still in the early learning stage and my code is still in the development stage, thanks a lot!
I'm sure you will be hearing from me soon when another problem arises!