• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
StoneMask

Do I need accessors and mutators for every class variable?

19 posts in this topic

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][code]/* 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();
[/code][/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
1

Share this post


Link to post
Share on other sites
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
1

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
0

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?
0

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 [url="http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr061.htm"]information[/url].
0

Share this post


Link to post
Share on other sites
I myself like to make setters and getters but the way from MichaBen:
[quote]Personally I try to avoid setters that refer to a specific variable, as that would mean the object who calls it has to know how this class works internally. Instead, I prefer to have functions that invoke a specific state change or give me the information I need.[/quote]
is really a great way.
You should know your class and focus it only on the specific tasks.
But sometimes there is no time or you have a lazy phase (never ever listen to this phase, please!), then can you make your class properties (variables) public, but only if it is something like a data storage (old-fashioned structs).
Example:
[CODE]
class A
{
/*Very much variables*/
int Variable1;
int Variable2;
int Variable3;
...
}
/*You want to return more than one variable, therefore use a class.*/
A foo(int i)
{
A a;
a.Variable1 = sin(i*PI);
a.Variable2 = cos(i*PI);
a.Variable3 = sin(i*PI)*cos(i*PI);
/*And so on...*/
return a;
}
[/CODE]
Class "A" will be never used somewhere else.

This above looks sometimes useful and time saving, right?

[b]Forget it![/b] It is a mess. You can never be really sure that class "A" will not be used somewhere else!
This example above is a design mistake. Give yourself time and make some design. Use setter/getter or better real members (like these from MichaBen).
You will be in the safer site and will not have a headache or acute laziness later.

This failure happened often to me years ago.
Of couse the example is not wrong, but for me it's just disgusting. Use it if you like it. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] I do not.
0

Share this post


Link to post
Share on other sites
[quote name='ApochPiQ' timestamp='1343412734' post='4963719']
...good information...
[/quote]
I'd just like to point out the fact that you have too many damn points. Ignore the fact that I've upvoted you a few times already :P
0

Share this post


Link to post
Share on other sites
Many of the variables are being accessed by code in my main.cpp, where the program determines what to do based on what I get from the class object in terms of information.
0

Share this post


Link to post
Share on other sites
[quote name='StoneMask' timestamp='1343430305' post='4963792']
Many of the variables are being accessed by code in my main.cpp, where the program determines what to do based on what I get from the class object in terms of information.
[/quote]

Thats probably your problem, you're trying to force an OO structure (a class) on a non OO design which is why you need so many accessors and mutators.

a class should have methods that do things with the class instance and possibly a few methods to inspect the state of the object.

For example, you have AddLevel and AddXP methods. atleast one of those is redundant, possibly both.

consider this:

player.attack(monster[6]);

if the monster dies the player can get the xp value from the monster directly, add it to its current XP and if necessary increase the level, no other class has to be able to do this.
Other classes might want to read the current level of the player object though (to be able to show it in for example the UI) and you could possibly want an addXP method if you need to trigger xp gains without the player doing anything. (I can't think of a single case where you'd want to do that though, xp gains are almost always the result of an action performed by the player object)

Also, OO is not the only or necessarily the best way to do things, if you prefer a different approach feel free to use it, but don't try to force OO structures on non OO code, it gets very painful very quickly. (if you just want to group data logically structs are a far better match than classes) Edited by SimonForsman
1

Share this post


Link to post
Share on other sites
[quote name='SimonForsman' timestamp='1343431104' post='4963797']
Also, OO is not the only or necessarily the best way to do things, if you prefer a different approach feel free to use it, but don't try to force OO structures on non OO code, it gets very painful very quickly. (if you just want to group data l[b]ogically structs are a far better match[/b] than classes)
[/quote]
I was just thinking this and was going to post it. But since you have already mentioned, I'll just reiterate by quoting you. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by Alpha_ProgDes
0

Share this post


Link to post
Share on other sites
[quote name='darookie' timestamp='1343472434' post='4963909']
While OO-purists would point out that having public class variables is a sign of bad design, I tend to think more practically. C++ is a multi-paradigm language for a reason and I personally see no point in debates over stylistics.

Oops, I just noticed that Alpha_ProgDes wrote exactly this Posted Image
[/quote]
But yours has more detail and a good point about C++. Edited by Alpha_ProgDes
0

Share this post


Link to post
Share on other sites
So when you're saying it should be more exclusively OO or not, you're saying my functions should either take place within the class object or in the main code? And how do I return a state to the main function when the struct or class only exists in my player's struct or class? Is it a simple Player::(struct) temp? I could probably find this out on my own, but I want to cover my bases, as I don't quite know what is poor practice yet. I have, however, figured out that it's probably easier to have a collide function that checks what the player collides with, then make appropriate alterations. Like say, colliding with nothing will cause me to move forward, and colliding with a heart will increase health. Most stat alterations happen through collisions. The only exceptions would be when I buy things from a shop, which uses external code from the class. How would I handle that? Would I just add in an addHealth function anyway?

Also, since the inventory is really only one thing, followed by two gained abilities, do you think a bool array would be good? Like, position one is whether or not fast travel is enabled, position two is whether or not I have a certain ability, etc.? It seems simpler than going with a vector or something, and even though I know it's negligible in terms of memory, I'm a somewhat minimalistic person.

I have more questions, but these are the more immediate ones. I was re-reading through the responses since I've cleared my mind of all the complicated stuff and looked through everything with fresh eyes, so to speak.
Thanks for your help.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0