Weird bug with user.getName()

You think you've found a bug? Please report it here.

Moderators: Lapo, Bax

Post Reply
aMUSiC
Posts: 48
Joined: 15 Dec 2007, 17:15

Weird bug with user.getName()

Post by aMUSiC »

Ok, this one is driving me crazy. I am trying to write an invitation to the game system which is being monitored through a Zone level script. All invitations being sent by users are stored in an array of "Invitation" objects.

Upon a succesful invitation, I store the name of the user in the invitation object like this:

Code: Select all

invitations[invId] = ({ 
   hostUser:user.getName(), 
   hostUserId:user.getUserId() 
});
the above happens through an XT call.

So far so good.

Now, I disconnect and reconnect the client. The zone level extension has the object in the server's memory, and I try to initiate a second invitation from the same user.

now, what I want is to forbid the user to initiate a second invitation if an invitation with his name is found in the list of invitations.

so I do:

Code: Select all

for (m in invitations) {
  trace("stored user is '"+invitations[m].hostUser+"'");
  trace("current user is '"+user.getName()+"'");
  trace(invitations[m].hostUser == user.getName());
}
The result of the trace I get is:

stored user is 'aMUSiC'
current user is 'aMUSiC'
false

the username is hardcoded in the client so there's absolutely no way it is in a different codepage, or mistyped. So.. I basically see that my stored variable derived from user.getName() and user.getName() produce the same result.. but they are NOT equal.. therefore an

Code: Select all

if (invitations[m].hostUser == user.getName()) { 
  ... do stuff...
}
does not work.

I also tried using toString() but the result is the same... not equal!
aMUSiC
Posts: 48
Joined: 15 Dec 2007, 17:15

Post by aMUSiC »

I think I found it out..

First I shouldn't have used parenthesis on the .toString method. Changing it from .toString() to .toString worked.. which brings me to the following interesting conclusion..

according to the documentation for the User class on the server extensions:

getName() Return the user name

I used typeof() to see what is the type of getName()'s result, and interestingly enough.. it was an Object.. and not a string.

So I suppose that getName() basically returns a pointer to where the username is stored insted of the actual textual representation of the username. All this time I've been comparing pointers...

The pointer approach is good and I guess saves resources.. so i guess the bug lies with the documentation after all..
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

The problem is due to the fact that the Actionscript environment interacts with the Java objects used by the server.

In this specific case you get a String from a java object (User) so you get a java.lang.String object which you compare with a native AS string.
The == operator compares the equality of the two references, hence the problem.

In Actionscript you can do this:

Code: Select all

var s1 = "Hello"
var s2 = "Hello"
trace(s1 == s2)
this compares the value of the two strings.

In Java:

Code: Select all

String s1 = "Hello";
String s2 = "Hello";

if (s1 == s2)
   System.out.println("Same references");

if (s1.equals(s2))
   System.out.println("Same values");
We have tried to add notes in the AS docs about this but some are still missing, I am aware of this.

In order to avoid the problem with "mixed" strings simply cast them to AS native strings like this:

Code: Select all

userName = String(user.getName())
Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply