Sunday, January 4, 2009

shallow copies in python

I had a fairly interesting bug today.

When going to cast a second spell the grid targeting was hitting every previous unit and position.

I tracked it down to the fact that I made a copy of the executing object.

Now I made a copy to it so that it wouldn't interfere with the objects on the unit. So why did that cause more problems?

Because in my code for the object I would say add 3 units to a list. Because its a shallow copy it would also add it to the previous object. And you cant use deep copyies as that would duplicate the units.

However when I went to clear the list to reset it I just created a new one like self.list = []

So what it did was clear the copied variable and not the original. Thus making the copy actually made things worse as it made the copy interfere in only some ways. So I just removed the copy method and things started working again.

This is an interesting bug because I could have clearly spent less time on it and still fixed it by clearing the list. But I spent the extra time to actually remove the copy method because I knew that would cause me trouble in the future if I didn't

It should be noted though that the actions must be updated to deal with repeated use for example on my move object. This is however much more simple and better than trying to debug the copying stuff. Either could be done, but the latter is like doing security in php

No comments:

Post a Comment