Page 1 of 1

Avatar Chat example checking if user has changed position

Posted: 08 Feb 2012, 14:25
by ceteri
I am just going through the avatar chat example and am struggling to understand why the conditions in the if statement indicate that a user has changed position.

why would changedVars.indexOf(USERVAR_X) be equal to -1 if the variables had not changed?

code from the website:

// Check if the user changed his position
if (changedVars.indexOf(USERVAR_X) != -1 || changedVars.indexOf(USERVAR_Y) != -1)
{
// Check if avatar exists
if (getAvatar(user.id) != null)
{
// Move the user avatar
moveAvatar(user)
}

Posted: 08 Feb 2012, 21:32
by rjgtav
Hi.

In order to save bandwith and server resources, when you update an user's variables, only those variables are sent to the server and the other clients. Not the whole list!
When you receive the USER_VARIABES_UPDATE event, it comes with those changedVars parameters, which is an Array containing the name of the variables that were updated. So, by checking if there is or not that variable in the changedVars list, you know if the variable was updated or not.