GMTimeLine

November 3, 2022 · View on GitHub

A pure code approach to timelines in Game Maker

Examples

Live examples here!

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

ParamTypeDescription
timelinesArray<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

ParamTypeDescription
secondsRealThe delay in seconds, takes timescale into account
[onProgress]functionThe 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

ParamTypeDescription
secondsRealThe limit in seconds, takes timescale into account

keyPress(key) ⇒ Struct.Timeline

Waits for a key to be pressed

ParamTypeDescription
keyConstant.VirtualKey | RealVirtual key index

keyReleased(key) ⇒ Struct.Timeline

Waits for a key to be released

ParamTypeDescription
keyConstant.VirtualKey | RealVirtual key index

instantiate(x, y, amount, interval, obj, mode, properties, callback) ⇒ Struct.Timeline

Will instantiate objects at the specified interval

ParamTypeDescription
xRealx coordinate to spawn instance at
yRealy coordinate to spawn instance at
amountRealAmount of instances to spawn
intervalRealInterval between each instance
objObjectObject to instantiate
modeWaitingModeWaiting mode
propertiesStructProperties to apply to each instance
callbackFunctionCallback 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

ParamTypeDescription
funcfunction(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
dataStruct.AnyData 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

ParamTypeDescription
callbackfunction(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
dataStruct.AnyData to be passed to the callback function

onFinish(callback) ⇒ void

Allow you to pass a function to be called when the timeline is finished

ParamTypeDescription
callbackfunctionFunction to be called when timeline finishes