Append character fix to evadventure tutorial. Resolve #3559

This commit is contained in:
Griatch 2023-08-20 18:48:16 +02:00
parent f034289ca0
commit b819bff556
3 changed files with 61 additions and 2 deletions

View file

@ -168,6 +168,8 @@ class EvAdventureCharacter(LivingMixin, DefaultCharacter):
Above we have assumed the `EquipmentHandler` (`.equipment`) has methods `.validate_slot_usage`, `.add` and `.remove`. But we haven't actually added them yet - we just put some reasonable names! Before we can use this, we need to go actually adding those methods.
When you do things like `create/drop monster:NPC`, the npc will briefly be in your inventory before being dropped on the ground. Since an NPC is not a valid thing to equip, the EquipmentHandler will complain with an `EquipmentError` (we define this see below). So we need to
## Expanding the Equipmenthandler
## `.validate_slot_usage`
@ -338,8 +340,7 @@ class EquipmentHandler:
if objslot is obj_or_slot:
slots[slot] = None
ret.append(objslot)
elif obj_or_slot in slots[WieldLocation.BACKPACK]:
# obj in backpack slot
elif obj_or_slot in slots[WieldLocation.BACKPACK]: # obj in backpack slot
try:
slots[WieldLocation.BACKPACK].remove(obj_or_slot)
ret.append(obj_or_slot)
@ -498,6 +499,55 @@ In the `.armor()` method we get the item (if any) out of each relevant wield-slo
In `.weapon()`, we simply check which of the possible weapon slots (weapon-hand or two-hands) have something in them. If not we fall back to the 'Bare Hands' object we created in the [Object tutorial lesson](./Beginner-Tutorial-Objects.md#your-bare-hands) earlier.
### Fixing the Character class
So we have added our equipment handler which validate what we put in it. This will however lead to a problem when we create things like NPCs in game, e.g. with
create/drop monster:evadventure.npcs.EvAdventureNPC
The problem is that when the monster is created it will briefly appear in your inventory before being dropped, so this code will fire on you when you do that (assuming you are an `EvAdventureCharacter`):
```python
# mygame/evadventure/characters.py
# ...
class EvAdventureCharacter(LivingMixin, DefaultCharacter):
# ...
def at_object_receive(self, moved_object, source_location, **kwargs):
"""
Called by Evennia when an object arrives 'in' the character.
"""
self.equipment.add(moved_object)
```
At this means that the equipmenthandler will check the NPC, and since it's not a equippable thing, an `EquipmentError` will be raised, failing the creation. Since we want to be able to create npcs etc easily, we will handle this error with a `try...except` statement like so:
```python
# mygame/evadventure/characters.py
# ...
from evennia import logger
from .equipment import EquipmentError
class EvAdventureCharacter(LivingMixin, DefaultCharacter):
# ...
def at_object_receive(self, moved_object, source_location, **kwargs):
"""
Called by Evennia when an object arrives 'in' the character.
"""
try:
self.equipment.add(moved_object)
except EquipmentError:
logger.log_trace()
```
Using Evennia's `logger.log_trace()` we catch the error and direct it to the server log. This allows you to see if there are real errors here as well, but once things work and these errors are spammy, you can also just replace the `logger.log_trace()` line with a `pass` to hide these errors.
## Extra credits