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.

Fetching my player name can't match with string variable

Featured Replies

I've defined my player's display name in a String variable (changed actual player's name for this example):

String MULE_NAME = "displayname";

And a method that checks their name:

public void getJob() {
		
		if (getPlayers().myPlayer().getName() == MULE_NAME) {
			
			CURRENT_JOB = "Mule";
			
		} else {
			
			CURRENT_JOB = "Other job";
			
		}
		
	}

It ALWAYS returns as "Other job". I've double checked that I've entered the right display name into MULE_NAME and even had the log output the variable and my player's name every loop. They match in the log yet apparently are not the same. What gives?

9 hours ago, homes196 said:

I've defined my player's display name in a String variable (changed actual player's name for this example):


String MULE_NAME = "displayname";

And a method that checks their name:


public void getJob() {
		
		if (getPlayers().myPlayer().getName() == MULE_NAME) {
			
			CURRENT_JOB = "Mule";
			
		} else {
			
			CURRENT_JOB = "Other job";
			
		}
		
	}

It ALWAYS returns as "Other job". I've double checked that I've entered the right display name into MULE_NAME and even had the log output the variable and my player's name every loop. They match in the log yet apparently are not the same. What gives?


First of all it should be:

myPlayer().getName()

Not:

getPlayers().myPlayer().getName()



Secondly you don't compare Strings using ==. == Checks that the references are equal, not the values. To check the values of Strings for equality you should use .equals():

myPlayer().getName().equals(MULE_NAME)


Example:

String string1 = new String("hi");
String string2 = new String("hi");

string1 == string2  // false, string1 is a different instance to string2

string1 == string1 // true, string1 is the same instance as string1

string1.equals(string2) // true, the value of string1 is equal to the value of string2



If there is a space in the username you _may_ need to do this to replace non-breaking spaces:

(I cant  remember if you actually need to do this or not in this case)

myPlayer().getName().replace('\u00A0', ' ').equals(MULE_NAME)


Also you have named the mule name variable as MULE_NAME, yet the value is not a constant. It should be:

private static final String MULE_NAME = "displayname";

This will not affect functionality, but it is best practice.


Finally you say that you are using this getJob() function every loop. I would recommend you only call it once in onStart() because the username isn't going to change throughout the script's lifetime. (Unless the script changes accounts)

Edited by Explv

  • Author

@Explv

Thank you for the detailed response, using myPlayer().getName().equals fixed my issue. I mix up other practices from other programming languages (Lua) sometimes and I'm not that great at Java yet.

1 hour ago, homes196 said:

@Explv

Thank you for the detailed response, using myPlayer().getName().equals fixed my issue. I mix up other practices from other programming languages (Lua) sometimes and I'm not that great at Java yet.

Scripting languages teach bad convention practices. 

9 hours ago, Explv said:

 


String string1 = "hi";
String string2 = "hi";

string1 == string2  // false, string1 is a different instance to string2

string1 == string1 // true, string1 is the same instance as string1

string1.equals(string2) // true, the value of string1 is equal to the value of string2

 

Actually, string1 == string2 IS true, but only in case of Strings, because of Javas String constant pool

12 minutes ago, FrostBug said:

Actually, string1 == string2 IS true, but only in case of Strings, because of Javas String constant pool

Yeah you right, thanks for the correction, I should have written new String() instead. Will updaterino

Edited by Explv

  • 4 months later...
On 9/25/2018 at 5:29 PM, Explv said:

myPlayer().getName().replace('\u00A0', ' ').equals(MULE_NAME)

 

thx

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.