Fixing tabulation in handler tut
This commit is contained in:
parent
59bb10e76a
commit
e9cce46d66
1 changed files with 87 additions and 60 deletions
|
|
@ -35,13 +35,23 @@ print(obj.key)
|
||||||
>>> "test_extra"
|
>>> "test_extra"
|
||||||
```
|
```
|
||||||
|
|
||||||
What happens here is that we make a new class `NameChanger`. We use the `@lazy_property` decorator to set it up - this means the handler will not be actually created until someone really wants to use it, by accessing `obj.namechange` later. The decorated `namechange` method returns the handler and makes sure to initialize it with `self` - this becomes the `obj` inside the handler!
|
What happens here is that we make a new class `NameChanger`. We use the
|
||||||
|
`@lazy_property` decorator to set it up - this means the handler will not be
|
||||||
|
actually created until someone really wants to use it, by accessing
|
||||||
|
`obj.namechange` later. The decorated `namechange` method returns the handler
|
||||||
|
and makes sure to initialize it with `self` - this becomes the `obj` inside the
|
||||||
|
handler!
|
||||||
|
|
||||||
We then make a silly method `add_to_key` that uses the handler to manipulate the key of the object. In this example, the handler is pretty pointless, but grouping functionality this way can both make for an easy-to-remember API and can also allow you cache data for easy access - this is how the `AttributeHandler` (`.attributes`) and `TagHandler` (`.tags`) works.
|
We then make a silly method `add_to_key` that uses the handler to manipulate the
|
||||||
|
key of the object. In this example, the handler is pretty pointless, but
|
||||||
|
grouping functionality this way can both make for an easy-to-remember API and
|
||||||
|
can also allow you cache data for easy access - this is how the
|
||||||
|
`AttributeHandler` (`.attributes`) and `TagHandler` (`.tags`) works.
|
||||||
|
|
||||||
## Persistent storage of data in handler
|
## Persistent storage of data in handler
|
||||||
|
|
||||||
Let's say we want to track 'quests' in our handler. A 'quest' is a regular class that represents the quest. Let's make it simple as an example:
|
Let's say we want to track 'quests' in our handler. A 'quest' is a regular class
|
||||||
|
that represents the quest. Let's make it simple as an example:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# for example in mygame/world/quests.py
|
# for example in mygame/world/quests.py
|
||||||
|
|
@ -59,15 +69,15 @@ class Quest:
|
||||||
# progress of this quest
|
# progress of this quest
|
||||||
getattr(self, f"step_{self.current_step}")()
|
getattr(self, f"step_{self.current_step}")()
|
||||||
|
|
||||||
def step_start(self):
|
def step_start(self):
|
||||||
# check here if quest-step is complete
|
# check here if quest-step is complete
|
||||||
self.current_step = "find_the_red_key"
|
self.current_step = "find_the_red_key"
|
||||||
def step_find_the_red_key(self):
|
def step_find_the_red_key(self):
|
||||||
# check if step is complete
|
# check if step is complete
|
||||||
self.current_step = "hand_in_quest"
|
self.current_step = "hand_in_quest"
|
||||||
def step_hand_in_quest(self):
|
def step_hand_in_quest(self):
|
||||||
# check if handed in quest to quest giver
|
# check if handed in quest to quest giver
|
||||||
self.current_step = None # finished
|
self.current_step = None # finished
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -93,8 +103,8 @@ class QuestHandler:
|
||||||
"quest_storage", default={}, category="quests")
|
"quest_storage", default={}, category="quests")
|
||||||
|
|
||||||
def _save(self):
|
def _save(self):
|
||||||
self.obj.attributes.add(
|
self.obj.attributes.add(
|
||||||
"quest_storage", self.storage, category="quests")
|
"quest_storage", self.storage, category="quests")
|
||||||
self._load() # important
|
self._load() # important
|
||||||
self.do_save = False
|
self.do_save = False
|
||||||
|
|
||||||
|
|
@ -150,8 +160,8 @@ class Quest:
|
||||||
def current_step(self):
|
def current_step(self):
|
||||||
return self._current_step
|
return self._current_step
|
||||||
|
|
||||||
@current_step.setter
|
@current_step.setter
|
||||||
def current_step(self, value):
|
def current_step(self, value):
|
||||||
self._current_step = value
|
self._current_step = value
|
||||||
self.questhandler.do_save = True # this triggers save in handler!
|
self.questhandler.do_save = True # this triggers save in handler!
|
||||||
|
|
||||||
|
|
@ -159,9 +169,19 @@ class Quest:
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
The `Quest.__init__` now takes `obj` as argument, to match what we pass to it in `QuestHandler.add`. We want to monitor the changing of `current_step`, so we make it into a `property`. When we edit that value, we set the `do_save` flag on the handler, which means it will save the status to database once it has checked progress on all its quests.
|
The `Quest.__init__` now takes `obj` as argument, to match what we pass to it in
|
||||||
|
`QuestHandler.add`. We want to monitor the changing of `current_step`, so we
|
||||||
|
make it into a `property`. When we edit that value, we set the `do_save` flag on
|
||||||
|
the handler, which means it will save the status to database once it has checked
|
||||||
|
progress on all its quests. The `Quest.questhandler` property allows to easily
|
||||||
|
get back to the handler (and the object on which it sits).
|
||||||
|
|
||||||
The `__serialize__dbobjs__` and `__deserialize_dbobjs__` methods are needed because `Attributes` can't store 'hidden' database objects (the `Quest.obj` property. The methods help Evennia serialize/deserialize `Quest` propertly when the handler saves it. For more information, see [Storing Single objects](../Components/Attributes.md#storing-single-objects) in the Attributes documentation.
|
The `__serialize__dbobjs__` and `__deserialize_dbobjs__` methods are needed
|
||||||
|
because `Attributes` can't store 'hidden' database objects (the `Quest.obj`
|
||||||
|
property. The methods help Evennia serialize/deserialize `Quest` propertly when
|
||||||
|
the handler saves it. For more information, see [Storing Single
|
||||||
|
objects](../Components/Attributes.md#storing-single-objects) in the Attributes
|
||||||
|
documentation.
|
||||||
|
|
||||||
### Tying it all together
|
### Tying it all together
|
||||||
|
|
||||||
|
|
@ -184,14 +204,21 @@ class Character(DefaultCharacter):
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
You can now make your Quest classes to describe your quests and add them to characters with
|
You can now make your Quest classes to describe your quests and add them to
|
||||||
|
characters with
|
||||||
|
|
||||||
character.quests.add(FindTheRedKey)
|
```python
|
||||||
|
character.quests.add(FindTheRedKey)
|
||||||
|
```
|
||||||
|
|
||||||
and can later do
|
and can later do
|
||||||
|
|
||||||
character.quests.check_progress()
|
```python
|
||||||
|
character.quests.check_progress()
|
||||||
|
```
|
||||||
|
|
||||||
and be sure that quest data is not lost between reloads.
|
and be sure that quest data is not lost between reloads.
|
||||||
|
|
||||||
You can find a full-fledged quest-handler example as [EvAdventure quests](evennia.contribs.tutorials.evadventure.quests) contrib in the Evennia repository.
|
You can find a full-fledged quest-handler example as [EvAdventure
|
||||||
|
quests](evennia.contribs.tutorials.evadventure.quests) contrib in the Evennia
|
||||||
|
repository.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue