Class attributes as Dictionary keys and values

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

Class attributes as Dictionary keys and values

Postby arawra » Tue May 26, 2015 5:26 am

Code: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
  File '..\addons\source-python\packages\source-python\events\listener.py', line 93, in fire_game_event
    callback(game_event)
  File '..\addons\source-python\plugins\dnd\dnd.py', line 218, in player_spawn
    spawnee.reset()
  File '..\addons\source-python\plugins\dnd\dnd.py', line 654, in reset
    self.maxhp = 100
  File '..\addons\source-python\packages\source-python\entities\entity.py', line 124, in __setattr__
    for server_class in self.server_classes:
  File '..\addons\source-python\packages\source-python\entities\entity.py', line 225, in server_classes
    for server_class in server_classes.get_entity_server_classes(self):
  File '..\addons\source-python\packages\source-python\entities\classes.py', line 143, in get_entity_server_classes
    datamap = entity.datamap

Boost.Python.ArgumentError: Python argument types in
    None.None(dndPlayer)
did not match C++ signature:
    None(class CBaseEntityWrapper {lvalue})



I have a list of subclassed PlayerEntity objects (dndPlayer). For the object, dndPlayer, I wish to set and change the maximum value of health they can have (as well as other things). At first, I thought I was getting this traceback because I may not have been declaring the attribute 'maxhp' in PlayerData with a default value in the form 'self.attribute' but it doesn't seem to be this way. Any light shed on the nature of this would help me out.

Syntax: Select all

def player_spawn(game_event):
global timers
spawnee_userid = game_event.get_int('userid')
spawnee = dndPlayerDictionary[spawnee_userid]
tell(spawnee.playerinfo, 'You are playing a %s %s level %s'%(spawnee.getRace(), spawnee.getClass(), spawnee.getLevel()))
spawnee.reset()


class _dndPlayerDictionary(dict):
def __missing__(self, userid):
value = self[userid] = dndPlayer(userid)
msg(value)
return value

def __delitem__(self, userid):
if userid in self:
super(_dndPlayerDictionary, self).__delitem__(userid)

dndPlayerDictionary = _dndPlayerDictionary()

class _PlayerDataDictionary(dict):
def __missing__(self, steamid):
value = self[steamid] = PlayerData()
name = ''
for players in PlayerIter():
if PlayerEntity(players).steamid == steamid:
tell(playerinfo_from_index(players), 'Your class and race have been automatically reset to Human Fighter')
name = PlayerEntity(players).name
msg('%s is new! Give them some help!'%name)
return value

class PlayerData(dict):
def __init__(self):
self.kills = 0
self.spree = 0
self.gold = 0
self.dndclass = 'Fighter'
self.race = 'Human'
self.la = 0
self.prestige = 0
self.points = 0
self.ecl = 1
self.Fighter = {
'level' :1,
'highest level':1,
'xp' :0
}

self.maxhp = 100
self.maxspeed = 100
self.stealth = 255
self.knifevisible = False
self.regen = []
self.damageflat = 0
self.damagepercent = 0
self.weaponlist = []

def __getattr__(self, attr):
# Redirect to __getitem__
# Can also use:
# return self[attr]
try:
return self[attr]
except KeyError:
raise AttributeError('Attribute "{0}" not found'.format(attr))

def __setattr__(self, attr, value):
# Redirect to __setitem__
# Can also use:
# self[attr] = value
self.__setitem__(attr, value)
msg("Set %s to %s"%(attr, value))

class dndPlayer(PlayerEntity):

def __new__(cls, userid):

# Since PlayerEntity requires an index for instantiation, we need to get the index
index = index_from_userid(userid)
self = super(dndPlayer, cls).__new__(cls, index)
return self

def __init__(self, userid):
PlayerDataDictionary[self.steamid]['last connected'] = int(time.clock())
PlayerDataDictionary[self.steamid]['name'] = self.name

def reset(self):

self.maxhp = 100
self.maxspeed = 100
self.stealth = 255
self.knifevisible = False
self.regen = []
self.damageflat = 0
self.damagepercent = 0
self.weaponlist = []
User avatar
Ayuto
Project Leader
Posts: 2209
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Tue May 26, 2015 5:40 am

That's not the full traceback. However, you have to call super().__init__().
arawra
Senior Member
Posts: 190
Joined: Fri Jun 21, 2013 6:51 am

Postby arawra » Tue May 26, 2015 5:53 am

E: Updated with full traceback
arawra
Senior Member
Posts: 190
Joined: Fri Jun 21, 2013 6:51 am

Postby arawra » Tue May 26, 2015 7:00 am

This error is really confusing me.

Syntax: Select all

#This is the class that is called
def __init__(self, userid):
super().__init__(index_from_userid(userid))
PlayerDataDictionary[self.steamid]['last connected'] = int(time.clock())
PlayerDataDictionary[self.steamid]['name'] = self.name
self.maxhp = 100
PlayerDataDictionary[self.steamid]['maxhp'] = 100
PlayerDataDictionary[self.steamid].maxhp = 100

def reset(self):
self.maxhp = 100

@Event
def player_spawn(game_event):
global timers
spawnee_userid = game_event.get_int('userid')
spawnee = dndPlayerDictionary[spawnee_userid]
tell(spawnee.playerinfo, 'You are playing a %s %s level %s'%(spawnee.getRace(), spawnee.getClass(), spawnee.getLevel()))
spawnee.reset()


Code: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
  File '..\addons\source-python\packages\source-python\events\listener.py', line 93, in fire_game_event
    callback(game_event)
  File '..\addons\source-python\plugins\dnd\dnd.py', line 218, in player_spawn
    spawnee.reset()
  File '..\addons\source-python\plugins\dnd\dnd.py', line 657, in reset
    self.maxhp = 100
  File '..\addons\source-python\packages\source-python\entities\entity.py', line 115, in __setattr__
    getattr(self.__class__, attr), property)):

AttributeError: type object 'dndPlayer' has no attribute 'maxhp'
arawra
Senior Member
Posts: 190
Joined: Fri Jun 21, 2013 6:51 am

Postby arawra » Tue May 26, 2015 5:20 pm

Just needed to sort out my API better.

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 87 guests