Jump to content

Fetching my player name can't match with string variable


homes196

Recommended Posts

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?

Link to comment
Share on other sites

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

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

  • Like 1
Link to comment
Share on other sites

  • 4 months later...

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...