GMTimeLine
November 3, 2022 · View on GitHub
A pure code approach to timelines in Game Maker
Examples
GMTimeline allows you to create timelines by simply chaining events, like so:
timer = 0;
/// @param {Real} msLeft
var updateTime = function (msLeft) {
timer = msLeft;
};
timeline = new Timeline()
.keyPress(vk_enter) // Wait till the Enter key is pressed
.delay(2, updateTime) // Wait 2 seconds, pass time passed to updateTime function
// Instantiate 5 instances of OMonster in intervals of 1 second, wait for the instances to be destroyed
// before considering the event to be finished
.instantiate(room_width / 2, 500, 5, 1, OMonster, WaitingMode.Destroy, {
direction: 0, // Pass properties that will be applied to the instances
})
.await() // Wait for the previous event to finish
.delay(2, updateTime) // Wait for 2 seconds
.instantiate(room_width / 2, 500, 10, 0.5, OMonster, WaitingMode.Destroy, {
direction: 180,
})
.await()
// Run custom logic once
.once(
function (done, data) {
show_debug_message(data.foo);
done();
},
{
foo: "bar",
}
)
// Run custom logic every step, until done() is called or timeline is released
.every(
function (done, data) {
var seconds = floor(data._secondsPassed);
// Every second
if (data.seconds < seconds) {
effect_create_above(
ef_firework,
random(room_width),
random(room_height),
10,
random(c_white)
);
data.seconds = seconds;
}
// Stop after 5 seconds
if (seconds == 5) {
done();
}
},
{
seconds: 0,
}
)
.await(); // Wait for the previous event to finish
timeline.onFinish(function () {
// Timeline is finished, stop processing any logic left behind by "every"
timeline.release();
});
timeline.start();
Or how about a sequence of timelines:
sequence = new Sequence([
new Timeline()
.once(function (done) {
OLog.logString("Starting a sequence of timelines");
done();
})
.delay(3, updateTime)
.await(),
new Timeline()
.once(function (done) {
OLog.logString(
"First timeline in sequence finished, second timeline started"
);
done();
})
.delay(3, updateTime)
.await(),
]);
sequence.onFinish(function (data) {
show_debug_message(
"Sequence complete in " + string(data.duration / 1000) + " seconds"
);
});
sequence.start();
Installing
Simply download GMTimeLine.yyp and drag it over to your project. A Marketplace link will come soon!
Documentation
Classes
Timeline() ⇒ Struct.Timeline
Creates a new timeline
Sequence(timelines) ⇒ Struct.Sequence
Creates a new sequence of timelines
| Param | Type | Description |
|---|---|---|
| timelines | Array<Struct.Timeline> | An array of timelines |
Timescale
Timelines take global.timeScale into account. 1 is normal speed, 0.5 is half speed, 2 is double speed, etc.
Timeline Functions
- Timeline() ⇒
Struct.Timeline -
Creates a new timeline
- start() ⇒
Struct.Timeline -
Starts/continues the timeline
- await() ⇒
Struct.Timeline -
Creates an Instantiate event that will instantiate objects
- delay(seconds, [callback]) ⇒
Struct.Timeline -
Delays events from further execution
- limit(seconds) ⇒
Struct.Timeline -
Limits time for previous batch of events to finish. Takes timescale into account. If limit is reached, the timeline will proceed as if the previous batch completed
- instantiate(x, y, amount, interval, obj, mode, properties) ⇒
Struct.Timeline -
Creates an Instantiate event that will instantiate objects
- keyPress(key) ⇒
Struct.Timeline -
Waits for a key to be pressed
- keyReleased(key) ⇒
Struct.Timeline -
Waits for a key to be released
- every(func, data) ⇒
Struct.Timeline -
Allows for a function to be run every step
- once(callback, data) ⇒
Struct.Timeline -
Allows for a custom function to be called in between events, the function gets called back a callback function that can be called to proceed with the timeline
- onFinish(callback)
-
Allow you to pass a function to be called when the timeline is finished
Timeline() ⇒ Struct.Timeline
Creates a new timeline
start() ⇒ Struct.Timeline
Starts/continues the timeline
await() ⇒ Struct.Timeline
Waits for previous events to finish
delay(seconds, [onProgress]) ⇒ Struct.Timeline
Allows for a delay between events
| Param | Type | Description |
|---|---|---|
| seconds | Real | The delay in seconds, takes timescale into account |
| [onProgress] | function | The function called every frame during the delay passing back remaining time in frames |
limit(seconds) ⇒ Struct.Timeline
Sets a time limit in seconds for a batch of events to finish
| Param | Type | Description |
|---|---|---|
| seconds | Real | The limit in seconds, takes timescale into account |
keyPress(key) ⇒ Struct.Timeline
Waits for a key to be pressed
| Param | Type | Description |
|---|---|---|
| key | Constant.VirtualKey | Real | Virtual key index |
keyReleased(key) ⇒ Struct.Timeline
Waits for a key to be released
| Param | Type | Description |
|---|---|---|
| key | Constant.VirtualKey | Real | Virtual key index |
instantiate(x, y, amount, interval, obj, mode, properties, callback) ⇒ Struct.Timeline
Will instantiate objects at the specified interval
| Param | Type | Description |
|---|---|---|
| x | Real | x coordinate to spawn instance at |
| y | Real | y coordinate to spawn instance at |
| amount | Real | Amount of instances to spawn |
| interval | Real | Interval between each instance |
| obj | Object | Object to instantiate |
| mode | WaitingMode | Waiting mode |
| properties | Struct | Properties to apply to each instance |
| callback | Function | Callback to be called when all instances are instantiated |
WaitingMode is an enum with the following values:
Default - Will simply create the instances and consider the event finished
Destroy - Will consider the event finished when all instances are destroyed
Attention!
If you want to use the Destroy mode, you must call destroy(id) on your instances to signal the timeline that they are destroyed.
The function will destroy the instance.
every(func, data) ⇒ Struct.Timeline
Allows for a function to be run every step
| Param | Type | Description |
|---|---|---|
| func | function(done, data) | Function to run every step - will be passed a function as its first argument. Call it to indicate that the function is done and the timeline can proceed |
| data | Struct.Any | Data to be passed to the callback function |
once(callback, data) ⇒ Struct.Timeline
Allows for a custom function to be called in between events, the function gets called back a with callback function that can be called to proceed with the timeline
| Param | Type | Description |
|---|---|---|
| callback | function(done, data) | The function to be called back. will be passed a function as its first argument. Call it to indicate that the function is done and the timeline can proceed |
| data | Struct.Any | Data to be passed to the callback function |
onFinish(callback) ⇒ void
Allow you to pass a function to be called when the timeline is finished
| Param | Type | Description |
|---|---|---|
| callback | function | Function to be called when timeline finishes |