User 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

Postby Wei » 24 Jul 2017, 20:48

I create a game room, it needs 3 users to start the game, if I set 3 user variables for each user when they join the room, which are userID, round and record, when I change one of those user's variables, how can other client side know who's variable is changed?

User0:
userID = 100000
round = 1
record = -5

User1:
userID = 100001
round = 3
record = 10

User2:
userID = 100002
round = 1
record = 15

if I change user2's round to 5, how could user0 and user1 know about it?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: User variable

Postby Lapo » 25 Jul 2017, 06:57

They must listen for the USER_VARIABLES_UPDATE event.
The event will tell you which user has updated his variables. I recommend taking a look at the SFSEvent.USER_VARIABLES_UPDATE documentation for more details.

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

Re: User variable

Postby Wei » 25 Jul 2017, 23:13

I've found those 2:
user (User) An object representing the user who updated his own User Variables.
changedVars (List<string>) The list of names of the User Variables that were changed (or created for the first time).

Could I get user and changedVars by using the lines below?
void OnUserVarUpdate(BaseEvent e) {

User theUser = (User) e.Params["user"];
List<string> Vars = e.Params["changedVars"];
}

In my case, if User0 and User1 have already joined the game room when User2 join the game room, how could User2 get User0 and User1's variables? Or it'll trigger USER_VARIABLES_UPDATE immediately when a user join a room? If one round of game is over, server side will change every user's variables, is the USER_VARIABLES_UPDATE will be triggered multiple times?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: User variable

Postby Lapo » 26 Jul 2017, 06:46

Wei wrote:Could I get user and changedVars by using the lines below?
void OnUserVarUpdate(BaseEvent e) {

User theUser = (User) e.Params["user"];
List<string> Vars = e.Params["changedVars"];
}

Yes the code is correct, but you need to cast the 2nd one to List<string> as well.

In my case, if User0 and User1 have already joined the game room when User2 join the game room, how could User2 get User0 and User1's variables?

You can get the list of users from the Room object ( Room.getUserList() ) then you can loop through each User and check their variables using User.getVariable(...)

Or it'll trigger USER_VARIABLES_UPDATE immediately when a user join a room?

No the event will not trigger for variable updates that occurred before you entered the Room.
When you enter the Room you need to check the what variables exist, be it in the Room or in specific Users.

If one round of game is over, server side will change every user's variables, is the USER_VARIABLES_UPDATE will be triggered multiple times?

Yes, of course.

Cheers
Lapo

--

gotoAndPlay()

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

Re: User variable

Postby Wei » 01 Aug 2017, 19:51

I have a new problem. Once a user join a room, front side will disable lobby canvas and show table canvas, and table.cs will take over, in that case, SFSEvent.ROOM_JOIN will not be triggered, because user has already in a room, right?

Here is my question, how could I get current Room? there is none event triggered, or I just send an extension request to get BaseEvent, so that I could use

Code: Select all

Room theRoom = (Room) e.Params["room"];


I found out if the room has any room variable, SFSEvent.ROOM_VARIABLES_UPDATE will be triggered immediately when a user join the room, I just need to get Room from this event,right?

I've looked documents and tried it, but I just need to be 100% sure about what I've done is correct.
User avatar
Wei
Posts: 50
Joined: 13 Jun 2017, 08:16

Re: User variable

Postby Wei » 02 Aug 2017, 01:07

I have another one, on server side, user variable and room variable are like key-value, I could put any type of variable, but on client side, I find some methods like GetBoolValue, GetDoubleValue, I'm a little confused, those methods only convert variable type?

server side

Code: Select all

new SFSRoomVariable("Round", 123),
new SFSRoomVariable("Current", "round1"),
new SFSRoomVariable("Finished", true)


client side

Code: Select all

List<RoomVariable> roomVars;

theRoom = (Room) evt.Params["room"];

roomVars = theRoom.GetVariables();
bool isFinished = roomVars[2]. GetBoolValue()


so that I don't need to do some thing like below

Code: Select all

bool isFinished = (bool) roomVars[2].Value;
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: User variable

Postby Lapo » 02 Aug 2017, 07:38

Wei wrote:I have a new problem. Once a user join a room, front side will disable lobby canvas and show table canvas, and table.cs will take over, in that case, SFSEvent.ROOM_JOIN will not be triggered, because user has already in a room, right?

Sorry I don't understand your question.
SFSEvent.ROOM_JOIN is triggered once in response to a JoinRoom request.

Here is my question, how could I get current Room? there is none event triggered, or I just send an extension request to get BaseEvent, so that I could use

Code: Select all

Room theRoom = (Room) e.Params["room"];


When the ROOM_JOIN event is triggered it passes you the Room object of the just-joined Room.
What you're doing in your code seems correct.

I found out if the room has any room variable, SFSEvent.ROOM_VARIABLES_UPDATE will be triggered immediately when a user join the room, I just need to get Room from this event,right?

When you join a Room you're given the Room object that represent it. The Room object contains all the RoomVars that are available at the time of joining.

So all you need to do is check which variables are available, if any.
ROOM_VARIABLES_UPDATE will be triggered to give you updates later, if any changes are applied to any of the Room Variables.

In other words:
1) When you join a Room all variables are sent to you with the Room object
2) All future changes to variables in that Room will be sent later via ROOM_VARIABLES_UPDATE

Makes sense?
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: User variable

Postby Lapo » 02 Aug 2017, 07:42

Wei wrote:I have another one, on server side, user variable and room variable are like key-value, I could put any type of variable, but on client side, I find some methods like GetBoolValue, GetDoubleValue, I'm a little confused, those methods only convert variable type?

No, Room and User Variables work the same server and client side.
You can't just use value.

Just like you have GetBool, GetDouble etc... you have the corresponding putBool, putDouble.
Supported types are: Null, Bool, Int, Double, String, SFSObject, SFSArray

Cheers
Lapo

--

gotoAndPlay()

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

Re: User variable

Postby Wei » 02 Aug 2017, 13:00

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

Re: User variable

Postby Wei » 02 Aug 2017, 13:21

I cannot find any putBool or putInt relate to room variable

Code: Select all

private void setSomeVariables(Room room)
{
    List<RoomVariable> listOfVars = new ArrayList<RoomVariable>();
    listOfVars.add( new SFSRoomVariable("bgImage", "coolBackground.jpg") );
    listOfVars.add( new SFSRoomVariable("stars", 4) );
     
    RoomVariable hiddenVar = new SFSRoomVariable("isPremiumUsersOnly", true);
    hiddenVar.setHidden(true);
    listOfVars.add(hiddenVar);
     
    // Set variables
    sfsApi.setRoomVariables(null, room, listOfVars)
}   
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: User variable

Postby Lapo » 02 Aug 2017, 14:15

Yeah, sorry my mistake. I was going by memory. :)

You create a Room Variable using the constructor:

Code: Select all

RoomVariable rv = new RoomVariable("myVar", 100);


Supported types are those I mentioned earlier.

Cheers
Lapo

--

gotoAndPlay()

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

Re: User variable

Postby Wei » 02 Aug 2017, 22:02

Thanks man, you rock

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 49 guests