Is there any way to get a user object from their id

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

Moderators: Lapo, Bax

Devops
Posts: 23
Joined: 27 May 2020, 15:23

Is there any way to get a user object from their id

Postby Devops » 18 Jun 2020, 13:49

When the user logs in to smartfox I would have to store userId and user object in memory.
The reason for this is I will have to send a custom smartfox event to a particular userId at any point based on my game logic.

I believe, Storing the entire user object would be heavy.
I am looking for ways to not store this entire user object
So I was thinking is there a way to get this user object from their id?
Or is there any other way to send a custom event to a user without storing the user object?

NOTE: Client is not aware of this situation so client won't be calling any event.
This event which will be sent is independently sent to a user so there is no chance of client calling any event before I send this event
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Is there any way to get a user object from their id

Postby Lapo » 18 Jun 2020, 14:10

Hi,
I am not sure what you mean by "user object". SmartFoxServer already represent every connected user with a class called SFSUser (which implements a User interface), which represents the player and all its properties (id, variables, settings, session data etc...) and these objects are already available in memory as long as the user's session is active.

You can obtain a User object from its ID, user name or session object. Here're a few examples you can use in your Extension code:

Code: Select all

public void test()
{
   Zone zone = getParentZone();
   
   User user = zone.getUserById(userId);
   User user = zone.getUserByName(userName);
   User user = zone.getUserBySession(userSession);
}


Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
Devops
Posts: 23
Joined: 27 May 2020, 15:23

Re: Is there any way to get a user object from their id

Postby Devops » 18 Jun 2020, 14:36

By user object I meant SFSUser object.

Code: Select all

zone.getUserByName(userName)


Thanks for the code. These should work but just to confirm
Consider I am an user with username "abc".
So using the above line can I get the user object with username "user2" and send "user2" and custom smartfox event
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Is there any way to get a user object from their id

Postby Lapo » 18 Jun 2020, 15:19

Yes, that's correct.
All connected users can be found via one of those method calls.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Devops
Posts: 23
Joined: 27 May 2020, 15:23

Re: Is there any way to get a user object from their id

Postby Devops » 18 Jun 2020, 15:42

The names in smartfox login will be unique if custom Login is set to false.

But if we set customLogin to true, does this unique name check is still done by smartfox?

EDIT: It still does this check. How do I disable this? I would like to allow users with same name login
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Is there any way to get a user object from their id

Postby Lapo » 19 Jun 2020, 07:43

Devops wrote:The names in smartfox login will be unique if custom Login is set to false.

The names are always unique, regardless of the custom Login setting.

But if we set customLogin to true, does this unique name check is still done by smartfox?

Yes

EDIT: It still does this check. How do I disable this? I would like to allow users with same name login

In SFS2X user names must not have duplicates.
If the same user tries to login twice there can be two possible outcomes:

1) If the Zone's forceLogout options is set to true, the previous User session will be shut down and the User will be allowed to login and create a new Session.

2) If the Zone's forceLogout options is set to false, the new login attempt will be denied.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 100 guests