Room variables not populated in the client

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

Moderators: Lapo, Bax

votagusvotag
Posts: 38
Joined: 07 Nov 2016, 12:26

Room variables not populated in the client

Postby votagusvotag » 29 Jun 2017, 18:32

Hi, I have a problem in the client trying to access a room variable created in the server, here is my code:

Code: Select all

...
            crs.setAutoRemoveMode(SFSRoomRemoveMode.NEVER_REMOVE);
            String lobbyName = LOBBY_NAME_PREFIX + serverId.toString();
            crs.setName(lobbyName);
            crs.setGroupId("conquestLobby");
            crs.setMaxUsers(100);
            crs.setDynamic(true);
            crs.setExtension(res);

            // This variable have the status of all the checkpoints, 0 is okay, 1 is hacked.
            SFSObject checkpoints = new SFSObject();
            checkpoints.putInt("lost", 0);
            checkpoints.putInt("total", 6);
            RoomVariable checkpointsVariable = new SFSRoomVariable("checkpoints", checkpoints);
            checkpointsVariable.setPrivate(true);
            checkpointsVariable.setGlobal(false);
            checkpointsVariable.setHidden(false);

            RoomVariable defendersAllowed = new SFSRoomVariable("defendersAllowed", new SFSArray());
            defendersAllowed.setPrivate(false);
            defendersAllowed.setGlobal(false);
            defendersAllowed.setHidden(false);

            // We also put in the lobby the server id to retrieve the battle room reference on init.
            crs.setRoomVariables(Arrays.asList(serverIdVariable, checkpointsVariable, defendersAllowed));
            try {
                Room lobbyRoom = getApi().createRoom(user.getZone(), crs, user);
...               


The variables are setted to the room in the config, even before the room is created. Now when I receive the OnRoomJoin event or OnUserEnterRoom in the client, I try to access those variables but sometimes I can't because the room does not contains the variable yet, I do receive the update of those variables (if they change) and then I can access them. Aren't those variables supposed to be available already if they were setted in the config class? Do I have to do it on the init method of the extension? Also this doesn't happens all the time, but very often. Thanks.

Server side version: 2.11.1.
C# Api version 1.7.3.
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Room variables not populated in the client

Postby Lapo » 29 Jun 2017, 19:16

Hi,
as you said correctly, the variables are created when the Room is created so they are there before any uses joins the Room.

Keep in mind that variables can be seen by a User when he joins the Room. The UserEnterRoom event only tells you that another user has joined a Room you're already in. So this is not relevant to what you're asking.

So let's say you create Room A from server side with a number of variables (as in your Example)
Then take User1 and join Room A.
In the client side ROOM_JOIN event you will be able to see all those variables. There should be no exception to this.

Hope it helps.

ps: If the problem persists send us the transcript of a User session by activating the Debug flag of the SmartFox class. The logs will show up in the Unity console.
Lapo
--
gotoAndPlay()
...addicted to flash games
votagusvotag
Posts: 38
Joined: 07 Nov 2016, 12:26

Re: Room variables not populated in the client

Postby votagusvotag » 29 Jun 2017, 21:58

Code: Select all

        public override void OnUserEnterRoom(BaseEvent evt)
        {
            User user = (SFSUser)evt.Params["user"];
            Room room = (Room)evt.Params["room"];
            Debug.Log("User enter room: " + user.Name + " " + room.Name);

            if (lobbyRoom == room) // Just a sanity check.
            {
                // It should never be the current user anyway.
                if (!user.IsItMe)
                {
                    var instance = Instantiate(playerItemPrefab);
                    instance.SetActive(true);
                    instance.transform.SetParent(spectatorsUIList.transform, false);

                    var item = instance.GetComponent<PlayerItem>();
                    item.playerName.text = (user.Name).ToUpper();
                    item.Id = user.Id;

                    bool allowed = false;
                   
                    if (lobbyRoom.ContainsVariable("defendersAllowed"))
                    {
                        var defendersAllowed = lobbyRoom.GetVariable("defendersAllowed").GetSFSArrayValue();
                        allowed = defendersAllowed.Contains(user.Name);
                    }

                    UpdatePlayerItemUI(user, item, allowed);
                }
            }
        }


That's my code when an user enters the room, as you can see, I have to check if the variable defendersAllowed exist, but I shouldn't since that variable was set before the creation of the room and to listen to that event I have to be in the room already.

Also I can't figure out how to see the debug, this is my code, but the console shows nothing new.

Code: Select all

        public void Conection(string host, string Zone, int portTCP)
        {
            if (!isInitialized) InitializeSFS();
            ConfigData cfg = new ConfigData()
            {
                Host = host,
                Zone = Zone,
                UseBlueBox = false,
                TcpNoDelay = true,
                Debug = true
            };
            sfs.ThreadSafeMode = true;
            sfs.Logger.LoggingLevel = Sfs2X.Logging.LogLevel.DEBUG;
            sfs.Connect(cfg);
        }


Is it something wrong there? Thanks.
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Room variables not populated in the client

Postby Lapo » 30 Jun 2017, 09:25

Hi,
votagusvotag wrote:That's my code when an user enters the room, as you can see, I have to check if the variable defendersAllowed exist, but I shouldn't since that variable was set before the creation of the room and to listen to that event I have to be in the room already.


Okay. Question:

Code: Select all

if (lobbyRoom == room)

Why do you have to check this?
Are your users allowed to be in more than one Room at a time? Just to better understand...

Minor nitpick:

Code: Select all

if (!user.IsItMe)

This is unnecessary. USER_ENTER_ROOM never fire for the user iteself, only when other players enter the Room you have joined.
If you use multi-room it will fire for all Rooms.

Back to your issue. Is this is a random issue? Can you reproduce it? If so how?

Also I can't figure out how to see the debug, this is my code, but the console shows nothing new.

Code: Select all

        public void Conection(string host, string Zone, int portTCP)
        {
            if (!isInitialized) InitializeSFS();
            ConfigData cfg = new ConfigData()
            {
                Host = host,
                Zone = Zone,
                UseBlueBox = false,
                TcpNoDelay = true,
                Debug = true
            };
            sfs.ThreadSafeMode = true;
            sfs.Logger.LoggingLevel = Sfs2X.Logging.LogLevel.DEBUG;
            sfs.Connect(cfg);
        }


Is it something wrong there? Thanks.[/quote]
Not that I can see. You should see the debug data in the Unity console.

Alternatively you can also capture the debug data from server side, by setting the logging level to DEBUG.
http://docs2x.smartfoxserver.com/Gettin ... figuration

Maybe this is event better as the whole client/server conversation is captured to the log files so you can send those to use directly.

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
votagusvotag
Posts: 38
Joined: 07 Nov 2016, 12:26

Re: Room variables not populated in the client

Postby votagusvotag » 30 Jun 2017, 14:44

1- Yes, depending on the situation, the user can be in more than one room, in the menu and in a sort of lobby for example.
2- The check if the user joined is not me I think it was because sometimes if the user joins other room that event will be fired but I have nothing to do there, but I don't know if that still happens.
3- It is a random issue in the sense that it doesn't happens all the time and I have to try several times to catch the error.

The log file is attached, there is a lot of calls that I didn't rip off just to be sure the log keeps its integrity.
What's happening there is:
1- The user Hanzo login at the line 258.
2- The user gabrielvp login at the line 695.
3- Two rooms are created, a game room and a normal room that works as a lobby at the line 1482.
4- The user gabrielvp logins to the game room.
4- At the line 1507 three variables are created for the conquestLobby room, the last two are public.

Code: Select all

10:13:30,382 DEBUG [SFSWorker:Ext:4] entities.SFSRoom     - RoomVar created: { N: serverId, T: INT, V: 8683, Pr: true, Ps: false, G: false, H: true, Owner: <Server> } in [ Room: conquestLobby8683, Id: 59, Group: conquestLobby, isGame: false ]
10:13:30,382 DEBUG [SFSWorker:Ext:4] entities.SFSRoom     - RoomVar created: { N: checkpoints, T: OBJECT, V: [SFSObject, size: 2], Pr: true, Ps: false, G: false, H: false, Owner: <Server> } in [ Room: conquestLobby8683, Id: 59, Group: conquestLobby, isGame: false ]
10:13:30,382 DEBUG [SFSWorker:Ext:4] entities.SFSRoom     - RoomVar created: { N: defendersAllowed, T: ARRAY, V: [SFSArray, size: 0], Pr: false, Ps: false, G: false, H: false, Owner: <Server> } in [ Room: conquestLobby8683, Id: 59, Group: conquestLobby, isGame: false ]


5- The user Hanzo joins the conquestLobby room at the line 1752 and then the error occurs, in the client none of the two variables can be seen, then they get an update and then they are visible.

Thanks.
Attachments
log.rar
(19.91 KiB) Downloaded 426 times
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Room variables not populated in the client

Postby Lapo » 01 Jul 2017, 09:02

Thanks.
From the logs it's clear that the variables are sent to the client, as they are visibile in the packet data.

5- The user Hanzo joins the conquestLobby room at the line 1752 and then the error occurs, in the client none of the two variables can be seen, then they get an update and then they are visible.


My suggestion is to run something like this in your ROOM_JOIN handler on the client to see exactly what is going on.

Code: Select all

private OnRoomJoin(BaseEvent evt)
{
   Room theRoom = (Room) evt.Params["room"]
   
   foreach (var roomVar in theRoom.getVariables())
   {
      Debug.Log("Name: " + roomVar.Name + " = " + roomVar.Value)
   }
}

NOTE: the snippet above checks the variables using the Room object passed by the event. Maybe in your code you're holding onto a reference acquired previously? I think that could be a potential cause of error.

In any case let me know if the test works.
Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 93 guests