Room Variables from server

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

jaeta
Posts: 6
Joined: 16 Jan 2007, 00:20

Room Variables from server

Post by jaeta »

I'm trying to update room variables from the server, but it does not appear to be working. I'm certainly getting a response back from the server after the client calls the extension, but the actual variables I have on the server never change. The variables are being set by the server initially in the server "config.xml" file. Anyone have any suggestions?

Many Thanks!

Code: Select all

<Zone name="multitest" customLogin="false" buddyList="20">
  <Rooms>
     <Room name="MainRoom" maxUsers="10" isPrivate="false"   
       isTemp="false" autoJoin="true">
     <Vars>
         <Var name="BlueScore" type="n" private="false">0</Var> 
         <Var name="RedScore" type="n" private="false">0</Var>
     </Vars>
     </Room>
   </Rooms>
   <Extensions>
   <extension name="multitest" className="multitest.as" type="script"/>
   </Extensions>
</Zone>

Code: Select all

function handleRequest(cmd, params, user, fromRoom) {
	if (cmd == "scores") {
		var room = _server.getCurrentRoom();
		BlueScore = Number(params.values[0]);
		RedScore = Number(params.values[1]);
		vObj = new Array();
		vObj.push({name:"BlueScore", val:BlueScore, priv:false});
		vObj.push({name:"RedScore", val:RedScore, priv:false});
		_server.setRoomVariables(room, null, vObj);
		_server.sendResponse(params, -1, null, [user]);
	}
}
[/code]
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Hi and welcome,
instead of this code:

Code: Select all

_server.setRoomVariables(room, null, vObj);
I'd use this:

Code: Select all

_server.setRoomVariables(room, user, vObj);
Let me know if it works
Lapo
--
gotoAndPlay()
...addicted to flash games
jaeta
Posts: 6
Joined: 16 Jan 2007, 00:20

Post by jaeta »

Won't this kill the room variable when a user leaves the room? I want this game to be ongoing regardless of the number of players in the game. The score variable should stay on the server until the score either reaches a determined value or everyone leaves the game.
jaeta
Posts: 6
Joined: 16 Jan 2007, 00:20

Post by jaeta »

I tried using the code you provided, but it's still not updating the room variables on the server.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

A couple of questions:
Are you checking if the variables are changed from the admin tool? (refresh the view after you have updated the variables)

What server version do you use?
Lapo
--
gotoAndPlay()
...addicted to flash games
jaeta
Posts: 6
Joined: 16 Jan 2007, 00:20

Post by jaeta »

I am refreshing the admin tool and it still shows the score as being 0,0 even though I'm passing values of 10,10

Code: Select all

extensionName="multitest"
var obj:Object = {};
obj.values = [10, 10];
smartfox.sendXtMessage(extensionName, "scores", obj, "xml");
Using version 1.5.0
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

do you get any errors from the server, after you have sent the extension request?
Lapo
--
gotoAndPlay()
...addicted to flash games
jaeta
Posts: 6
Joined: 16 Jan 2007, 00:20

Post by jaeta »

Nope... this is what I get back from the server:

[multitest.as]: Bye bye!
[multitest.as]: Hello from the Simple Extension Example!
[multitest.as]: Event received: userJoin
[multitest.as]: RedScore=10
[multitest.as]: BlueScore=10

This is the extension script:

Code: Select all

function handleRequest(cmd, params, user, fromRoom) {
	if (cmd == "scores") {
		var room = _server.getCurrentRoom();
		BlueScore = Number(params.values[0]);
		RedScore = Number(params.values[1]);
		trace("RedScore="+RedScore);
		trace("BlueScore="+BlueScore);
		vObj = new Array();
		vObj.push({name:"BlueScore", val:BlueScore, priv:false});
		vObj.push({name:"RedScore", val:RedScore, priv:false});
		_server.setRoomVariables(room, user, vObj);
		var response = {};
		response._cmd = "scores";
		response.values = [];
		for (var i = 0; i<params.values.length; i++) {
			var n = params.values[i];
			response.values[i] = n;
		}
		_server.sendResponse(response, -1, null, [user]);
	}
}
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

I wasn't able to reproduce the problem. We tested a similar scenario and it worked.

Advice: make sure that this line of code

Code: Select all

var room = _server.getCurrentRoom();
is not returning null

If your extension is at Zone Level you should get a null, which in turn will make the setRoomVariables fail
Lapo
--
gotoAndPlay()
...addicted to flash games
jaeta
Posts: 6
Joined: 16 Jan 2007, 00:20

Post by jaeta »

You were correct. It was returning a null. Upon moving the extension to the room level it started working.

Thanks!
melojoy
Posts: 35
Joined: 02 Oct 2006, 21:27
Location: Italy

Post by melojoy »

Hi,
I have to set room variables from the extension...

Code: Select all

// impostiamo le room variables
var room = _server.getCurrentRoom();
trace("La stanza è: "+room);
trace("Utente: "+user.name);
trace("Posizione: "+params.posizione);

var rVars = new Array();
rVars.push( {name:"g"+params.posizione, val:user.name, persistent:false} );
//_server.setRoomVariables(room, null, rVars);
_server.setRoomVariables(room, user, rVars);

trace("Variabile inserita: "+rVars[0].g0) //
This it doesn't work (also with null user) and the result is:

Code: Select all

[3-Sette4.as]: La stanza è: it.gotoandplay.smartfoxserver.data.Room@aa82b
[3-Sette4.as]: Utente: al
[3-Sette4.as]: Posizione: 0
[3-Sette4.as]: Variabile inserita: undefined
This code is in a room extension dynamically creates when I create a new game room

In the last trace if I put...

Code: Select all

trace("Variabile inserita: "+rVars[0]);
or

Code: Select all

trace("Variabile inserita: "+rVars);
... the result is

Code: Select all

[3-Sette4.as]: Variabile inserita: [object Object]
thanks 8)
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

it's a bit confusing!
params.posizione = undefined

Code: Select all

[3-Sette4.as]: Variabile inserita: undefined
but you use that variable to set a Room Var and you expect "g0"... but it will be "gundefined" :)

makes sense?

where is params.posizione coming from?
Lapo
--
gotoAndPlay()
...addicted to flash games
melojoy
Posts: 35
Joined: 02 Oct 2006, 21:27
Location: Italy

Post by melojoy »

Escuse me... this is mistaken

Code: Select all

trace("Variabile inserita: "+rVars[0].g0)
The correct trace is...

Code: Select all

trace("Variabile inserita: "+rVars[0].name)
trace("Variabile inserita: "+rVars[0].val)
This return the name and the value of the variable... but with the admin tool I don't see var in the room.

:(

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

Post by Lapo »

Which version of the server do you use?
There was a bug with variable creation (when creating a room) in the first release of the 1.5.0 which was fixed with a later patch.
Lapo
--
gotoAndPlay()
...addicted to flash games
darnpunk
Posts: 229
Joined: 22 Jun 2007, 02:58
Location: SG

Post by darnpunk »

Hi Lapo,

I am not able to setRoomVariables from a different room. This is how I do it:

var rVars:Array = []
rVars.push( {name:"msg", val:"Sticky Test", persistent:false} )
smartfox.setRoomVariables(rVars, 4); // 4 is the roomId i am trying to set the variables to and i am in room id 3

I can only setRoomVariables successfully if I am in the same room. I am using SFS 1.5.5. Am I doing anything wrong?

Regards,
darnpunk
Post Reply