User variable and room variable

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

Moderators: Lapo, Bax

User avatar
Wei
Posts: 50
Joined: 13 Jun 2017, 08:16

User variable and room variable

Postby Wei » 08 Jul 2017, 08:43

in my game, all user variables and room variables are created on server side, here is my question: if I update a user variable or room variable on server side, will all users in this room trigger update event automatically or I need to send some thing to all of user in this room?
Hassan Khallouf
Posts: 41
Joined: 06 Jul 2017, 04:35

Re: User variable and room variable

Postby Hassan Khallouf » 08 Jul 2017, 12:45

what function are you using to update your user/room variable ?
if you used the function in the api (this code works inside an extension for example)

Code: Select all

getApi().setUserVariables(user,vars,true,true);


the last 2 booleans in the function are fireClientEvent,fireServerEvent
this will fire the events to update the client and so
and this is the documentations for the method

void setUserVariables(User owner,
java.util.List<UserVariable> variables,
boolean fireClientEvent,
boolean fireServerEvent)
Set User Variables. Only variables that change are broadcast to the Users that can "see" the sender. In order to delete a variable you can set it to null.
Parameters:
owner - the User for which variables are set
variables - a list of UserVariables
fireClientEvent - fires client side Event
fireServerEvent - fires server side Event


the same thing goes for setRoomVariables method
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: User variable and room variable

Postby Lapo » 08 Jul 2017, 14:12

Wei wrote:in my game, all user variables and room variables are created on server side, here is my question: if I update a user variable or room variable on server side, will all users in this room trigger update event automatically or I need to send some thing to all of user in this room?

Yes, updating a Room variable will trigger and update for everyone regardless of which side it was set (client or server)

As Hassan pointed out in his reply, make sure to use

Code: Select all

getApi().setUserVariables(...)

on the server side.

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Wei
Posts: 50
Joined: 13 Jun 2017, 08:16

Re: User variable and room variable

Postby Wei » 08 Jul 2017, 14:47

Got it and thanks.

But I have another one, how could I update room variable and user variable。

I've set up room variables and user variable when User

Code: Select all

UserVariable score = new SFSUserVariable("score", 1);
        List<UserVariable> userVars = new ArrayList<>();
        userVars.add(score);
        getApi().setUserVariables(theUser, userVars);
   
        RoomVariable round = new SFSRoomVariable("round", 1);
        round.setPrivate(true);
        round.setGlobal(true);
       
        RoomVariable remain = new SFSRoomVariable("remain", 30);
        remain.setGlobal(true);
        remain.setPrivate(true);
       
        List<RoomVariable> roomVars = Arrays.asList(round, remain);
       
        Room room = getParentExtension().getParentZone().getRoomByName(roomName);
        getApi().setRoomVariables(theUser, room, roomVars);
       


And how can I update them? I've searched documents, it looks like no method for doing that. Or I just need to do new SFSRoomVariable() again?

Code: Select all

List<Room> Rooms = user.getJoinedRooms();
                Room currentRoom = Rooms.get(0);
                RoomVariable roomVar = currentRoom.getVariable("round");
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: User variable and room variable

Postby Lapo » 09 Jul 2017, 16:45

Wei wrote:Got it and thanks.
And how can I update them? I've searched documents, it looks like no method for doing that. Or I just need to do new SFSRoomVariable() again?

Yes, you just set it again. Using a new object.

cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Wei
Posts: 50
Joined: 13 Jun 2017, 08:16

Re: User variable and room variable

Postby Wei » 09 Jul 2017, 17:57

sure, tanks
User avatar
Wei
Posts: 50
Joined: 13 Jun 2017, 08:16

Re: User variable and room variable

Postby Wei » 25 Jul 2017, 02:11

here comes another one, when a user join a game room, the game room has some room variables, how could I get all those room variables when I join the room, or it'll trigger room_variable_change event directly when I join the room?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: User variable and room variable

Postby Lapo » 25 Jul 2017, 06:38

RoomVariables are sent out to the client in the ROOM_JOIN event.
In that event you are passed a Room object and you can call getVariables() on it to get all vars in the Room.

cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 56 guests