checking with you if im setting user variables correctly

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

haplo
Posts: 78
Joined: 08 Jun 2010, 13:32

checking with you if im setting user variables correctly

Postby haplo » 11 Nov 2010, 01:16

im trying to understand the steps involve in updating a single variable for a user . it seems just too much work to add a single element to one of the varaibles. i think im doing something wrong so i want to double check with you guys.


Code: Select all

//i create my own sfsArray of card (this is the new element i want to add)
           SFSArray card  = GameCard.newCard();

//i get the existing variable list form the user
          ArrayList<UserVariable> userVariables = (ArrayList<UserVariable>) user.getVariables();

// i wrote a small util to get the UserVariable from the list based on its name
          UserVariable existingCardList = GameUtil.getVariableFromList(userVariables,"cards") ;
         
// init a new array
          SFSArray sfsarray;

// if the userVariable does not exist  we create a new one ,add our new array, and add it to the user variable list.
to the user variables
          if (existingCardList == null ) {
            
              sfsarray = SFSArray.newInstance();
              sfsarray.addSFSArray(card  );   
              existingCardList  = SFSUserVariable.newInstance("cards", sfsarray) ;
              userVariables.add(existingCardList ) ;
   
            
          }
// if it exist we conver it to sfsArray, add our new array , searching for the variable index, and replacing it with the new one
else {
             sfsarray = (SFSArray)existingCardList.toSFSArray()  ;
             sfsarray.addSFSArray(card  );
             existingCardList  = SFSUserVariable.newInstance("cards", sfsarray) ;
              userVariables.set(GameUtil.getVariableIndexFromList(userVariables,"cards"),  existingCardList ) ;
       
          }
//set to user
this.main.getApi().setUserVariables(user, userVariables);
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 11 Nov 2010, 10:47

If you need one specific variable why don't you use User.getVariable("varName") ? Maybe you didn't notice it in the javadoc?

Setting User or Room vars is a pretty simple process:

Code: Select all

// Prepare a list of variables
List<UserVariable> theVars = new ArrayList<UserVariable>();
theVars. add( new SFSUserVariable("avatar", "Kermit") );
theVars. add( new SFSUserVariable("colorCode", 20) );

getApi().setUserVariables(theUser, theVars);


end of story :)
Lapo
--
gotoAndPlay()
...addicted to flash games
haplo
Posts: 78
Joined: 08 Jun 2010, 13:32

Postby haplo » 12 Nov 2010, 06:21

I understand that
but in my case i want to update SFSArray (adding 1 more element to it)

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 119 guests