EasyPlayer creates new instance on every call?

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

EasyPlayer creates new instance on every call?

Postby Mahi » Mon Sep 03, 2012 6:59 pm

I can't download the newest build yet, so I can't test this out for couple of days.
However, I was just wondering, if I get EasyPlayer from userid, and then later on check if an other EasyPlayer instance from the same userid is equal to the first EasyPlayer, will it be? Or would I have to compare like userid's or steamid's?

Example of what I mean:

Syntax: Select all

from players import EasyPlayer

# Create player list for storing all the players (just an example)
players = []

@Event
def player_spawn(GameEvent):
userid = GameEvent.GetInt('userid')
player = EasyPlayer(userid)
# Add spawned player to players if he's not there already
if player not in players:
players.append(player)
So would the same player be added to the players list on each spawn? If that's the case, I would have to do like

Syntax: Select all

from players import EasyPlayer

# Create player list for storing all the players (just an example)
players = []

@Event
def player_spawn(GameEvent):
userid = GameEvent.GetInt('userid')
player = EasyPlayer(userid)
# Add spawned player to players if he's not there already
for p in players:
if player.steamid == p.steamid:
return
players.append(player)
And I'm not asking how to add players to player list, I'm asking about the functionality of EasyPlayer, does it create a new instance, even if I'd call it on same userid?
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Postby BackRaw » Mon Sep 03, 2012 7:42 pm

Weeeeeell.. I'd do it the same way as I did it during the good ol' ES days -- much thanks to Satoon again for that!!

Syntax: Select all

from events import Event
from players import EasyPlayer

players = {} # declare a dictionary, not a list!

@Event
def player_activate(GameEvent):
userid = GameEvent.GetInt("userid")

if not userid in players.keys():
players[userid] = EasyPlayer(userid) # make sure only one key - e.g. userid - equals one value - e.g. EasyPlayer instance

@Event
def player_disconnect(GameEvent):
userid = GameEvent.GetInt("userid")

if userid in players.keys():
del players[userid] # delete only if the key - e.g. userid - exists in the dictionary

# ------------- example usage then on player_say for example ------

@Event
def player_say(GameEvent):
userid = GameEvent.GetInt("userid")

if userid in players.keys():
players[userid].health += 10 # add 10 HP each time a player says something AND is added to the players dictionary

Note: It is not the fastest method! There may be some very cool, much faster variants out there which I don't know of, but I know it works, so you can use it as long as another variant appears to be faster/better, and at the same time as stable as this one.
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Postby Mahi » Mon Sep 03, 2012 7:53 pm

BackRaw wrote:Weeeeeell.. I'd do it the same way as I did it during the good ol' ES days -- much thanks to Satoon again for that!!

Syntax: Select all

from events import Event
from players import EasyPlayer

players = {} # declare a dictionary, not a list!

@Event
def player_activate(GameEvent):
userid = GameEvent.GetInt("userid")

if not userid in players.keys():
players[userid] = EasyPlayer(userid) # make sure only one key - e.g. userid - equals one value - e.g. EasyPlayer instance

@Event
def player_disconnect(GameEvent):
userid = GameEvent.GetInt("userid")

if userid in players.keys():
del players[userid] # delete only if the key - e.g. userid - exists in the dictionary

# ------------- example usage then on player_say for example ------

@Event
def player_say(GameEvent):
userid = GameEvent.GetInt("userid")

if userid in players.keys():
players[userid].health += 10 # add 10 HP each time a player says something AND is added to the players dictionary

Note: It is not the fastest method! There may be some very cool, much faster variants out there which I don't know of, but I know it works, so you can use it as long as another variant appears to be faster/better, and at the same time as stable as this one.
As I already said, and even added comment to my code "#Create player list for storing all the players (just an example)",
Mahi wrote:And I'm not asking how to add players to player list, I'm asking about the functionality of EasyPlayer, does it create a new instance, even if I'd call it on same userid?
But according to your code, I think it does create a new instance every time.
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Mon Sep 03, 2012 8:39 pm

As you can see here EasyPlayer is a simple object. So everytime you want to get an instance, a new instance will be created. Here is an example to make it clear:

Syntax: Select all

>>> class Player(object):
def __init__(self, userid):
self.userid = userid


>>> p1 = Player(1)
>>> p1
<__main__.Player object at 0x0000000001D4BA20>
>>> p2 = Player(1)
>>> p2
<__main__.Player object at 0x0000000002BC4B70>
>>> p1 == p2
False


This class is sometimes very useful.

Syntax: Select all

class _Players(dict):
def __missing__(self, userid):
self[userid] = instance = EasyPlayer(userid)
return instance

def __delitem__(self, userid):
if userid in self:
del self[userid]

Players = _Players()

It will always return you an EasyPlayer instance for the given user ID. Also you don't need to check if it contains a user ID, if you want to delete the player's instance. The class takes care about that. Here is an example:

Syntax: Select all

@Event
def player_spawn(game_event):
userid = game_event.GetInt('userid')
player = Players[userid]
print player.steamid

@Event
def player_disconnect(game_event):
userid = game_event.GetInt('userid')
del Players[userid]
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Postby Mahi » Mon Sep 03, 2012 8:49 pm

Ayuto wrote:As you can see here EasyPlayer is a simple object. So everytime you want to get an instance, a new instance will be created.
This would've been enough, problem is that I'm on a phone so I couldn't test it myself. But thanks :)
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Postby BackRaw » Mon Sep 03, 2012 9:16 pm

Ayuto wrote:As you can see here EasyPlayer is a simple object. So everytime you want to get an instance, a new instance will be created. Here is an example to make it clear:

Syntax: Select all

>>> class Player(object):
def __init__(self, userid):
self.userid = userid


>>> p1 = Player(1)
>>> p1
<__main__.Player object at 0x0000000001D4BA20>
>>> p2 = Player(1)
>>> p2
<__main__.Player object at 0x0000000002BC4B70>
>>> p1 == p2
False


This class is sometimes very useful.

Syntax: Select all

class _Players(dict):
def __missing__(self, userid):
self[userid] = instance = EasyPlayer(userid)
return instance

def __delitem__(self, userid):
if userid in self:
del self[userid]

Players = _Players()

It will always return you an EasyPlayer instance for the given user ID. Also you don't need to check if it contains a user ID, if you want to delete the player's instance. The class takes care about that. Here is an example:

Syntax: Select all

@Event
def player_spawn(game_event):
userid = game_event.GetInt('userid')
player = Players[userid]
print player.steamid

@Event
def player_disconnect(game_event):
userid = game_event.GetInt('userid')
del Players[userid]


Wow! That's a great helper class, very useful! Thanks for posting =)

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 37 guests