I am using the pysdl2 library. self.velocity will print, but self.forward_tick with throw a key error.
What is causing only part of the self attributes from being assigned. I am thinking it has something to do with the inheritance.
class Robot(sdl2.ext.Entity):
def __init__(self, world, sprite, posx=0, posy=0):
self.sprite = sprite
self.sprite.position = posx, posy
self.velocity = Velocity()
self.forward_tick = 0
self.go_forward = 0
self.unit_forward = (1,0)
print(self.velocity)
print(self.forward_tick)
Here is the output:
Collins-MacBook-Air:soccer_bots collinbell$ python test_simulation_world.py
<simulation_world.Velocity object at 0x108ecb5c0>
Traceback (most recent call last):
File "/Users/collinbell/.pyenv/versions/3.4.3/lib/python3.4/site-packages/sdl2/ext/ebs.py", line 53, in __getattr__
ctype = self._world._componenttypes[name]
KeyError: 'forward_tick'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test_simulation_world.py", line 3, in <module>
world = Simulation_World("Test Simulation", 1080, 720)
File "/Users/collinbell/Programs/soccer_bots/simulation_world.py", line 100, in __init__
self.player1 = Robot(self.world, sp_paddle1, 0, 250)
File "/Users/collinbell/Programs/soccer_bots/simulation_world.py", line 22, in __init__
print(self.forward_tick)
File "/Users/collinbell/.pyenv/versions/3.4.3/lib/python3.4/site-packages/sdl2/ext/ebs.py", line 56, in __getattr__
(self.__class__.__name__, name))
AttributeError: object ''Robot'' has no attribute ''forward_tick''
From the docs on component-based design with sdl2.ext
, the Entity
type is special, and doesn't follow normal Python idioms. In particular, you can't just create arbitrary attributes; you can only create attributes whose value is a component type, and whose name is a lowercased version of that type:
Entity
objects define the in-application objects and only consist of component-based attributes.…
The
Entity
also requries its attributes to be named exactly as their component class name, but in lowercase letters.
So, when you try to add an attribute named forward_tick
, that causes Entity.__setattr__
to go looking for a class named Forward_Tick
in the world's component types. Which it apparently does by looking up self._world._componentypes[name]
, which is the line that's actually raising the exception, as you can see in the tracebacks.
Without knowing anything more about your code and your design than the tiny fragment you showed us, I can't tell you how to fix this. But most likely, it's one of the following:
Component
, not an Entity
,forward_tick
in a Component
type that this Entity
can contain, orComponent
-oriented design in the first place.At any rate, as the docs say:
If you are just starting with such a [component-oriented] design, it is recommended to read through the The Pong Game tutorial.