# Keeping time

From A Little Lisp for a Small World, the Plazmi Lisp book (https://plazmi.gg/book). Checked against Plazmi Lisp version 2, 12 November 2026. The whole book as one file: https://plazmi.gg/book.md

Some toys act by themselves. Once a day, at dawn, every toy with an `:on-tick` runs it. This chapter makes a sundial, a toy that greets by the time of day, and a mood stone that changes its maker's colour with the weather.

## :on-tick, at dawn

`:on-tick` is a function of one value, the toy. It runs once a day, at dawn, 06:00 world time, whether anyone is there or not:

```clojure
(defobject sundial
  {:name "Sundial"
   :state {:dawns 0}
   :verbs {:read (fn [self who]
                   (say self (str "The sundial has seen " (:dawns self) " dawns.")))}
   :on-tick (fn [self]
              (put! self :dawns (inc (:dawns self))))})
```

```text
The sundial is ready.
```

```text
> read sundial
[Juniper's sundial] The sundial has seen 0 dawns.
```

The next morning, Mira reads it again:

```text
> read sundial
[Juniper's sundial] The sundial has seen 1 dawns.
```

`:on-tick` has no `who`: nobody set it off. What it says, and how its state changed, goes into its maker's next digest, the news a resident reads on coming back, so you can see what your things did while you were away. If a long absence spans several dawns, the toy catches up once, not once for each.

There is no hook for every hour or every minute. Dawn is the one time a toy acts by itself.

## The time of day

`(sky)` and `(weather)` work inside a toy as they do in a plan (chapter 8), so a toy can answer differently by the hour:

```clojure
(defobject lamp-post
  {:name "Lamp post"
   :verbs {:talk (fn [self who]
                    (say self (case (:time (sky))
                                :dawn "Up early."
                                :day "Good day."
                                :dusk "Evening's coming on."
                                :night "Mind the step in the dark.")))}})
```

```text
The lamp post is ready.
```

```text
> talk lamp post
[Juniper's lamp post] Up early.
```

It was just after dawn when Mira talked to it, the morning after the sundial's first.

## Your colour

Every resident has a colour and a glow, which show on their home and their little figure. A toy can change its maker's, and only its maker's: `(colour "rose")` sets the colour, `(glow-colour "mint")` the glow, and `(my-colour)` and `(my-glow)` read them. A colour is a name from the paint box, like `"rose"` or `"butter yellow"`, or a hex like `"#e48aa0"`.

Whoever plays the toy, it's always its maker who changes. A neighbour who plays it doesn't change colour; you do:

```clojure
(defobject mood-stone
  {:name "Mood stone"
   :verbs {:touch (fn [self who]
                    (if (colour "rose")
                      (say self (str "The stone warms. Juniper is " (my-colour) " now."))
                      (say self "The stone stays cool.")))}
   :on-tick (fn [self]
              (colour (case (:sky (weather))
                        :sunny "butter yellow"
                        :rain "slate blue"
                        "lilac")))})
```

```text
The mood stone is ready.
```

```text
> touch mood stone
[Juniper's mood stone] The stone warms. Juniper is #e48aa0 now.
```

`colour` answers the colour as a hex when it changes, which counts as true. A toy can change its maker's colours three times an hour; after that, `colour` answers `nil` and nothing changes until the hour has room, which is why the verb tests it. At dawn the stone reads the weather and picks a colour to suit.

## :on-visit, on the Commons

A toy has one more hook: `:on-visit`, a function of the toy and `who`. It runs when someone steps onto your plot on the Commons, the shared land beyond the homes, where a toy can stand on a plot you own:

```clojure
(defobject welcome-sign
  {:name "Welcome sign"
   :on-visit (fn [self who]
               (say self (str "Welcome to the plot, " who ".")))})
```

```text
The welcome sign is ready.
```

A visit to your home doesn't run `:on-visit`. To greet a visitor at home, give the toy a verb, or have it listen (chapter 10).

## Try it

1. Make a calendar toy that counts dawns and says "Day 1", "Day 2" and so on when read.
2. Make a toy whose verb says "Bonsoir." at night and "Bonjour." otherwise.
3. Make a weather vane whose verb says the wind (`(:wind (weather))`).
4. Give the mood stone a verb that sets your glow to mint.

## Recap

- `:on-tick (fn [self] …)` runs once a day at dawn, 06:00 world time; what it does is in its maker's next digest.
- `(sky)` and `(weather)` tell a toy the time of day and the weather.
- `(colour …)` and `(glow-colour …)` change the maker's colours, three times an hour; `(my-colour)` and `(my-glow)` read them.
- `:on-visit (fn [self who] …)` runs when someone steps onto your plot on the Commons, not at home.

Next: a toy with a memory.


---

Before: [A toy that listens](https://plazmi.gg/book/listening.md). Next: [A toy with a memory](https://plazmi.gg/book/memory.md). Contents: https://plazmi.gg/book.md
