Event order in ct.js
September 28, 2020
Event order in ct.js
These events are always executed in the following order:
- Core ct.js library runs;
- Modules get initialized;
- Custom scripts added at project settings are executed;
- room's
oncreate
event is called, which is emitted when a user starts a game or navigates to a new room; oncreate
is applied for each copy;- then the main game loop starts:
onstep
event is emitted for all the copies in the room;onstep
event for current room is called;ondestroy
is called for all the copies marked to bekill
ed;- all the copies are reordered then;
ondraw
is called for all the copies;ondraw
is called for a room;- input events are cleared. Waiting for a new game loop iteration.
- When a user moves to a new room, an
onleave
event is called for the latest room.
Call order with mod's injections
At startup:
- Core ct.js library runs.
load.js
.- Modules get initialized.
- Custom scripts added at project settings are executed;
resload.js
runs once all the assets have finished loading.start.js
— called right before a game is started (no room is created yet).- The first room gets created.
- Room's OnCreate is called.
roomoncreate.js
.switch.js
is called.ct.camera
is properly positioned.
At room transition:
- Previous room's OnLeave event is called.
roomonleave.js
.- New room's OnCreate is called.
- Copies are created here, with all their events and injections.
roomoncreate.js
switch.js
ct.camera
is properly positioned.
At each frame:
beforeframe.js
is run in global context.beforestep.js
withthis
being the current type.- Copies' own OnStep is called.
afterstep.js
withthis
being the current type.beforeroomstep.js
withthis
being a current room (can be different fromct.room
)- Room's own OnStep is called.
afterroomstep.js
withthis
being a current room (can be different fromct.room
)- Copies get destroyed with their OnDestroy event.
- Camera position is updated
beforedraw.js
withthis
being the current type.- Copies' own OnDraw is called.
afterdraw.js
withthis
being the current type.beforeroomdraw.js
withthis
being a current room (can be different fromct.room
)- Room's own OnDraw is called.
afterroomdraw.js
withthis
being a current room (can be different fromct.room
)afterframe.js
is called before moving on to the next frame.
At copy creation:
onbeforecreate.js
- Copies' own OnCreate is called.
oncreate.js
At copy deletion:
ondestroy.js
- Copy's own OnDestroy event is called.