andye456 / RandomRooms

0 stars 0 forks source link

BUG: If a potion is depleted then another with same name picked up the uses is initialised to 0 #13

Closed andye456 closed 4 years ago

andye456 commented 4 years ago

Scenario: Healing1 is used until it is depleted (0 uses left). At which point it is removed from the inventory. If a new healing1 is picked up then it will have it's uses initialised to 0, but it can be used an infinite number of times. It should have it's uses initialised to 5. The "uses" is part of the healing1 object and has the value set to 0 when it is in the room and available to pick up.

andye456 commented 4 years ago

This was due to the fact the Python dictionaries are used by-reference so if you create a python dictionary and assign it to more than one variable then change it in one instance it will change it for all instances. I fixed this by assigning a copy() of the variable to the item store.

e.g. a = {"foo":5, "bar":10} b,c = [],[] b.append(a) c.append(a)

b[0]['foo']=200 print(c[0]['foo']) 200

What you need to do is: b.append(a.copy()) c.append(a.copy()) Then they will be "passed by value" and altering b won't affect c