Reading server-owned roomVars change without onJoin?

Need help with SmartFoxServer? You didn't find an answer in our documentation? Please, post your questions here!

Moderators: Lapo, Bax

Rashomon
Posts: 72
Joined: 11 Aug 2010, 19:48

Reading server-owned roomVars change without onJoin?

Postby Rashomon » 22 Sep 2011, 17:19

I'm working on a quickJoin game feature in SFS Pro and Actionscript 3.

I'm having trouble pulling the correct value of a server-owned roomVar if it is upsted AFTER I join the zone.

Here's what I mean...

When a game starts, I call my server-side extension which sets an "isStarted" roomVar. The roomVar is server-owned. Here's my code:

Code: Select all

function startGame()
{   
   var room = _server.getCurrentRoom()
   
   var rVars = [];
   rVars.push( {name:"isStarted", val:"true", priv:false, persistent:true} );
   _server.setRoomVariables(room, null, rVars);

   var res = {};
   res._cmd = "start";
   
   _server.sendResponse(res, currentRoomId, null, users);
}   



Back on the client, I get all of the rooms in the zone, and exclude any where "isStarted == 'true'"... like so:

Code: Select all

function onQuickJoin( evt:MouseEvent ):void
{
   var gameToJoin:String = "";

   gameRoomArray = smartFox.getAllRooms();

   for (var r:String in gameRoomArray)
   {
      var gameRoom:Room = gameRoomArray[r]
      trace("Room: " + gameRoom.getName())
      if ((gameRoom.getName() != "Lobby")&&(gameRoom.getVariable("isStarted") != "true")) {
         trace("Game name: " + gameRoom.getName() + ", Id: " + gameRoom.getId() + ", isStarted: " + gameRoom.getVariable("isStarted") as String);
         gameToJoin = gameRoom.getName();
         break;
      }
   }
   if (gameToJoin != "") {
      smartFox.joinRoom( gameRoom.getName(), "" );
   } else {
      // Create waiting room, which then creates new game room and joins player
      trace("Create new game and join: g" + smartFox.myUserId);
      var newGameRoom:Object = new Object();
      newGameRoom.name = "game" + smartFox.myUserId;
      //newGameRoom.password = pwd
      newGameRoom.maxUsers = 3
      newGameRoom.maxSpectators = 0
      newGameRoom.isGame = true
      newGameRoom.isTemp = true
      var xt:Object = {}
      xt.name = "myXtName"
      xt.script = "myGameRoom.as"
      newGameRoom.extension = xt
      smartFox.createRoom( newGameRoom )   
   }
}


I added a "showGames" button, which traces the room info:

Code: Select all

function onShowRooms( evt:MouseEvent ):void
{
   trace("-----------");
   trace("onShowRooms");
   
   gameRoomArray = smartFox.getAllRooms();
   
   trace(gameRoomArray.length + " rooms found");
   
   for (var r:String in gameRoomArray)
   {
      var gameRoom:Room = gameRoomArray[r]
      trace("Room: " + gameRoom.getName())
      if (gameRoom.getName() != "Lobby") {
         trace("Name: " + gameRoom.getName() + ", Id: " + gameRoom.getId() + ", isStarted: " + gameRoom.getVariable("isStarted") as String);
      }
   }
}


When a game is in progress, I am able to pull the correct value of isStarted for that room, but ONLY if I join the zone (in this case, the lobby) AFTER the game has started (and the isStarted flag has been set).

However, If I'm already in the zone/lobby BEFORE the game starts, then isStarted shows as "undefined".

My understanding is that SFS sends an updated room list whenever you join a room. I'm trying to manually pull in a fresh copy of the room list by using smartFox.getAllRooms(). Am I using this incorrectly, or is there some other reason why this isn't working?
User avatar
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Postby BigFIsh » 22 Sep 2011, 22:00

My understanding is that SFS sends an updated room list whenever you join a room. I'm trying to manually pull in a fresh copy of the room list by using smartFox.getAllRooms(). Am I using this incorrectly, or is there some other reason why this isn't working?


Not quite. The server is only sending the newly added room(s) which will be added to the existing list. You should only need to call smartFox.getRoomList() once upon logging in and after exiting a game room.

The server does not send updates for room variables for users outside the room. You will need to manually send the updates to clients in lobby room via extension code using sendResponse.
Smartfox's forum is my daily newspaper.
Rashomon
Posts: 72
Joined: 11 Aug 2010, 19:48

Postby Rashomon » 23 Sep 2011, 15:20

Thanks, BigFish. That makes sense.

I'm sending a response to the client to update the isStarted roomVar, but it doesn't seem to be taking effect. I suspect I am not updating the target (other) roomVar correctly.

Here's what I have:

Server: (Send "closeRoom" message to everyone in the Lobby to close this room)

Code: Select all

var res = {};
res._cmd = "closeRoom";
   
var closedRoom = _server.getCurrentRoom();
res.startedGameName = closedRoom.getName();
res.startedGameId = closedRoom.getId();
   
var zone = _server.getCurrentZone();
var targetRoom = zone.getRoomByName("Lobby");

_server.sendResponse(res, -1, null, targetRoom.getAllUsers(), "xml");


