1.0.3 • Published 2 years ago

@rbxts/events v1.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

A simple but reliable signal-based module that doesn't require BindableEvents, more to come but simple utilities that make it powerful!

Installation

npm i @rbxts/events

Guide

Simple guide on how to use it and what are the advantages when using this package.

Usage

When it comes to signal-based programming, we often need to communicate between different scripts on our game, we can either use BindableEvents or some hacky methods like Values, this isn't good at all, that's why these type of modules are created. Provide a customisable interface and reliability when working with it, and adding to that the power of roblox-ts, the advantages start to show themselves.

For this "tutorial" we will be working with a structure like this:

- src
    - events.ts
    - script.server.ts

First on the events.ts file, we will need to import the module, then define an interface such as event: callback and at the end, construct the new object from the Event class using the interface.

import { Event } from "@rbxts/events"

interface EventsDescription {
    /* Both ways are okay */
    increment(v: number): void
    decrease: () => void;

    returnSum: (a: number, b: number) => number;
}

export const Events = new Event<EventsDescription>()

It is RECOMMENDED that the object construction it is global for one context, but this can be done in different files as well. Then we could require this module from the script.server.ts file, and we will end with something like this:

import Events from "...";

function connectEvents() {
    Events.listen("increment", (variable: number) => {
        print(`Old count: ${variable} | New count: ${variable + 1}`)
    });

    Events.listen("returnSum", (a: number, b: number) => {
        return a + b;
    });
}

function fireEvents() {
    let v = 0;
    Events.fire("increment", v); // 0, 1

    const [sum] = Events.fire("returnSum", 5, 61);
    print(sum); // 66
}

connectEvents();
fireEvents();

And whoala, the magic happened!

The limit is your own skills (and you can always get better, as I'm trying to do)! Here you have the source code so you can get some reference from it: https://github.com/siriuslatte/rbxts-events

This package will hold support as long as I wish to do, I'll update it and notify in the discord server! When there's major changes or a feature addition, those will be notified as well.