Jump to content

Keeping track of a global variable


Recommended Posts

Posted

I know in Java there is no such thing as a global variable but I am looking for something similar.

 

I am writing a script that does several things in 4 areas. I need a way to update a variable to say "section one is done, move on to section two" and then in my other classes be able to check the state of this variable. Can someone point me in a direction?

Posted (edited)
1 minute ago, andrewboss said:

That doesn't make sense..

How does it not make sense? 

start all booleans false, once it completes one thing, that boolean is true and moves onto next step until that one is true and continues.

He has 4 steps he wants to accomplish so he can use 4 booleans and after each one is completed set it true 

Edited by Juggles
Posted
29 minutes ago, Juggles said:

If I am understanding correctly, why not just use booleans?

Boolean hasBanked

Then once it has banked, set it true and move on to the next step?

That's exactly what I was looking for. You are awesome as always. I have often said that this farming thing would be a lot easier if I knew Java!

  • Like 2
Posted (edited)

I always optimize my code to less use primitive variables when possible.

 

	
	public enum States {
		BANKED, WHATEVER
	}
	
	public static States state;
	
	private static void transitionState(States state) {
		Class.states = state;
	}
	
	public static States getCurrentState() {
		return Class.state;
	}

Something along these lines could also be used (quickly made it up obviously)

Edited by Vichy
Posted
12 hours ago, dreameo said:

then you force methods to be static

loop would look like this since you couldn't use any of your static methods inside

onLoop(){

 Main.cut()

 Main.bank()

 ...

}

 

12 hours ago, Team Cape said:

'public' for global variables related to an instance of a class

'public static' for global variables related to no particular instance

 

  • Like 1
Posted

 

54 minutes ago, Chris said:

 

 

 

15 hours ago, dreameo said:

global variables do exist lol.

 

13 hours ago, Team Cape said:

'public' for global variables related to an instance of a class

 

14 hours ago, Chris said:

just go static :doge:

 

15 hours ago, dreameo said:

Seems like you're doing everything inside of a single class. You just need to init the variable within the class and any object or method can access it.

 

13 hours ago, Team Cape said:

'public' for global variables related to an instance of a class

'public static' for global variables related to no particular instance

 

13 hours ago, dreameo said:

then you force methods to be static

loop would look like this since you couldn't use any of your static methods inside

onLoop(){

 Main.cut()

 Main.bank()

 ...

}

 

  • Like 1

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...