[SOLVED] WHY SMARTFOX IS ADDING EXTRA DECIMALS TO SYNCHED VARIABLES?!

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

User avatar
TryMyGames
Posts: 59
Joined: 19 Dec 2016, 22:20
Location: Moldova, Chisinau
Contact:

[SOLVED] WHY SMARTFOX IS ADDING EXTRA DECIMALS TO SYNCHED VARIABLES?!

Postby TryMyGames » 30 May 2017, 11:03

Code: Select all

      List<UserVariable> userVariables = new ArrayList<>();
      System.out.println(Math.round(100f * health) / 100f);
      userVariables.add(new SFSUserVariable(UserVariableKeys.HEALTH, Math.round(100f * health) / 100f));
      sfsApi.setUserVariables(user, userVariables);


For example I want to add a user variable which is rounded to 2 decimals, but on admin tool show that number with maximum number of decimals added. This causes an extra traffic, why this is happening?
Last edited by TryMyGames on 31 May 2017, 13:34, edited 1 time in total.
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: WHY SMARTFOX IS ADDING EXTRA DECIMALS TO SYNCHED VARIABLES?!

Postby Lapo » 30 May 2017, 15:31

Hi,
Room and User variables use double for any floating point value so the extra decimals you see are just the added precision, but the value is actually the same.

Additionally you're not causing extra traffic, it's always the same amount of bytes.

If you need your data to be of type float and not double you should simply convert back the value you get from the UserVariable. Like this:

Code: Select all

float myValue = userVariable.getDoubleValue("name").floatValue()

As regards rounding, you can do it for output but internally either a float or a double cannot be limited in the number of decimals they support.

Hope it's clear.
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
TryMyGames
Posts: 59
Joined: 19 Dec 2016, 22:20
Location: Moldova, Chisinau
Contact:

Re: WHY SMARTFOX IS ADDING EXTRA DECIMALS TO SYNCHED VARIABLES?!

Postby TryMyGames » 30 May 2017, 15:51

So if I need to optimize the traffic i have to use smaller type, like byte?
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: WHY SMARTFOX IS ADDING EXTRA DECIMALS TO SYNCHED VARIABLES?!

Postby Lapo » 30 May 2017, 16:03

Why do you need to "optimize traffic"? This is already done by the protocol for you and you should not worry about it unless there's a specific reason. Keep in mind that data gets compressed if packets start getting fat :)

As regards using "byte"... that's pretty limited in terms of numbers you can use: 0-255, no decimals.
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
TryMyGames
Posts: 59
Joined: 19 Dec 2016, 22:20
Location: Moldova, Chisinau
Contact:

Re: WHY SMARTFOX IS ADDING EXTRA DECIMALS TO SYNCHED VARIABLES?!

Postby TryMyGames » 31 May 2017, 08:10

I am developing an io game for mobile devices using Unity, and there can be such situations when in area of interest of some clients can be 8-10 players, each player updates it's position 8 times per second, on some devices the game starts to lag, the traffic is ~40KBs, I found that rounding the position to 2 decimals will be enough, but the problem with added decimals appears, so if I create an user variable of type int with value equals to 1, or value = 2147483647 it will take the same amount of traffic? If that's right then I should use smaller types everywhere it is possible, because in this type of games network optimization is very important :)
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: WHY SMARTFOX IS ADDING EXTRA DECIMALS TO SYNCHED VARIABLES?!

Postby Lapo » 31 May 2017, 08:15

Yes an INT uses 4 bytes.
Shorts use 2 bytes, and Byte (of course) uses 1 byte.
More on this here:
http://docs2x.smartfoxserver.com/Develo ... t-sfsarray

If you need to optimize numeric values I'd suggest to do it via a byte array so you can customize the format of any data type.

Then you wrap the byte array in an SFSObject and send that.

cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
TryMyGames
Posts: 59
Joined: 19 Dec 2016, 22:20
Location: Moldova, Chisinau
Contact:

Re: WHY SMARTFOX IS ADDING EXTRA DECIMALS TO SYNCHED VARIABLES?!

Postby TryMyGames » 31 May 2017, 08:47

Thanks ;) This will help a lot :)

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 60 guests