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);