TStand90 / roguelike_tutorial_revised

The python libtcod roguelike tutorial, with good coding practices kept in mind from the beginning.
97 stars 28 forks source link

Part 8 - item.use - Extend called causing crash #17

Open realityworks opened 4 years ago

realityworks commented 4 years ago

Hey guys,

I kept getting a crash as the results of used items were compacted into a list string from using extend instead of in append for the results of an item_use_results. Changing the results.extend to results.append fixed the issue


+   def use(self, item_entity, **kwargs):
+       results = []
+
+       item_component = item_entity.item
+
+       if item_component.use_function is None:
+           results.append({'message': Message('The {0} cannot be used'.format(item_entity.name), libtcod.yellow)})
+       else:
+           kwargs = {**item_component.function_kwargs, **kwargs}
+           item_use_results = item_component.use_function(self.owner, **kwargs)
+
+           for item_use_result in item_use_results:
+               if item_use_result.get('consumed'):
+                   self.remove_item(item_entity)
+
>>>>           results.extend(item_use_results) <- Should be RESULTS.APPEND
+
+       return results
+
+   def remove_item(self, item):
+       self.items.remove(item)
lucasdpau commented 4 years ago

results.extend works for me. I notice that the line in your code is inside of the for loop, did you try taking it out of the loop?