SFSObject as UserVariable

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

Post Reply
oalasergiu
Posts: 19
Joined: 02 Feb 2014, 21:47

SFSObject as UserVariable

Post by oalasergiu »

Hi,

I've been trying to set a SFSObject as an UserVariable on the server and play with it.

So, on the server side I have something like:

Code: Select all

ISFSObject score = new SFSObject();
score.putInt("wins", 20);
score.putLong("points", 800);

List<UserVariable> uvList = new ArrayList<>();

uvList.add(new SFSUserVariable("score", score));

someUser.setVariables(uvList);
Client:

Code: Select all

  Debug.Log("Wins: " + thatUser.GetVariable("score").GetSFSObjectValue().GetInt("wins");
And I'm getting null on client. I've debugged the script, and instead of my SFSObject there, I have a score variable which is the last set value in SFSObject. In the above case it is score = 800;

Now I wonder, is it possible to set SFSObjects as UserVariables and use them on Client Side?

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

Re: SFSObject as UserVariable

Post by Lapo »

Yes, what you are doing is fine.

What server version are you using? What version of the C# API?
Do you get any server or client side errors?

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
oalasergiu
Posts: 19
Joined: 02 Feb 2014, 21:47

Re: SFSObject as UserVariable

Post by oalasergiu »

Server Version: Version 2.7.0.
C# Client API: 1.4.0.0

I didn't notice any errors on client/server.
However, I'll check again when I'm home. Though, I've debugged the extension, and it seems like the SFSObject variable is set to the user.
oalasergiu
Posts: 19
Joined: 02 Feb 2014, 21:47

Re: SFSObject as UserVariable

Post by oalasergiu »

No errors, I've checked.
Also, I've updated the client API to 1.5.3 and it didn't solve the problem.
User avatar
Bax
Site Admin
Posts: 4626
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: SFSObject as UserVariable

Post by Bax »

I'm not able to reproduce the issue you mentioned.
This is the code I'm using on the server side:

Code: Select all

		// Set user variable containing a SFSObject
		ISFSObject score = new SFSObject();
		score.putInt("wins", 20);
		score.putLong("points", 800);

		List<UserVariable> uvList = new ArrayList<UserVariable>();

		uvList.add(new SFSUserVariable("score", score));
		
		getApi().setUserVariables(sender, uvList);
And this is the code I'm using on the client side

Code: Select all

		private void OnUserVarsUpdate(BaseEvent evt)
		{
			ArrayList changed = (ArrayList) evt.Params["changedVars"];

			if (changed.Contains ("score")) {
				User user = (User)evt.Params ["user"];
				UserVariable scoreVar = user.GetVariable ("score");
				ISFSObject obj = scoreVar.GetSFSObjectValue ();

				Trace (obj.GetInt ("wins").ToString ());
				Trace (obj.GetLong ("points").ToString ());
			}
		}
Please note that you can't set the User Variable directly on the User object, but you have to use the API's setUserVariables method.
Paolo Bax
The SmartFoxServer Team
oalasergiu
Posts: 19
Joined: 02 Feb 2014, 21:47

Re: SFSObject as UserVariable

Post by oalasergiu »

Hi Bax,

I've tried using "getApi().." as you suggested. It didn't work.

Some more information, I'm setting the variables when user joins a zone. Can this be the problem?
Also, on client, I'm not doing this inside a OnUserVarsUpdate method.

To clarify the flow:
1. User login.
2. Join zone.
3. The server sets the varibales.
4. User gets into a game scene.
5. In the game scene, I'm trying to access the score variable.

And I can get other primitive variables like int or string, but not the SFSObject. :(
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SFSObject as UserVariable

Post by Lapo »

I would suggest to check if the variables are really set as expected via the AdminTool > ZoneMonitor.
From there navigate the Zone, Room and User and check his variables.
Lapo
--
gotoAndPlay()
...addicted to flash games
oalasergiu
Posts: 19
Joined: 02 Feb 2014, 21:47

Re: SFSObject as UserVariable

Post by oalasergiu »

Hmm... Via the admin tool as you suggested, I see the score variable of type INT, which must have been of type SFSObject.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SFSObject as UserVariable

Post by Lapo »

Then something is not right with your code setting those variables. Or you have multiple places in the code (either client or server) that overwrite the variable.
I'd suggest to investigate that aspect. :)

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
oalasergiu
Posts: 19
Joined: 02 Feb 2014, 21:47

Re: SFSObject as UserVariable

Post by oalasergiu »

Damn it! I found the problem...
The score variable have been overridden in some other place :D

Sorry for taking your time.

However I find the getApi().setUserVariables() useful.

Thanks.
Post Reply