Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Can't switch boolean from another class

Featured Replies

I can't switch any booleans from another class.

public class Main {
public boolean hans = false;

public int onLoop() {
	Processing p = new Processing();
	p.exchangeContext(getBot();
	log("Hans is" + hans);
	p.Loop();
	return 500;
	}

}

public class Processing {
	public void Loop() {
	Main m = new Main();
	m.exchangeContext(getBot());
	log("Switching hans to true)"
	m.hans = true;
	}


}

As soon as I use this loop, the log will say:

[INFO][Bot #1][04/03 04:14:29 PM]: Switching hans to true
[INFO][Bot #1][04/03 04:14:29 PM]: hans is false

Pretty sure it's some dumb mistake but I can't get it.

 

13 minutes ago, The Undefeated said:

I can't switch any booleans from another class.


public class Main {
public boolean hans = false;

public int onLoop() {
	Processing p = new Processing();
	p.exchangeContext(getBot();
	log("Hans is" + hans);
	p.Loop();
	return 500;
	}

}

public class Processing {
	public void Loop() {
	Main m = new Main();
	m.exchangeContext(getBot());
	log("Switching hans to true)"
	m.hans = true;
	}


}

As soon as I use this loop, the log will say:

[INFO][Bot #1][04/03 04:14:29 PM]: Switching hans to true
[INFO][Bot #1][04/03 04:14:29 PM]: hans is false

Pretty sure it's some dumb mistake but I can't get it.

 

Make hans static (However I'm sure that's a bad idea unless they're final) or in your main class have a method that's responsible for updating the value?

I.e:

public class Main {
public boolean hans = false;

public void setHansValue(boolean val) {
	this.hans = val;
}

public int onLoop() {
	Processing p = new Processing();
	p.exchangeContext(getBot();
	log("Hans is" + hans);
	p.Loop();
	return 500;
	}

}

public class Processing {
	public void Loop() {
	m.exchangeContext(getBot());
	log("Switching hans to true)"
	Main.setHansValue(true);
	}
}

 

Edited by harrypotter

1 hour ago, The Undefeated said:

I can't switch any booleans from another class.


public class Main {
public boolean hans = false;

public int onLoop() {
	Processing p = new Processing();
	p.exchangeContext(getBot();
	log("Hans is" + hans);
	p.Loop();
	return 500;
	}

}

public class Processing {
	public void Loop() {
	Main m = new Main();
	m.exchangeContext(getBot());
	log("Switching hans to true)"
	m.hans = true;
	}


}

As soon as I use this loop, the log will say:

[INFO][Bot #1][04/03 04:14:29 PM]: Switching hans to true
[INFO][Bot #1][04/03 04:14:29 PM]: hans is false

Pretty sure it's some dumb mistake but I can't get it.

 

You are changing the boolean value in one instance of the class and outputting the boolean value from another. They aren't the same value.

The solution is to use getters and setters to change fields within a class.

Edited by Final

  • Author
8 minutes ago, Final said:

You are changing the boolean value in one class and outputting the boolean value from another. They aren't the same value.

 

11 minutes ago, harrypotter said:

Make hans static (However I'm sure that's a bad idea unless they're final) or in your main class have a method that's responsible for updating the value?

I.e:


public class Main {
public boolean hans = false;

public void setHansValue(boolean val) {
	this.hans = val;
}

public int onLoop() {
	Processing p = new Processing();
	p.exchangeContext(getBot();
	log("Hans is" + hans);
	p.Loop();
	return 500;
	}

}

public class Processing {
	public void Loop() {
	m.exchangeContext(getBot());
	log("Switching hans to true)"
	Main.setHansValue(true);
	}
}

 

Some Java basics I completely forget sometimes, I am ashamed.

Thanks both.

 

14 minutes ago, Final said:

You are changing the boolean value in one instance of the class and outputting the boolean value from another. They aren't the same value.

The solution is to use getters and setters to change fields within a class.

 

17 minutes ago, harrypotter said:

Make hans static (However I'm sure that's a bad idea unless they're final) or in your main class have a method that's responsible for updating the value?

I.e:


public class Main {
public boolean hans = false;

public void setHansValue(boolean val) {
	this.hans = val;
}

public int onLoop() {
	Processing p = new Processing();
	p.exchangeContext(getBot();
	log("Hans is" + hans);
	p.Loop();
	return 500;
	}

}

public class Processing {
	public void Loop() {
	m.exchangeContext(getBot());
	log("Switching hans to true)"
	Main.setHansValue(true);
	}
}

 

http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value

http://softwareengineering.stackexchange.com/questions/21802/when-are-getters-and-setters-justified

http://stackoverflow.com/questions/1568091/why-use-getters-and-setters

http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil

Read pls :gnome:

Edited by Vilius

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.