[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Subtle Memory Bug



Kris Schnee wrote:

Bob Ippolito wrote:

It's probably not the uniqueness of ID numbers that's breaking his code, it's the fact that he has two different generations in the same data structure. That's simply not how you should do what he's trying to do.


So, maybe have a tribes_this_turn dictionary and a next_turn_tribes dictionary, with tribes getting copied into the next only if they haven't been flagged as "gone?" It'd be tricky because what if tribes A and B merge one turn, and in the same turn C tries to interact with them? I guess the tribe that A/B become wouldn't exist till next turn, but then there's the possibility that C will attack and destroy A on the same turn that A has been absorbed into B.

The way this is typically handled is to separate actions from effects. For example, simple physical modelling systems will update the velocity and position of all objects before considering collisions and other forces that affect acceleration.


In your case, say each tribe has some hit points. Your run loop could look like:

for each tribe:
 attack other tribes, decreasing their hit points
 flag intent to merge with another tribe
remove any tribe with hit points < 0
for each pair of tribe intending to merge:
 merge the tribes

As with a physical system, if your constraints / actions are more complex than this, you may want to solve a linear system or integrate a non-linear system to determine the simultaneous effect of all actions.

[This comment is orthagonal to the idea of using separate vectors for current and next generations, which I am also in favour of.]

Alex.