I was storing them by id() because it was an easy unique number to use and
meant that I wouldn't need to dynamically generate unique numbers for each
instance. The main thing I need them for is referencing individual bullets
for collision detection. I also need to delete the dictionary reference,
because otherwise, collision detection with no longer "existing" bullets
continues, causing a sort of invisible barrier for bullets in the future.

So, recycling bullets? That might be a good idea for the ships (which can
only shoot one bullet at a time), so I think I might try to implement that.
I could also recycle bullets where possible in the Target class, and
otherwise create a new bullet. Finally, I suppose I can simply increment a
number to create my own unique IDs for each instance, then pass them on to
the instances. I'll try all that, and if it doesn't work, I'll reply again.
Thanks for everyone's input! :)

Just a couple more thing I feel the need to clear up, because I hate leaving
messes. :)

> You are calling *two* times destroy (in bullet update) ?
> [line 412]
>   self.shooter.game.bullets[i].destroy() # one
>   self.destroy() # two
>

Actually, the first one destroys the other bullet (the one self is colliding
with). :)

Why are you storing stuff by id() in a dictionary in the first place? When
> would you want to look something up by it's id() when you wouldn't want just
> to have a reference (possible a weakref) to the object itself?
>
> (I ask because it seems like doing what you are doing would be a big ol'
> error prone hassle and waste of your time compared to other more commonly
> used techniques in python, but I can't figure out exactly what problem that
> dictionary approach was originally conceived of as a solution for)
>

Simple: I needed instances to be able to reference themselves, and I didn't
feel like generating my own unique IDs and passing them on as a parameter.
:)

On Mon, Jul 12, 2010 at 5:34 PM, Kris Schnee <[email protected]> wrote:

> On Mon, Jul 12, 2010 at 9:25 AM, 音本四 <[email protected]
>> <mailto:[email protected]>> wrote:
>>
>>    In my game that I'm working on, Senso (which is my first game in
>>    Pygame), there is a main game class, Game, which handles things on
>>    the global level. One thing it does is store each instance of game
>>    objects in dictionaries, with the dictionary keys being each
>>    instance's memory address, grabbed with id(). When an instance needs
>>    to be removed, part of the process involves removing the dictionary
>>    reference of the instance, by calling "del
>>    game.myclassdict[id(self)]" (replacing "myclassdict" with the actual
>>    name of the correct dictionary). This seems to cause a problem:
>>    sometimes, this action seems to reference a nonexistant key in the
>>    respective dictionary. I've only experienced it with bullets. I've
>>    checked and double-checked the code, and it doesn't seem that at any
>>    point I forget to add an instance to the dictionary.
>>
>
> Like Brian Fisher wrote, you probably shouldn't be storing references this
> way, since there doesn't seem to be a good reason for it. (We got into a fun
> discussion once about "bullet theology". Should bullet objects be destroyed
> or recycled when their time runs out?)
>
> My guess is that you're experiencing a problem with Pygame's "garbage
> collection" system, which destroys objects once there are no more references
> to them. Maybe Python is destroying your object's ID reference before the
> deletion command is fully carried out, causing the id() command to fail?
>

Reply via email to