Jump to content

Keeping track of a global variable


sudoinit6

Recommended Posts

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?

Link to comment
Share on other sites

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

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

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

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

 

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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