Saturday, December 25, 2010

SmartFoxServer Tutorial: setUserVariables()

Hello everyone!

I just received a request from Bobhead202 asking about how to change moderator outfits on a SmartFoxServer avatarChat file. Anyway, the command to do this in-game is server.setUserVariables().

setUserVariables basically sets user variables. There is no other simpler definition than that.

So, I'm going to go ahead and start this tutorial now. This is pretty simple so I'm not going to have any pictures in this tutorial. Plus, the computer I'm currently doesn't have Flash installed.

Anyway, let's begin.

1. Open up your Flash ActionScript 2.0 avatarChat file.

2. Make a button anywhere on the stage and give it any instance name you want.

3. Add the following coding inside the button:

on (release) {
var newCol = frameNumberHere;
_root.smartfox.setUserVariables({col:newCol, init:true});
_root.myAvatar.disc.gotoAndStop(newCol);
}
That's pretty simple coding - only 5 lines of coding. I'm now going to break up each line of ActionScript and explain what each one does.

on (release) { - This handler indicates to perform the action on the release of the button.
var newColor = frameNumberHere; - This line creates a variable. (Replace "frameNumberHere" with the frame number of the new avatar in the myAvatar.disc movieclip.)
_root.smartfox.setUserVariables({col:newCol, init:true}); - This line calls the setUserVariables command and updates the current user's variables. After doing this, it calls the onUserVariablesUpdate event. Anyway, the reason we use the curly brackets inside the normal brackets is because the data inside is an object. "col" is the name of the user variable pre-set in the avatarChat file. "init" - When this is set to true, it updates all clients right then. If it's set to false, then it does the opposite and doesn't update at that time.
_root.myAvatar.disc.gotoAndStop(newCol); - This line of coding updates the client side so that the disc's frame goes to the one defined in the newCol variable.
} - Indicates the end of the coding handler.

That's basically all for this tutorial. If you encounter any problems or errors, or if you don't understand something, make a comment on this post and I will be glad to assist you further.

~Cool Boy 714

7 comments:

  1. How would I make it so when a moderator logs on (names are Oopsyocto155, Slidoo, and Bobhead202) that they're avatar changes into a different avatar, but all of them have different outfits? xD

    ReplyDelete
  2. Bobhead202: For that, you could use this coding:

    if (_global.myName == "Username here") {
    var newCol = frameNumberHere;
    _root.smartfox.setUserVariables({col:newCol, init:true});
    _root.myAvatar.disc.gotoAndStop(newCol);
    }

    You can copy and paste that coding as many times as you want in the "chat" frame of the avatarChat file with changing the username as many times as you want.

    ReplyDelete
  3. Cool Boy 714 & Bobhead202: For this code to work in the example file you need to change myAvatar to avatar.

    e.g

    if (_global.myName == "Username here") {
    var newCol = frameNumberHere;
    _root.smartfox.setUserVariables({col:newCol, init:true});
    _root.avatar.disc.gotoAndStop(newCol);
    }

    ReplyDelete
  4. Great tutorial, how do you do rotation or items?

    ReplyDelete
  5. Anonymous: I have just now posted a tutorial on this which is located on the home page of the site. :P

    ReplyDelete
  6. Would it be possible to make the variables stick with the player after it logs out?

    ReplyDelete
  7. @Anonymous: You would have to use a database to do that, but yes, it is possible.

    ReplyDelete