System API
Systems are the home of your plugin's logic and are designed to be as flexible as possible. Systems provide optional lifecycle events and a robust API to allow you to quickly get going.
Example
Below is an example system. Notice how we are using two of the lifecycle events here.
If you've used Flamework Singletons (opens in a new tab), then you will feel right at home. Systems are very similar to flamework's Controllers and Components.
import { System } from "@rbxts/comet"
export class MySystem extends System implements onInit, onEnd {
onInit(): void {
print ("plugin loaded!")
}
onEnd(): void {
print ("plugin unloaded!")
}
}
Lifecycle Events
Lifecycle Events are essentially extensions you can add to your systems as needed. They can be mixed in to your class using the implements
keyword.
-
onInit
- Called sequentially for each system. This means that any yielding logic may delay other systems from loading.
-
onStart
- onStart is called for each system simultaneously directly after all
onInit
methods finish.
- onStart is called for each system simultaneously directly after all
-
onRender
- onRender is called for each frame and is bound to
RenderStepped
. This method will provide delta time as its one argument.
- onRender is called for each frame and is bound to
-
onEnd
- onEnd is called when the plugin is unloaded. Useful for cleaning up any system specific connections or instances.
System API
The system api is inherited by all systems and can be accessed via the this
keyword.
UI / FX
createWidget()
Creates and returns a view that is mounted to a dock widget.
this.createWidget(name: string, size: Vector2, maxSize: Vector2): View
createOverlay()
Creates and returns a view that is mounted to the viewport.
this.createOverlay(name: string): View
createButton()
Creates a button on the toolbar.
this.createOverlay(
title: string,
toolTip: string,
image: sring,
toggleable = true,
enabledWhenViewportHidden = false,
): View
buildMenu()
Creates a menu builder.
this.buildMenu(): MenuBuilder
playSFX()
Easily play an sound effect.
Under the hood, the initial call will load the sound which may cause a delay. Subsequent calls will be instantaneous as sounds are cached.
this.playSFX(soundId: string | number, looped?: boolean): void
History
record()
The recording api is tasteful wrapper for ChangeHistoryService
.
Be sure to commit, append or cancel a recording before starting another. If you fail to do this, comet will throw an error.
this.record(name: string, description?: string): {
commit: (options?: object) => void,
append: (options?: object) => void,
cancel: (options?: object) => void,
}
const recording = this.record("Spawned Part");
// For example, lets spawn a part and have an error that randomly occurs.
const [s, e] = pcall(() => {
const part = new Instance("Part", Workspace);
if (math.random(1,3) === 3) error("A random error!")
});
// If we did not error, commit. If we did error, revert the creation of the part.
if (s) recording.commit();
else recording.cancel();
Selection
getSelection()
Return the current selection in studio.
this.getSelection(): Instance[]
select()
Select an instance or a group of instances.
select(obj?: Instance | Instance[]): void
onSelectionChanged()
Bind a callback that is invoked whenever the selection is changed.
onSelectionChanged(cb: (sel: Instance[]) => void);
DI
use()
Used to get a reference to another system.
It is not reccomended to use this method outside of onStart()
as failing to do so may cause a race condition.
use(system: ClassRef<System>): System;
Helpers
getJanitor()
Returns the janitor. You can add any events to this and they will be automatically cleaned up on plugin unload.
getJanitor(): Janitor;
Constants
plugin
- A read-only reference to the plugin global. Helpful for behavior unsuppored by comet.