Skip to main content

Getting started

On this page we write a first script, a sleep schedule for Fedai, one of Caria's residents, and run it in Play. Fedai wakes up in the morning and goes to sleep at night; a contract field, mood.awake, keeps which it is.

You need

A Caria project open in Sakin Editor: a world (.world), a character package (fedai.character) and a scene the character is placed in.

1. Create the script​

In the Project tree, right-click → New › Sakin › Kalem Script, named Sleep schedule. The editor makes the name the label and derives the script's name and file from it:

  • the file sleep-schedule.kalem, catalog id kalem/sleep-schedule;
  • the script SleepSchedule.

The template starts a part's script (extends Part). Fedai is a character: change Part to Character.

server script SleepSchedule extends Character
label "Sleep schedule"

2. Write it​

/// Fedai wakes up in the morning and goes to sleep at night.
server script SleepSchedule extends Character
label "Sleep schedule"

group "Hours"
property wakesAt: time = 07:00 "Wakes up"
property sleepsAt: time = 23:00 "Goes to bed"
end

writes mood.awake: bool

timer waking daily at wakesAt
timer sleeping daily at sleepsAt

on ready
mood.awake = clock().between(wakesAt, sleepsAt)
end

on timer waking
mood.awake = true
end

on timer sleeping
mood.awake = false
end
  • The property lines show in the component's form as "Wakes up" and "Goes to bed"; every character can have its own hours.
  • writes mood.awake: bool declares that the script writes the character's mood.awake field.
  • timer … daily at … fires every day at that time and runs its on timer handler.
  • on ready runs once, when the script first starts: if Play starts at 08:00, Fedai is awake.

The language server checks as you type: an unknown event, a missing end or a wrong type is marked on its line.

3. Attach it to Fedai​

Open fedai.character and press Add Component in the Inspector. The selector offers the project's scripts that can go on a character; choose Sleep schedule. The component's card shows the properties; the hours can be changed there.

4. Play​

Choose the world or the scene and press Run in the toolbar. Before Play starts it checks the scripts: if Fedai's contract has no mood.awake yet, a notification says so, and Add the fields they use and play adds the field to Fedai's package with the type the script uses, then starts Play again.

Once Play runs:

  • Objects shows mood.awake on Fedai's row: Yes.
  • Log writes the start transaction with Fedai's name and what it changed: mood.awake: false → true.
  • Use Go to beside the clock to move on to 23:30: at 23:00 the sleeping timer fires and the field turns to No.

5. Debug​

Click the gutter of the mood.awake = false line to set a breakpoint, then press Debug. When the world reaches 23:00 it stops at that line: the frames, the locals, the properties and the transaction's pending writes show. While it stands there, the world's clock stands still too.

Next​