Jump to content
  • Advertisement
Sign in to follow this  
mpc91193

Variables + OOP/OOD

This topic is 2666 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 have a question for you guys i have a bunch of variables, that i want to be able to be accessed by any class(in C#).These will be ones such as health, mana, gold, exp, attakpwr, defense. How would i go about making these "global variables" ?

Share this post


Link to post
Share on other sites
Advertisement
You can make them private and make public methods for accesing them
For example

private:
int health;
public:
int getHealth()
{
return health;
}
void setHealth(int h)
{
health = h;
}

it is always better to do this way, although I'm using C++, but i think C# has it's own methods to quickly create accesor functions

Share this post


Link to post
Share on other sites

I have a question for you guys i have a bunch of variables, that i want to be able to be accessed by any class(in C#).These will be ones such as health, mana, gold, exp, attakpwr, defense. How would i go about making these "global variables" ?


1. That's a bad idea.
2. Make them public. Then other classes can access that instance's variables. Again, generally frowned upon.
3. Make them static. Then you don't need to create an instance of the class for the variables to exist. Generally, a little frowned upon, and decidedly un-OOP (especially for this scenario).

Share this post


Link to post
Share on other sites

[quote name='mchristensen' timestamp='1299418106' post='4782397']
I have a question for you guys i have a bunch of variables, that i want to be able to be accessed by any class(in C#).These will be ones such as health, mana, gold, exp, attakpwr, defense. How would i go about making these "global variables" ?


1. That's a bad idea.
2. Make them public. Then other classes can access that instance's variables. Again, generally frowned upon.
3. Make them static. Then you don't need to create an instance of the class for the variables to exist. Generally, a little frowned upon, and decidedly un-OOP (especially for this scenario).
[/quote]


could you give a small example? Im still sort of new to the whole proper way to use methods and such.

Share this post


Link to post
Share on other sites

[quote name='Telastyn' timestamp='1299421219' post='4782410']
[quote name='mchristensen' timestamp='1299418106' post='4782397']
I have a question for you guys i have a bunch of variables, that i want to be able to be accessed by any class(in C#).These will be ones such as health, mana, gold, exp, attakpwr, defense. How would i go about making these "global variables" ?


1. That's a bad idea.
2. Make them public. Then other classes can access that instance's variables. Again, generally frowned upon.
3. Make them static. Then you don't need to create an instance of the class for the variables to exist. Generally, a little frowned upon, and decidedly un-OOP (especially for this scenario).
[/quote]


could you give a small example? Im still sort of new to the whole proper way to use methods and such.


[/quote]

OOP groups data and their related methods into encapsulating classes. For example, you create a class Player that has member variables health, exp, attakpwr, defense, and so on. You could then either
1)make the member variables public (i.e. declared as "public int health") but this is one of those "generally frowned upon" ideas in OOP as it breaks the encapsulation, or
2)make the data private but create public methods that modify it. As an example:


//in class Player
...
private int health, defense;
...

//simple method to cause the player to take damage, mitigated by their current defense stat.
public void takeHit(int dmg)
{
health -= (dmg - defense);

if(health <= 0)
die();
}

Share this post


Link to post
Share on other sites

[quote name='mchristensen' timestamp='1299430962' post='4782463']
[quote name='Telastyn' timestamp='1299421219' post='4782410']
[quote name='mchristensen' timestamp='1299418106' post='4782397']
I have a question for you guys i have a bunch of variables, that i want to be able to be accessed by any class(in C#).These will be ones such as health, mana, gold, exp, attakpwr, defense. How would i go about making these "global variables" ?


1. That's a bad idea.
2. Make them public. Then other classes can access that instance's variables. Again, generally frowned upon.
3. Make them static. Then you don't need to create an instance of the class for the variables to exist. Generally, a little frowned upon, and decidedly un-OOP (especially for this scenario).
[/quote]


could you give a small example? Im still sort of new to the whole proper way to use methods and such.


[/quote]

OOP groups data and their related methods into encapsulating classes. For example, you create a class Player that has member variables health, exp, attakpwr, defense, and so on. You could then either
1)make the member variables public (i.e. declared as "public int health") but this is one of those "generally frowned upon" ideas in OOP as it breaks the encapsulation, or
2)make the data private but create public methods that modify it. As an example:


//in class Player
...
private int health, defense;
...

//simple method to cause the player to take damage, mitigated by their current defense stat.
public void takeHit(int dmg)
{
health -= (dmg - defense);

if(health <= 0)
die();
}

[/quote]

So if i understand correctly by making the variable private it keeps it central to the class it is contained in, but a public method within the same as the variable class can change the value, but not a method outside that class? That seems slightly counter productive because, wouldn't it be easier to access the variables data as needed by any class/method that needs it? or am i mis understanding something?


Share this post


Link to post
Share on other sites



So if i understand correctly by making the variable private it keeps it central to the class it is contained in, but a public method within the same as the variable class can change the value, but not a method outside that class? That seems slightly counter productive because, wouldn't it be easier to access the variables data as needed by any class/method that needs it? or am i mis understanding something?





The point is to control access to the data. It's not counter-productive to restrict access, especially when all you have to do is make a call to "get" or "set" a value; it's not much more to do than to directly assign a value or to pass one as a parameter. But you don't need to worry about accidentally changing the values when you don't mean to, it's easier to check a value at any given time, and so on. There's nothing inherently wrong with global variables, that can be manipulated by anything in the program at any time, but you have more options and better control by taking an object oriented approach rather than a procedural one.

It's really not harder to use get and set functions for a private variable compared with passing the value in question directly.

Share this post


Link to post
Share on other sites

So if i understand correctly by making the variable private it keeps it central to the class it is contained in, but a public method within the same as the variable class can change the value, but not a method outside that class? That seems slightly counter productive because, wouldn't it be easier to access the variables data as needed by any class/method that needs it? or am i mis understanding something?

You want to keep things private and locked away in most classes. A class is, ideally, a thing that has a single purpose exposed as a set of actions. You don't want to have "health + getters/setters", you want more specific terms to your object like "recieve_damage" and "is_dead". This does two major things:
First off, it makes sure you can impose limits on the variables. In the case of health, you'd want to know when/if it goes below 0, because that killed you. You'd also probably check that healing you didn't take you past full health. Sure, other items could manage those limits, but the first time another programmer comes along to edit health they have to know all the esoteric rules to editing health. That instantly means that you have bugs to work out of the code. Keeping it in a method function of the class owning the health means it is a lot harder to misuse the variable.
Secondly, it localizes the place to change code by abstracting the implementation away from other classes. It is easy now to implement "health = health - damage" everywhere, but what happens when you want to add armor to that? Elemental resistances? etc? When you have code all over the place touching the health variable, you don't know who is doing what with it. It makes for a lot of code changes for something that should be a one line change to "health = health - max( (damage - armor), 0 )". By making health private, you can just change the "damage()" function, and know that your new armor damage reduction works everywhere.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!