reading roomVariables problem

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

Moderators: Lapo, Bax

Post Reply
Virusescu
Posts: 260
Joined: 07 Sep 2005, 09:36
Location: [RO]Bucharest
Contact:

reading roomVariables problem

Post by Virusescu »

Simple question first.
Can I access, from the client code, all the roomVariables from all the rooms in the zone I'm currently connected to (roomList)? If so, how?
I'm strugling with the roomVariables tutorial and I can't seem to find an answer.

I'm setting my variables directly from the server in the config.xml like

Code: Select all

<Room name="Room1" maxUsers="12" isPrivate="false" isTemp="false" uCountUpdate="true">
					<Vars>
						<Var name="initBet" type="n" private="true">500</Var>
						<Var name="gameType" type="s" private="true">Challenge</Var>
					</Vars>
				</Room>
But it seems I'm dooing something wrong reading them. I can see the variables set with the admin tool.
I first tried like this

Code: Select all

smartfox.onRoomListUpdate = function(roomList:Object){
        this.autoJoin();	
        for (var r in roomList){
		trace("Room: "+r+" > "+roomList[r].getName()); // traces corectly
		vars = roomList[r].getVariables();
		for (var v in vars) {		
			trace("Var "+v+"="+vars[v]); // traces nothing
		}
                // in fact it doesn't enters the for because the vars is an empty object as it turned out at the debugging 
	}
}
I've then added an onRoomVariablesUpdate event but it's not beeing called
Then in the roomVariables fla example it states "// variables = a property Object containing all room variables" - for the roomObject, as opposed from the documentation where we have getVariables() and getVariable() methods of the Room object.

One other question. It seems to me that the user that sets a room variable doesn't recieve the onRoomVariablesUpdate event. Is that correct? Or am I dooing something wrong?

Can you tell if I'm dooing someting wrong?
I'm building a lobby and I need to access other variables besides the names of one room and display them in the lobby. Is this possible or do i need to write a method for the lobby extension?
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Can I access, from the client code, all the roomVariables from all the rooms in the zone I'm currently connected to (roomList)? If so, how?
Nope, you can't. You can only read the variables of the rooms that you joined. This "limitation" is necessary or the server would send tons of data to all clients in order to keep them updated about all variables in all rooms.

You can use a server side extension to access any room variable in any room of the Zone.
One other question. It seems to me that the user that sets a room variable doesn't recieve the onRoomVariablesUpdate event. Is that correct? Or am I dooing something wrong?
I think you're doing something wrong because you should receive the event immediately after sending the request.

Another thing: by default you don't get the RoomVariables when you receive the RoomList. That's why you have that problem.
Again this was done for optimizing the bandwidth usage.

You can recieve them by setting to the roomListVars attribute to true, in the <Zone></Zone> tag
Lapo
--
gotoAndPlay()
...addicted to flash games
Virusescu
Posts: 260
Joined: 07 Sep 2005, 09:36
Location: [RO]Bucharest
Contact:

Post by Virusescu »

Thank you for the quick answer.

Have a nice day :)
Virusescu
Posts: 260
Joined: 07 Sep 2005, 09:36
Location: [RO]Bucharest
Contact:

Hello again. Another problem from me :(

Post by Virusescu »

I'm still strugling to get the room variables defined in the xml configuration file when the user enters the lobby.
The extension has a script like this

Code: Select all

// this is called when a XT message is recieved
function displayRoomList (userObj)
{
	trace(userObj.getName()+" wants to see the rooms");
	var response = new Object;
	response._cmd = "displayRoomList";
	response.rooms = new Array();
	var rooms = _server.getCurrentZone().getRooms();
	for (var r in rooms){
		trace("Room "+rooms[r].getName()+" is in this zone");
		vars = rooms[r].getRoomVariables(); // ERROR
		nri = "";
		nri += rooms[r].getName()+",";
		nri += rooms[r].getId()+",";
		nri += rooms[r].getUserCount()+",";
		nri += rooms[r].getRoomVariable("initBet")+","; // ERROR
		nri += rooms[r].getRoomVariable("gameType")+","; // ERROR
		response.rooms.push(nri);
		trace("Built string->"+nri);	
	}
	_server.sendResponse (response, - 1, null, [userObj])
}
I have managed to separate the errors. Although rooms[r] are all valid Room Objects I can't seem to be able to access the variables defined in the XML beacuse of an error like the following
Error in extension [ thu_login.as ]: TypeError: getRoomVariable is not a function. (thu_login.as#726) Line: 37
I can't seem to find the availability of this methods of the Room Object. I thought this are available since the introduction of the Room Object in version 1.2.1 PRO. Is that corect?
If that so, can you see anything wrong with the code above?

Thank you
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

what version are you using at the moment?
Lapo
--
gotoAndPlay()
...addicted to flash games
Virusescu
Posts: 260
Joined: 07 Sep 2005, 09:36
Location: [RO]Bucharest
Contact:

Post by Virusescu »

I use Version 1.3.0 PRO
Although. in my admin console it states Remote Administration v 1.2.2
and at the bottom "Connected to ip --- SmartFoxServer PRO - ver 1.3.0"
But I'm guessing 1.2.2 si the version of the admin tool.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

You're right.
There's a problem here with the way room variables are read.
Since the Room object is a native java object it will return another native java object, specifically an HashMap

So:
roomObj.getVariables()
returns a java.util.HashMap

Here's how you can cycle through it:

Code: Select all

var rVars = roomObject.getVariables()

// We cycle through the HashMap and trace each variable
for (var i = rVars.entrySet().iterator(); i.hasNext();)
{
	var rVar = i.next()
	var varName = rVar.getKey()
	var varValue = rVar.getValue().getValue()
	
	trace("var: " + varName + ", val: " + varValue)
}
A quick explanation. An HashMap is like an associative array, so each values is stored with its own key.

the entrySet() method returns a set of objects with a getKey() and getValue() methods.

The variable name is stored in the key of the array
The variable itself is the value.

I will update the documentation soon with more details :)
Lapo
--
gotoAndPlay()
...addicted to flash games
Virusescu
Posts: 260
Joined: 07 Sep 2005, 09:36
Location: [RO]Bucharest
Contact:

Post by Virusescu »

Finally SPOTTED.
You have a typo in the docs here - http://www.smartfoxserver.com/docs/docP ... s/Room.htm

the room's methods are spelled in the documentation as getRoomVariables and getRoomVariable.

Thanks for the input.
Back to work :).
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Thanks for spotting it. I've updated the page

:)
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply