Jump to content
  • Advertisement
Sign in to follow this  
StoneMask

Do I need accessors and mutators for every class variable?

This topic is 2182 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I understand the importance of keeping some data members from being accidentally manipulated outside class functions, but say I have a class object that's full of variables I need to check up on all the time, like the player in a video game. Is it really worth it to make the program jump control between those functions all the time? What are the criteria for a variable that's worth making an accessor/mutator function? Because so far, this is what my character's header looks like:

[spoiler]/* mutators */
void AddLevel (short alteration);
void AddHealth (short alteration);
void AddExP (short alteration);
void AddKeys (short alteration);
void AddTorches(short alteration);
void AddGems (short alteration);
void SetFacing (short alteration);
void SetExP (short alteration);
void SetPosX (short alteration);
void SetPosY (short alteration);
void SetIsHurt (short posX, short posY, bool isBoss);
void SetInFort (bool alteration);
void SetInShop (bool alteration);
void SetInSpace (bool alteration);
void SetFasT (bool alteration);
void SetGoldWhale(bool alteration);
void SetWin ();
void SetmFufni();
void SetName ();
/* accessors */
short GetLevel();
short GetHealth();
short GetExP();
short GetKeys();
short GetTorches();
short GetGems();
string GetName();
short GetFacing();
short GetPosX();
short GetPosY();
bool GetTorch();
short GetTorchX();
short GetTorchY();
char GetInFront();
bool GetInFort();
bool GetInShop();
bool GetInSpace();
bool GetFasT();
bool GetIsHurt();
bool GetSetting();
bool GetWin();
COORD GetPos();
[/spoiler]

I'm running out of stuff to name these things, and sometimes I have to work around just naming it something I think is clear enough. Edited by StoneMask

Share this post


Link to post
Share on other sites
Advertisement
What do you mean? Wouldn't that mean just any variable that's actually changed? How do you define arbitrary when modifying something? Or breaking the class from the outside, for that matter? Edited by StoneMask

Share this post


Link to post
Share on other sites
Could you give me an example of something that would need subdividing into multiple classes, and how to implement it so you can still access all the information of the other class? Edited by StoneMask

Share this post


Link to post
Share on other sites
Getting a bit away from your specific question, I'd caution that part of the problem is that you're beginning to have a rather monolithic, unfocused class.

It's difficult to say without knowing more about the design of the game itself, but the impression that the accessor/mutator names give me is that some of them could stand to be combined into their own class (For example: posX, PosY, and [maybe] Direction might become a class) and others either seem to be misplaced (InFront, InFort, InShop, InSpace) and/or might indicate some kind related state that could be expressed as an enumeration (if states are mutually exclusive or have limited but well-known combinations) or a bitfield (if states can be combined more freely).

Still others might be better solved in a much more general fashion, for example you have a number of variables that seem to track inventory -- if the variety of available inventory items is small this approach may be fine, but if the varieties grow to more than a couple of handfuls, you might want to look into creating an inventory object that holds the actual items, and having this "player" class have an inventory (either directly, or by owning a unique key within a shared inventory system). In short, as you add more variety of items, you tend to want something that starts to look more and more like a database, rather than an ad-hoc collection of values. You can do stats similarly.

Basically, try to think about the different contexts in which you currently plan to access your "player" class, and see how the data naturally groups together -- for example, its unlikely that a single context would care about the player position *and* the contents of their inventory simultaneously, in a well-designed system.

Share this post


Link to post
Share on other sites
I made this project under a deadline and I just kind of added stuff and didn't plan anything out. The state of where the character is in the environment could definitely be an enumeration, now that I think about it. I have other enumerations in the class.

The character's inventory really only has two things in it. The game is very simple.

So you're all recommending that I make some of these things structs or classes and have the getters of my player class just return one of the values depending on the states in those structs or classes? As a cleanliness question, would I want to put the classes that are used solely for other classes in their own .h/.cpp files?

Share this post


Link to post
Share on other sites
How do you mean 'used solely for other classes'? Like you have class A and class B and class A is the only class that uses class B? If that's what you mean then one option is to nest class B within class A. Here's some more information.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!