Run something every month or year
The goal
Section titled “The goal”Something happens on a rhythm of the calendar: a harvest each year, a tithe each month, a tale at the turn of a season.
Do it, step by step
Section titled “Do it, step by step”In a scheme: On Date
Section titled “In a scheme: On Date”Add an On Date node. It has no input, and the calendar starts it while the scheme runs. Set When to A New Month, A New Season or A New Year, narrow it with Only Month (from 1) or Only Season, and link Began.
With no scheme running: a decision
Section titled “With no scheme running: a decision”- In Behaviour ▸ Decisions, click From a template and pick A yearly event for everyone, or A recurring event for a decision that starts its own scheme with a dialogue.
- The yearly template’s Action is Only tell the game: nothing happens in the story until your code listens to
IM.onDecisionMade, or until you set Action to Start a scheme, Send a signal or Call a method. - Each character no player plays thinks about it on its own rhythm: Think about it every (days) at 360, which matches A year has (days) at first, with Starts at 100. It does not wait for the first day of a year. For that, use On Date or the clock events.
- Turn on Characters think for themselves in Behaviour ▸ Settings, as in Let characters start schemes on their own.
In code: the clock events
Section titled “In code: the clock events”Each listener runs in its own try and catch.
| Event | Gives | When |
|---|---|---|
WorldClock.MonthBegan, IM.onMonthBegan |
Month number, from 1 | A month begins. Only with a calendar. |
WorldClock.YearBegan, IM.onYearBegan |
Year number | A year begins. Also without a calendar. |
WorldClock.SeasonChanged, IM.onSeasonChanged |
Season ID | The season changes. Only with a calendar. |
WorldClock.DayPassed, IM.onDayPassed |
Day number | A day has passed. |
The demo subscribes in DemoRules.cs, lines 196 and 197, and unsubscribes in OnDisable:
WorldClock.MonthBegan += OnMonth;WorldClock.YearBegan += OnYear;Its OnYear starts with if (!Hosting) return;, so only the host of the demo world does the yearly work.
What you will see
Section titled “What you will see”On the day a year begins, the events come in a fixed order: year, month, season, then the day itself. They come once for each day that really passed.
What can go wrong
Section titled “What can go wrong”- Nothing fires. The days do not pass. Days pass is When the game says so at first.
- A month or season event never comes. The world has no calendar. A year event still comes.
- A load or a restart raises nothing. The events are not raised when the day is set from code, on a load or on a restart. Use On Game Loaded for that, or
WorldClock.Restartedfor a load, a restart or a set day. - Handlers add up. A static handler you never remove outlives your object. Unsubscribe in
OnDisable. - Clients. Clients hear the clock events too, as the host’s days arrive, which is why the demo’s handler checks
Hosting.WorldClock.Advancedoes nothing where the game’s mind does not run, such as a multiplayer client, so write to the world from the host only, as the demo does.
How it connects
Section titled “How it connects”- Days and the calendar are on Days, calendar and game speed, and every event on Events reference.
- The node is On Date. To wait inside a flow, see Wait some days or until something happens.
- Code that writes to the world is on Host-aware game code. After a load, see Restore the scene after a load.