Make sure Typeclass.at_init() is always called on cache initialization for any typeclass. Resolve #2641

This commit is contained in:
Griatch 2022-02-07 20:45:48 +01:00
parent a427594f25
commit 5e2372f79d
6 changed files with 27 additions and 8 deletions

View file

@ -157,8 +157,6 @@ class ScriptDB(TypedObject):
# deprecated ...
pass
if isinstance(value, (str, int)):
from evennia.objects.models import ObjectDB
value = to_str(value)
if value.isdigit() or value.startswith("#"):
dbid = dbref(value, reqhash=False)

View file

@ -473,6 +473,14 @@ class ScriptBase(ScriptDB, metaclass=TypeclassBase):
super().delete()
return True
def at_init(self):
"""
Called when the Script is cached in the idmapper. This is usually more reliable
than overriding `__init__` since the latter can be called at unexpected times.
"""
pass
def at_script_creation(self):
"""
Should be overridden in child.

View file

@ -11,9 +11,11 @@ from evennia.scripts.scripts import DoNothing, ExtendedLoopingCall
class TestScript(BaseEvenniaTest):
def test_create(self):
"Check the script can be created via the convenience method."
obj, errors = DefaultScript.create("useless-machine")
self.assertTrue(obj, errors)
self.assertFalse(errors, errors)
with mock.patch("evennia.scripts.scripts.DefaultScript.at_init") as mockinit:
obj, errors = DefaultScript.create("useless-machine")
self.assertTrue(obj, errors)
self.assertFalse(errors, errors)
mockinit.assert_called()
class TestScriptDB(TestCase):