Accessing properties of parent in subclass

Please post any questions about developing your plugin here. Please use the search function before posting!
arawra
Senior Member
Posts: 190
Joined: Fri Jun 21, 2013 6:51 am

Accessing properties of parent in subclass

Postby arawra » Wed May 16, 2018 7:04 am

SP keeps throwing errors whenever I try and access player.steamid in an object thats a subclass of players.entities.Player

Code: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
  File "..\addons\source-python\packages\source-python\events\listener.py", line 92, in fire_game_event
    callback(game_event)
  File "..\addons\source-python\plugins\rpgo\rpgo.py", line 123, in player_spawn
  File "..\addons\source-python\plugins\rpgo\rpgo.py", line 67, in __init__
    class rpgPlayer(Player):
  File "..\addons\source-python\packages\source-python\players\_base.py", line 141, in steamid
    return self.playerinfo.steamid
  File "..\addons\source-python\packages\source-python\players\_base.py", line 122, in playerinfo
    if self._playerinfo is None:
  File "..\addons\source-python\packages\source-python\entities\_base.py", line 107, in __getattr__
    for server_class in self.server_classes:
  File "..\addons\source-python\packages\source-python\entities\_base.py", line 244, in server_classes
    yield from server_classes.get_entity_server_classes(self)
  File "..\addons\source-python\packages\source-python\entities\classes.py", line 137, in get_entity_server_classes
    if entity.classname in self._entity_server_classes:

Boost.Python.ArgumentError: Python argument types in
    None.None(rpgPlayer)
did not match C++ signature:
    None(class IServerUnknown *)
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Accessing properties of parent in subclass

Postby satoon101 » Wed May 16, 2018 8:36 pm

It works fine for me in GunGame. We'd have to see some of your code to see why this might not be happening in your case.
Image
arawra
Senior Member
Posts: 190
Joined: Fri Jun 21, 2013 6:51 am

Re: Accessing properties of parent in subclass

Postby arawra » Wed May 16, 2018 8:43 pm

Syntax: Select all

from players.entity import Player

class rpgPlayer(Player):

def __init__(self, index):
'''
newSteamid = self.steamid
if self.is_bot():
newSteamid += self.name
self.data = playerList[newSteamid]
'''

def setClass(rpgClass):
if rpgClass in CLASSES:
self.data.rpgClass = rpgClass

@Event('player_spawn')
def player_spawn(args):
player = rpgPlayer(index_from_userid(args['userid']))
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Accessing properties of parent in subclass

Postby satoon101 » Wed May 16, 2018 9:00 pm

You'll want to call the super() init method, otherwise certain things won't happen on initialization of the instance:

Syntax: Select all

class rpgPlayer(Player):

def __init__(self, index):
super().__init__(index)
# Other stuff


*Edit: also, you're missing the "self" argument in your other method.
*Edit2: also, instead of using index_from_userid yourself, you can always use the Player.from_userid classmethod:

Syntax: Select all

@Event('player_spawn')
def player_spawn(args):
player = rpgPlayer.from_userid(args['userid'])
Image

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 34 guests