Swift example set and retrieve custom user var

Post here your questions about the Objective-C API for SFS2X

Moderators: Lapo, Bax

intomo
Posts: 51
Joined: 22 Nov 2010, 21:21
Location: Los Angeles

Swift example set and retrieve custom user var

Postby intomo » 22 Nov 2021, 16:28

Can I get a Swift example of set and retrieve custom user variable?
Each client will set their own.
1 client, will pull all of these from the user list.

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

Re: Swift example set and retrieve custom user var

Postby Lapo » 23 Nov 2021, 11:39

Hi,
you can check the Swift2XConnector example in our iOS Example Pack here:
https://www.smartfoxserver.com/download ... p=examples

This will give you a generic idea on how to setup your code, instantiate the SmartFoxClient class and handle the events. It also shows how to create Room Variables, once logged in and joined into a Room.

As regards setting user variables you can use this, which is very similar to the example code just mentioned:

Code: Select all

let var1 = SFSUserVariable(name: "MyName", value: "Kermit the Frog", type: VariableType_STRING);
let var2 = SFSUserVariable(name: "MyColor", value: "Green", type: VariableType_STRING);
let var3 = SFSUserVariable(name: "MyNumber", value: 1, type: VariableType_INT);
       
sfs!.send(SetUserVariablesRequest(userVariables: [var1!, var2!, var3!]));


And handle the relative update event with:

Code: Select all

func onUserVariablesUpdate(_ evt: SFSEvent!)
{
    let user = evt.params["user"] as! SFSUser
    let changedVariables = evt.params["changedVars"] as! NSArray
   
    print("Variables updated:")
    for key in (changedVariables as! [String])
    {
        let value = user.getVariable(key)?.getValue() as Any?
        print("\tkey:", key, "value:", value!)
    }
}


Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
intomo
Posts: 51
Joined: 22 Nov 2010, 21:21
Location: Los Angeles

Re: Swift example set and retrieve custom user var

Postby intomo » 25 Nov 2021, 23:10

Can I read these from the room.userlist? Maybe I'm on the wrong track. What I'm looking to do is to setUserVariable "done" to true or false. Then be able to pull this from the user list so I can see in real time which client is done with a task.

onUserVariablesUpdate is across the entire room, right? Or is it only broadcast within the client? Thanks.
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Swift example set and retrieve custom user var

Postby Lapo » 26 Nov 2021, 15:02

The onUserVariablesUpdate event triggers exactly when there is an update, so that's the preferred way of doing it.

Reading from the user list is also possible but it is less efficient as you don't know when a change has occurred so, I guess, what you would need to do is keep reading from the list continuously, with some sort of polling system.

The use case is not very clear, but the polling system seems less efficient.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X iPhone / iPad / OSX API”

Who is online

Users browsing this forum: No registered users and 7 guests