Elaborate more on protfunc usage. Resolve #3108

This commit is contained in:
Griatch 2023-02-26 10:42:39 +01:00
parent 1d94d77248
commit 0e71e00fae
3 changed files with 73 additions and 45 deletions

View file

@ -1,28 +1,35 @@
"""
Protfuncs are FuncParser-callables that can be embedded in a prototype to
provide custom logic without having access to Python. The protfunc is parsed at
the time of spawning, using the creating object's session as input. If the
protfunc returns a non-string, this is what will be added to the prototype.
Protfuncs are FuncParser-callables that can be embedded in a prototype to provide custom logic
without having access to Python. The protfunc is parsed at the time of spawning, using the creating
object's session as input. If the protfunc returns a non-string, this is what will be added to the
prototype.
In the prototype dict, the protfunc is specified as a string inside the prototype, e.g.:
{ ...
```python
{ ...
"key": "$funcname(args, kwargs)"
"key": "$funcname(args, kwargs)"
... }
... }
```
Available protfuncs are either all callables in one of the modules of `settings.PROT_FUNC_MODULES`
or all callables added to a dict FUNCPARSER_CALLABLES in such a module.
or all callables added to a dict FUNCPARSER_CALLABLES in such a module. By default, base inlinefuncs
for text manipulation and searching are included, as well as the special `$protkey` function.
See the Prototypes and Spawner documentation for more info.
def funcname (*args, **kwargs)
```python
def funcname (*args, **kwargs):
return "replacement text"
```
At spawn-time the spawner passes the following extra kwargs into each callable (in addition to
what is added in the call itself):
- session (Session): The Session of the entity spawning using this prototype.
- prototype (dict): The dict this protfunc is a part of.
- current_key (str): The active key this value belongs to in the prototype.
- `session` (Session): The Session of the entity spawning using this prototype.
- `prototype` (dict): The dict this protfunc is a part of.
- `current_key` (str): The active key this value belongs to in the prototype.
Any traceback raised by this function will be handled at the time of spawning and abort the spawn
before any object is created/updated. It must otherwise return the value to store for the specified
@ -36,8 +43,9 @@ from evennia.utils import funcparser
def protfunc_callable_protkey(*args, **kwargs):
"""
Usage: $protkey(keyname)
Returns the value of another key in this prototoype. Will raise an error if
the key is not found in this prototype.
the key is not found in this prototype.
"""
if not args: