Getting / setting room variables is impossible

You think you've found a bug? Please report it here.

Moderators: Lapo, Bax

Post Reply
BenjAns
Posts: 16
Joined: 19 Apr 2008, 09:30

Getting / setting room variables is impossible

Post by BenjAns »

Hi everyone !

I lost my day trying to do this thing and now I'm almost sure it's a smartfox bug...

I'm trying to duplicate a room when the previous one reach a certain limit. Of course, I create the new room dynamicaly this way :

Code: Select all

// Get the previous room
var prevRoom = _server.getCurrentZone().getRoom(params.id);
// First, you have to create the room
// Exactly the same as the previously choosed.
var roomObj = {};
// Getting the display number
var dispName = getDisplayName(params.id);
roomObj.name = dispName;
roomObj.maxU = prevRoom.getMaxUsers();
roomObj.isGame = false;
roomObj.isLimbo = false;
roomObj.uCount = true;
roomObj.xtName = "room";
roomObj.xtClass = "mygame_sfs_room_extension.as";
// Room Vars : add the 2 vars
var roomVars = [];
var rVars = prevRoom.getVariables();
trace("-- ADD VARS TO ROOM -----");
for (var i = rVars.entrySet().iterator(); i.hasNext();)
{	
	var rVar = i.next();	
	trace("- Add var "+rVar.getKey()+" with value "+rVar.getValue().getValue());
	roomVars.push({name:rVar.getKey(), val:rVar.getValue().getValue(), priv:true, persistent:false});
}
trace("-- END ADD VARS TO ROOM (add "+roomVars.length+" vars) -----");
trace("");

var newRoom = _server.createRoom(roomObj, user, true, true, roomVars)
// Then, join the room
if (newRoom == null)
	trace("Impossible to create the room. You don't really need to handle this...");
else
	_server.joinRoom(user, fromRoom, true, newRoom.getId());
	

trace("-- CHECK THE VARS FROM ROOM "+newRoom.getName()+" ----------------");
var rVars = newRoom.getVariables();
var cpt = 0;
for (var i = rVars.entrySet().iterator(); i.hasNext();)
{	
	var rVar = i.next();	
	trace("Variable "+rVar.getKey()+" : "+rVar.getValue().getValue());
	cpt++;
}
trace("-- "+cpt+" vars returned ! ----------");
And here's the return :

Code: Select all

[mygame_sfs_zone_extension.as]: -- ADD VARS TO ROOM -----
[mygame_sfs_zone_extension.as]: - Add var RoomCfg with value xml/roomconfig_Garden.xml
[mygame_sfs_zone_extension.as]: - Add var SRoomId with value 2
[mygame_sfs_zone_extension.as]: -- END ADD VARS TO ROOM (add 2 vars) -----
[mygame_sfs_zone_extension.as]: 22:40:11.375 - [ INFO ] > Room Extension [ room ] created!
[mygame_sfs_room_extension.as]: mygame Room Extension v0.1.00 initialized [room:The Garden 1]
[mygame_sfs_zone_extension.as]: -- CHECK THE VARS FROM ROOM The Garden 1 ----------------
[mygame_sfs_zone_extension.as]: -- 0 vars returned ! ----------
I tried it many ways, and it always does exactly the same thing : it parse the room vars, store it, I can trace the number of variables and the structure of the roomVars array, everything is fine, but wen I add it to the room, they're not taken into consideration...
It returns no error, the room is properly created, except I don't have the room vars.

It's certainly a bug, please can you tell me how to fix that asap ?

Thank you !
BenjAns
Posts: 16
Joined: 19 Apr 2008, 09:30

Post by BenjAns »

Lapo is there any solution ?
Menser
Posts: 111
Joined: 13 Nov 2007, 18:32

Post by Menser »

Heya-

I to have previously had problems adding room variables at creation in the same manner you are. Im not sure why i couldnt get them to work either :(

My solution in the end was to create the room, and then create the variables after setting their values.

Any word on whats up with this would be fantastic though.

_-Menser-_
BenjAns
Posts: 16
Joined: 19 Apr 2008, 09:30

Post by BenjAns »

Great someone's responding... Thank you !

Can you explain precisely how did you do that ? It may be the solution for my headaches...
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

What you are both saying, in other words, is that you don't seem to be able to create room variables via the createRoom() call. Right?

Copying other values from a previous room is just the same thing, unless you are doing something wrong when getting the variables from the older room.

I just went back to our test extension library and found exactly this code:

Code: Select all

function init()
{
	// --- Room params -------------------------
	var roomObj = {}
	roomObj.name = "TheGameRoom"
	roomObj.maxU = 10
	roomObj.isGame = true
	
	// --- Vars --------------------------------
	roomVars = 	[
					{name:"Manufacturer", val:"Commodore", priv:true, persistent:true},
					{name:"Model", val:"C64", priv:true, persistent:false},
					{name:"CPU", val:"Motorola 6502", priv:true, persistent:true},
					{name:"RAM", val:64, priv:true, persistent:true},
					{name:"8-Bit", val:true, priv:false, persistent:true},
				]
	
	theRoom = _server.createRoom(roomObj, null, true, true, roomVars)
	
	if (theRoom != null)
	{
		var vars = theRoom.getVariables().values()	
		for (var it = vars.iterator(); it.hasNext();)
		{
			trace(it.next())
		}
	}
	else
		trace("OUCH! Room was not created!!!")
}
Which works perfectly. Room is created, variables are populated and immediately available for enumeration.
It doesn't look like there's a bug in populating variables at the time of a room creation.

If this snippet is not working for you maybe you are using an older server version?

If it does work, then the problem is somewhere else in your code.[/code]
Lapo
--
gotoAndPlay()
...addicted to flash games
BenjAns
Posts: 16
Joined: 19 Apr 2008, 09:30

Post by BenjAns »

What we say is that it works this way, adding vars like you did, but it does not work when you try to catch all the variables of a room and put them into another one...

Try my code, and you'll see :/
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

I understand, but it's also clear that there is no bug in createRoom related with the variables, so the case is closed as far as the bug hunting goes.

With this said it's quite clear that you are having a problem in copying the variables from room to another and by taking a look at your code is easy to understand why.

First of all you are passing all strings. While your original values might be numbers, booleans etc...
The RoomVariable object that you get with the last getValue() method always returns a string. The type of the variable is obtained by calling getType()
This means that there's a little more work to do in order to ensure proper type copying.

Additionally the strings you are getting are of type java.lang.String which might not work as variable values.
You should simply add a method to your code that checks the RoomVar types and populates the array you will be passing to the createRoom call
Lapo
--
gotoAndPlay()
...addicted to flash games
BenjAns
Posts: 16
Joined: 19 Apr 2008, 09:30

Post by BenjAns »

You're right, I figured it out tonight...

SO it's not a bug, but this is not the best part of the doc (wich is great anyway...), as this is not clear and there's some errors (the room variables refers to user variables and in the variable list there's an error : var varValue = rVar.getValue().getValue() )

Thank you for your help, you can move it to the serverside dev instead of bug trapping ;)
Post Reply