Evil evil global variables

0 Dimitrios Pechlivanis · April 1, 2015
Ok,, so I search the net and found out that using global variables is bad most of the times. for many reasons.
But in my text based game I wanna create, I have used some global variables like "torch_taken=0 or =1" which implies whether the user has taken the torch or no.

I have about 4-5 classes so thinks are pretty messy, and these variables are necessary in many functions as parameters or simple variables.
What do you think? should I replace them? if so, how? Is an interface full of these variables more or less the same thing?


Post a Reply

Replies

Oldest  Newest  Rating
+1 steven kitchener · April 2, 2015
Hi Dimitrios,

As you have commented that this is a project for college i would assume that your tutors are trying to teach you the "correct" way to pass information to and from different classes. Also on this note you should definitely be using boolean values for whether something has been taken or not. That is what that variable is there for so you should be using it. The reason using int's to determine whether something is true of false is bad practice is down to the fact that an int can be any number, so if you accidentally type a 11 instead of a 1 then youre going to have a bad time. But if you stick to using boolean values then you can only equate them to true or false and you have will have no problem discovering a typo then.

Back to passing information. The best way to send and receive information is to use Getter and Setter methods. If you know about return types and parameters already then skip over this part.
*********************************************************************************
When defining a method it takes 4 parts. Ill define a "getter" method now

-The visibility, in this case we will be using "public" as we need to use it outside of the class its in.
-The return type. If you are not returning anything from the method then this will be "void" but in our case we are going to return a "boolean"
-The name. It is common practice for getter methods to start with "get" followed by what it is you are getting. However, when getting a boolean the name is often called "is" followed by the thing you are getting. So in this case we will call it "isTorchTaken"
-Finally the parameters. As we need no other information we will leave this blank.
**************************************************************************************************
So the method will look like this:

public boolean isTorchTaken(){

           return torchTaken;
}

where the variable torchTaken is your int variable stated in your original post that we now know to use as a boolean.

So, to put all that into practice you define a variable in your main class like so: boolean torchTaken = false;
Add the getter method into your main class.
When you instantiate your subclass in the main class pass the keyword (this) into its parameters. "this" means this class i.e. the Main class.  e.g. SubClass sc = new SubClass(this);

Thats the main class set up to send over that variable, now onto the SubClass.

When you define the SubClass you will have to have a MainClass object in the parameters. Then store that object as an object in the SubClass like so...

//variables
private MainClass main;

//constructor
public SubClass(MainClass mainClass){
       main = mainClass;
}

From now on you can get the value for "torchTaken" from inside this class by using the method call, main.isTorchTaken();

A setter method is a similar setup to the getter method, its main difference is obviously it sets a variable instead of gets a variable. One for this case would look like this:

public void setTorchTaken(boolean x){
        torchTaken = x;
}

This will be in your main class and you would use the method call main.setTorchTaken(true); in your sub class.

I know this was very lengthy but once you get a concrete understanding on how these work then the rest of Java becomes fairly easy. 

Hope this helps

Steven
0 Branislav Lazic · April 1, 2015
Global variables? You mean instance variables? Yes, you should encapsulate them. And why would you represent boolean value as 0 or 1? 
0 Dimitrios Pechlivanis · April 1, 2015
I mean public static variables which can be accessed by all classes. OK as far as boolean values , that's not my problem. Is it that bad for my game structure if I keep them like that? Cause I will be tested on my design of the game(its a project for college)
0 Dimitrios Pechlivanis · April 2, 2015
Well, first of all I want to thank you for this extensive reply, I am always happy to learn new information.
As far as my tutor, he is like the worst tutor in college, not interested in his job. Never answers emails etc.
So I don't expect him to teach me. Most of the things I know are from tutorials.

I will use boolean, I totally get that part.

Yes I am familiar with setters and getter from C++, as I am still learning Java, I will search for more info over the next days.

Now look, I have the main class where the game loop runs. 
login();     // a simple login system
While(game_on==true)
{
     getCommand();
     handleCommand();
     invUpdate();  //inventory update, not yet defined this function
     commandSave(); // I save commands entered into a .txt file so that each user keeps track of their proccess.
}
I have the gameFunctions class with functions like getCommand(), handleCommand() and other.
And I also have itemFunctions class, where I have functions about items.
Should I declare torchTaken and other item variables in gameFunctions class and call them in itemFunctions class?

According to your example, gameFunctions will be my "MainClass" and itemFunctions will be my "SubClass"


Another approach would be to take all itemFunction functions and add them to gameFunctions class, but it would be too big.
  • 1

Java / Android Development

107,017 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator