summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--catbird/mixins.scm22
1 files changed, 17 insertions, 5 deletions
diff --git a/catbird/mixins.scm b/catbird/mixins.scm
index 3945aab..6e1cd4a 100644
--- a/catbird/mixins.scm
+++ b/catbird/mixins.scm
@@ -150,7 +150,16 @@
(define-class <scriptable> (<updatable>)
(paused? #:accessor paused? #:init-form #f #:init-keyword #:paused?)
- (agenda #:getter agenda #:init-thunk make-agenda))
+ ;; The agenda gets created lazily when the first script is run.
+ ;; This reduces overhead for scriptable objects that don't end up
+ ;; running any scripts.
+ (agenda #:accessor agenda #:init-value #f))
+
+(define-method (ensure-agenda (obj <scriptable>))
+ (or (agenda obj)
+ (let ((fresh-agenda (make-agenda)))
+ (set! (agenda obj) fresh-agenda)
+ fresh-agenda)))
(define-method (on-pause (obj <scriptable>))
#t)
@@ -170,15 +179,18 @@
(define-method (update/around (obj <scriptable>) dt)
(unless (paused? obj)
- (with-agenda (agenda obj)
- (update-agenda dt)
+ (let ((agenda (agenda obj)))
+ (when agenda
+ (with-agenda agenda
+ (update-agenda dt)))
(next-method))))
(define-syntax-rule (run-script obj body ...)
- (with-agenda (agenda obj) (script body ...)))
+ (with-agenda (ensure-agenda obj) (script body ...)))
(define-method (stop-scripts obj)
- (with-agenda (agenda obj) (clear-agenda)))
+ (when (agenda obj)
+ (with-agenda (agenda obj) (clear-agenda))))
;;;