Client: (Update isStarted roomVar for the target room)

Code: Select all

function executeMessage( resultObj:Object, msgType:String )
{
var type:String = msgType
     if ( type == SmartFoxClient.XTMSG_TYPE_XML )
     {
          switch ( cmd )
          {   
   case "closeRoom":
             rVars.push({name:"isStarted", val:"true", persistent:true});
             smartFox.setRoomVariables(rVars, resObj.startedGameName, false);         
   break
          }
     }
}


However, when I check the isStarted variable for the rooms in the zone (via onShowRooms, below), isStarted still shows up as undefined. Furthermore, the onRoomVariablesUpdate event never fires:

Client: (Show roomVars that were changed)

Code: Select all

function onRoomVariablesUpdate(evt:SFSEvent):void
{
   trace(">>> onRoomVariablesUpdate");
    var changedVars:Array = evt.params.changedVars
   for (var v:String in changedVars) {
       trace("Room var " + v + " = " + evt.params.room.getVariable(v));
   }
}

What am I doing wrong?
User avatar
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Postby BigFIsh » 23 Sep 2011, 23:22

Ah, that's not how it's done.

Code: Select all

case "closeRoom":
     rVars.push({name:"isStarted", val:"true", persistent:true});
     smartFox.setRoomVariables(rVars, resObj.startedGameName, false);         
break


This code is used to set or update a new or existing room variable for that room, which will be boardcasted to all users inside that room.

You need to manually update the room variable by other means such as: room.getVariables()["isStarted"] = "true", and then manually trigger the require event to update the room list.
Smartfox's forum is my daily newspaper.
Rashomon
Posts: 72
Joined: 11 Aug 2010, 19:48

Postby Rashomon » 26 Sep 2011, 13:38

You need to manually update the room variable by other means such as: room.getVariables()["isStarted"] = "true"


I don't follow you. How can I set a variable with getVariables? I thought that was read-only.

and then manually trigger the require event to update the room list.


Do you mean smartFox.getRoomList() ?

I tried this:

Code: Select all

rVars.push({name:"isStarted", val:"true", persistent:true});
smartFox.setRoomVariables(rVars, resObj.startedGameName, false);
room.getVariables()["isStarted"] = "true";         
smartFox.getRoomList();


and this:

Code: Select all

room.getVariables()["isStarted"] = "true";         
smartFox.getRoomList();


but neither worked.
Rashomon
Posts: 72
Joined: 11 Aug 2010, 19:48

Postby Rashomon » 27 Sep 2011, 14:35

This code is used to set or update a new or existing room variable for that room, which will be broadcasted to all users inside that room.


I don't mind if the update is broadcasted to users in the room. I just want to be able to access the updated roomVar from outside of the room.

then manually trigger the require event to update the room list.


Will updating the roomList refresh the roomVar that I just updated? If so, how can I update the roomList? I'm currently trying to use smartFox.getAllRooms(). According to this thread, though, this only works when the user logs in.

when a variable updates on another room you don't receive the update. And I think the roomList method only works at the login time.


I need to get the updated/fresh room vars for the users in the lobby as if they just joined the lobby (after the roomVar update). How can I do this?
User avatar
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Postby BigFIsh » 27 Sep 2011, 22:57

I see some confusion here.

smartFox.getRoomList() will trigger a call to the server to return a full list of all the rooms which in turn will trigger the local room list update function. That's not what you want here.

After receiving the variable update from the server (via onExtensionResponse), you need to call a local function (not a server-interactive method) which will repopulate the rooms in your game list or for that particular room locally. You simply just need to get that game room to 'refresh' itself after setting its variable.

room.getVariables() returns an object of variables associated with that room. That object will hold dynamic variables which can then be modified. A simple test to verify this:

Code: Select all

//assuming that isStarted is false
room.getVariables()["isStarted"] = "true"; //or room.getVariable("isStarted") = "true"
trace(room.getVariable("isStarted")) //should trace out true.
Smartfox's forum is my daily newspaper.
Rashomon
Posts: 72
Joined: 11 Aug 2010, 19:48

Postby Rashomon » 28 Sep 2011, 12:32

Hey! That did it! Thank you sooooo much! This problem was about to derail my project.

I'm curious, though. Why did this work?

Code: Select all

rVars.push({name:"isStarted", val:"true", persistent:true});
smartFox.setRoomVariables(rVars, resObj.startedGameName, false);
trace(closedRoom.getVariable("isStarted")) // Traces out true.


But not this?

Code: Select all

var closedRoom:Room = smartFox.getRoomByName(resObj.startedGameName);
closedRoom.getVariables()["isStarted"] = "true"; //or room.getVariable("isStarted") = "true"
trace(closedRoom.getVariable("isStarted")) // Traces out undefined.
User avatar
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Postby BigFIsh » 28 Sep 2011, 19:10

Mm.. I would have expected that to work. I havn't tried that code though. So I must've assumed wrong. :oops:

Anyway, glad you got it working.
Smartfox's forum is my daily newspaper.

Return to “SmartFoxServer 1.x Discussions and Help”

Who is online

Users browsing this forum: No registered users and 21 guests