SelinaDev / Godot-Roguelike-Tutorial

Yet Another Roguelike Tutorial in Godot
MIT License
78 stars 10 forks source link

Bug: Unable to use items after loading saved game #13

Closed Jomii closed 7 months ago

Jomii commented 7 months ago

Thanks for the tutorial series 🙏 It's been fun following it along! In part 10 however I stumbled upon the following issue.

In part 10 there seems to be an issue with items not having their entities properly set up when loading from a save file.

The game crashes when I try to use a health potion or a scroll in a loaded game.

Steps to reproduce with a health potion:

  1. Start a new game in the part 10 project
  2. Pick up a health potion
  3. Take some damage
  4. Quit to main menu
  5. Select Continue last game
  6. Try to use a potion
  7. Game crashes with message Invalid call. Nonexistent function 'get_entity_name' in base 'Nil', on the script healing_consumable_component.gd at line 15 (in the activate -> MessageLog.send_message function).

Steps to reproduce with a lightning scroll:

  1. Start a new game in the part 10 project
  2. Pick up a lightning scroll
  3. Quit to main menu
  4. Select Continue last game
  5. Try to use lightning scroll with an enemy in range
  6. Game crashes with message Cannot call method 'queue_free' on a null value. on the script consumable_component.gd at line 16 (in the consume function).

I also tested the other scrolls and they also similarly to the lightning scroll crash the game once a valid target has been selected.

I did try to reproduce these in the part 13 project as well but I found I couldn't pick up potions or scrolls there. The message log would just print "There is nothing here to pick up." when standing on top of a potion or a scroll and pressing g.

SelinaDev commented 7 months ago

Thank you for that info. I just confirmed it. Somehow I completely forgot to integrate the consumable components into the save system. The save system gave me some headaches, so I admit that it was a bit rushed in the end. This might be a slightly bigger task to fix, but I'll work on it as soon as I can.

The part regarding part 13 is also worrying. I did change the code that identifies items, and messed up there, as it seems. I'll create a separate issue for that, as this is unrelated to the save game problem.

SelinaDev commented 7 months ago

I figured it out. I did in fact not forget the consumable components. They don't have save/load code because they are stateless and get restored when their parent entity is restored. I assumed that this should fully restore items in the inventory. This is essentially true, but I forgot that the entity property on components gets automatically set via an @onready variable. As items that get restored in the inventory don't enter the tree, their entity remains null. I solved this by explicitly setting it for consumable components. I'll soon update the corresponding tutorial code.

Jomii commented 7 months ago

I tested the fix as well and it works great now, thanks!