From edcd06d5319f5f509c2edc5af0f6620b3a1dc708 Mon Sep 17 00:00:00 2001 From: Griatch Date: Sat, 29 Feb 2020 12:24:26 +0100 Subject: [PATCH] Handle case of script.at_repeat() immediately calling stop(). Resolves #2061. --- evennia/commands/default/building.py | 3 ++- evennia/scripts/scripthandler.py | 13 ++++++++++--- evennia/scripts/scripts.py | 15 +++++++++------ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/evennia/commands/default/building.py b/evennia/commands/default/building.py index 44a9350c52..7e195e401e 100644 --- a/evennia/commands/default/building.py +++ b/evennia/commands/default/building.py @@ -3052,7 +3052,8 @@ class CmdScript(COMMAND_DEFAULT_CLASS): ok = obj.scripts.add(self.rhs, autostart=True) if not ok: result.append( - "\nScript %s could not be added and/or started on %s." + "\nScript %s could not be added and/or started on %s " + "(or it started and immediately shut down)." % (self.rhs, obj.get_display_name(caller)) ) else: diff --git a/evennia/scripts/scripthandler.py b/evennia/scripts/scripthandler.py index d924636f8c..90404af0f3 100644 --- a/evennia/scripts/scripthandler.py +++ b/evennia/scripts/scripthandler.py @@ -78,11 +78,18 @@ class ScriptHandler(object): scriptclass, key=key, account=self.obj, autostart=autostart ) else: - # the normal - adding to an Object - script = create.create_script(scriptclass, key=key, obj=self.obj, autostart=autostart) + # the normal - adding to an Object. We wait to autostart so we can differentiate + # a failing creation from a script that immediately starts/stops. + script = create.create_script(scriptclass, key=key, obj=self.obj, autostart=False) if not script: - logger.log_err("Script %s could not be created and/or started." % scriptclass) + logger.log_err("Script %s failed to be created/started." % scriptclass) return False + if autostart: + script.start() + if not script.id: + # this can happen if the script has repeats=1 or calls stop() in at_repeat. + logger.log_info("Script %s started and then immediately stopped; " + "it could probably be a normal function." % scriptclass) return True def start(self, key): diff --git a/evennia/scripts/scripts.py b/evennia/scripts/scripts.py index daf7386f4c..2f5f95852b 100644 --- a/evennia/scripts/scripts.py +++ b/evennia/scripts/scripts.py @@ -221,10 +221,13 @@ class ScriptBase(ScriptDB, metaclass=TypeclassBase): self.at_repeat() # check repeats - callcount = self.ndb._task.callcount - maxcount = self.db_repeats - if maxcount > 0 and maxcount <= callcount: - self.stop() + if self.ndb._task: + # we need to check for the task in case stop() was called + # inside at_repeat() and it already went away. + callcount = self.ndb._task.callcount + maxcount = self.db_repeats + if maxcount > 0 and maxcount <= callcount: + self.stop() def _step_task(self): """ @@ -342,9 +345,9 @@ class DefaultScript(ScriptBase): try: obj = create.create_script(**kwargs) - except Exception as e: + except Exception: + logger.log_trace() errors.append("The script '%s' encountered errors and could not be created." % key) - logger.log_err(e) return obj, errors