Correct method to update UserVariable

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
LordDawn
Posts: 42
Joined: 15 Apr 2019, 08:36

Correct method to update UserVariable

Postby LordDawn » 16 Jun 2019, 19:24

I have a block of around 16 UserVariables for my characters, mainly serialized data in the form of strings. One of which I want to update regularly (health,stamina). What is the correct most performant method for updating a single variable from the server, whilst leaving the others alone?

Many thanks.
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Correct method to update UserVariable

Postby Lapo » 17 Jun 2019, 08:09

Hi,
just update that variable. The USER_VARIABLE_UPDATE will only send what is changed.
The method to use is the one from the SFSApi class, called setUserVariables()

Extension code example:

Code: Select all

getApi().setUserVariables(...);


Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
LordDawn
Posts: 42
Joined: 15 Apr 2019, 08:36

Re: Correct method to update UserVariable

Postby LordDawn » 17 Jun 2019, 08:29

So I can't update a single user-variable on the server, I use setUserVariables() which seems to send updates for all 16 user-variables, even though only one has changed.

Code: Select all

private List<UserVariable> m_vars = new ArrayList<UserVariable>();

m_vars.add(new SFSUserVariable("wvid", 10, false, false));
m_vars.add(new SFSUserVariable("transform", transform.toString(), false, false));
m_vars.add(new SFSUserVariable("data", m_data, false, false));
m_vars.add(new SFSUserVariable("option", 17, false, false));



If I change the value of a single var with

Code: Select all

 m_vars.set(0, new SFSUserVariable("wvid", 10, false, false));
 xxx.setUserVariables(getUser(), m_vars);
 


It seems to send a USER_VARIABLE_UPDATE with all the user-variables listed, not just the one that's changed (wvid). I guess I'm doing something wrong somewhere.
User avatar
LordDawn
Posts: 42
Joined: 15 Apr 2019, 08:36

Re: Correct method to update UserVariable

Postby LordDawn » 17 Jun 2019, 09:01

Ah, sorted it. You need to set a list of UserVariables but it still remembers existing ones. That's not quite how I imagined it working, but it's cool, I have a handle on it now :)
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Correct method to update UserVariable

Postby Lapo » 17 Jun 2019, 10:44

Yes, those that are already set are not "forgotten". :)
In the list of variables you want to set you typically pass only what needs to be changed. The server then checks which have really changed and sends only those.

So in case you set a variable to its current value it won't be transmitted.
Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 91 guests