Time-sensitive events

This page describes **time-sensitive events**. These are events that are either only usable during a certain period of time (e.g. an NPC appearing only on Fridays), or which reset themselves at a certain time (e.g. midnight every day) or have a delay after being used (e.g. wait 24 hours).

## Script Switches

*Main article: Events*
All time-sensitive events will need to decide when they should do their special action, and when they should not. You can do this by using Game Switches (see the page Events for more information).

As this page describes time-sensitive events, it will use the Game Switches that depend on the time/day/etc. You can of course create new Game Switches to cover the periods of time you want an event to behave differently in.

## Time period only

These events will only be active during a certain period of time (e.g. only between 6pm and 8pm every day, or on Fridays only).

Typical uses for this kind of time-sensitive event are:

* A set Pokémon encounter that can only be triggered during the day.
* An NPC that appears only on a certain day of the week.
* A trainer that only battles at night.
* A prize give-away that is only available during a certain time.
* The Bug Catching Contest that only happens on certain days.

Events like this should be set up as follows:

* Page 1 - The event as it appears at all other times. This may be blank, or an NPC reciting a line of dialogue, whatever is appropriate.
* Page 2 - The event as it appears during the special period of time (e.g. the NPC challenges the player to a battle). This page will depend on one or more time-related Script Switches.
* Optional other pages - The event as it appears during another special period of time, or the secondary "talking to the NPC after defeating them" page, etc.

## Time delay

Once triggered, these events will begin counting time, and will not allow the primary interaction again until a certain amount of time has passed (e.g. 24 hours). If interacted with before the time has elapsed, a different string of events can occur (e.g. a message saying "Sorry, come back tomorrow.").

Typical uses for this kind of time-sensitive event are:

* An NPC that gives out a gift once every 24 hours.
* A lottery which you can only enter once every 24 hours.
* A berry/apricorn tree that only flowers every 24 hours.
* A tree that can be Headbutted, but which will only produce an encounter if left alone for an hour.
* A switch that will open a door for only a certain amount of time.
* A set Pokémon encounter that occurs once every 24 hours.

### Event page 1

The first event page describes the primary interaction, and something in this interaction will start the event to begin counting time. To make the event start counting time, use the following method:

```
pbSetEventTime

```

This method sets the event's personal variable to the current time, and also sets the event's Self Switch A to ON. Once this method has been used, the event's processing should end.

You can use the above method to affect multiple events on the same map at once (e.g. a switch affecting a timed door). To do this, simply include the event IDs of those other events as arguments, like so:

```
pbSetEventTime(2, 3)

```

This example will set the current event's personal variable to the current time and set its Self Switch A, and will also do the same for events 2 and 3 on the same map. Note that this method can *only* affect events on the same map.

### Event page 2

The second event page describes the "intermediate" interaction, which is what happens if the player interacts with the event while it is counting. This is typically just a message saying "Try again tomorrow" or something to that effect.

This page must have the condition of Self Switch A being ON.

### Event page 3

The third event page checks whether the desired amount of time has elapsed. It will check this automatically each time the player enters the map that the event is in, but not while the player remains on the map - they must leave and return in order for the event to recheck itself.

This page must have the condition of Self Switch A being ON (like the second page), it must have the Autorun trigger, *and* it must have the condition of a Game Switch (e.g. 23: `s:cooledDown?(86400)`) being ON.

The content of this page are as follows:

```
@>Control Self Switch: A =OFF
@>Script: setTempSwitchOn("A")
@>

```

This page contains only these two lines, and it *must* contain both of them.

The Game Switch used as one of the conditions of this page determines what length of time the event counts down for. It is typically a Script Switch of the following form:

```
s:cooledDown?(86400)

```

The number is the length of time in seconds (86400 seconds = 24 hours). You can create your own Script Switches with different lengths of time.

## Reset at midnight

This kind of event is similar to the time delay event above, except it counts days rather than seconds. More accurately, it counts the number of midnights that have passed between uses. That is, if the event was made to count 1 day, then it could be used just before and just after midnight and be the regular interaction each time.

Typical uses for this kind of time-sensitive event are:

* An NPC that gives out a gift once every day.
* A lottery which you can only enter once a day.
* A plot device that triggers at midnight.
* A puzzle that resets/changes at midnight.

The event's layout will be the same as described above for a time delay event. The only difference is which Game Switch to use as a condition for page 3. The Game Switch you should use instead will typically be a Script Switch of the following form:

```
s:expiredDays?(1) && tsOff?("A")

```

The number is how many days to wait between regular interactions (i.e. the number of midnights that need to elapse). A number of 1 means the event can be used once each day, 2 means every other day, and so on.

## Reset at another time

This kind of event is the same as the "reset at midnight" event above, except the checkpoint time is not midnight but a different time (e.g. 4pm). This kind of event is unusual to have, so creating them is a little more complex.

To create this kind of event, begin by making a "time delay" event (*not* a "reset at midnight" event) as described above.

This event will need to calculate how many seconds have to pass after the current time before the event will have "cooled down", rather than having a fixed cool-down period. This length of time will need to be stored in a Game Variable specifically used for this purpose (number 42 in this example). Each event or set of events will need its own Game Variable.

Page 3 of the event will need to have a new Script Switch as its condition. This Script Switch should have the following name:

```
s:cooledDown?(pbGet(42))

```

The number is that of the Game Variable mentioned above. Each event or set of events will need its own Script Switch whose number is that of their own Game Variable.

The only other change to the event will be in page 1, where `pbSetEventTime` appears. In addition to that line of code, you will also need to include the following:

```
checkpoint = 16
number = 1
t = 0
tNow = pbGetTimeNow
t += (checkpoint - tNow.hour - 1) * 3600
t += 3600 - tNow.min * 60 - tNow.sec
t += 24 * 3600 if tNow.hour >= checkpoint
t += 24 * 3600 * (number - 1)
pbSet(42, t)

```

The three highlighted numbers are the only things you will need to change. The first is the hour you want the checkpoint time to be (4pm in this example), and the second is the number of those checkpoints you want to pass before the event resets (1 means every day, 2 means every other day, etc.). The third is the Game Variable number as mentioned above.

This code will only allow checkpoints to be on the hour, but that should be more than sufficient for most users. It can be expanded to allow checkpoints at more specific times, but that is an exercise left to the developer.