Skip to content

Expression 2 Events

Vurv edited this page Apr 21, 2023 · 20 revisions

Expression 2 now has events that allow you to manage what parts of your code run when certain things happen, like a player typing in chat and when a player leaves the server.
They're analogous to gmod's hook system if you are familiar Garry's Mod Lua.

Why?

If you're here because of your code giving you warnings about certain runOn* functions being deprecated, this is for you.

The clk (trigger) system is the old way of handling "events" with runOn* and *clk() functions. It's clunky, baggage from Expression 1 and trying to behave like an actual gate, causes a lot of user errors and unoptimized code, and makes writing extensions that add events much more inconvenient. The biggest issue was that most issues on the discord related to E2 code were due to users misunderstanding how their code was running, since the trigger system re-runs the entire chip.

Events are simpler, more efficient, and easier to understand in terms of syntax, and since it isolates what code runs due to an event happening rather than the whole chip.

Syntax

The syntax is pretty simple. They are similar to functions in how you declare them, but missing the return value type, and using event instead of function. Note, however, that events are not functions. They are a compile time construct. You can't redeclare them or stop them, at least for now. Check a sentinel variable inside the event if you do not want to listen to it anymore.

event chat(Ply:entity, Said:string, Team:number) {
    print(Ply, " just said ", Said)
}

Owner = owner()
event keyPressed(Ply:entity, Key:string, Down:number, Bind:string) {
    if (Ply == Owner & !Down) {
        print("Owner released key " + Key)
    }
}

Additionally, if you just begin to type event and the name of the event, the editor will autocomplete the whole declaration for you!

Documentation

Currently there's no documentation for these events besides here (you can view the list of events below). The current idea is that their names, and now the parameters they autocomplete with should be self-explanatory enough to be used without any documentation for now, until the E2Helper is entirely overhauled in the future.

Timers

If you've come here looking for a replacement for interval(n), and timer(sn), unfortunately, there is no replacement as of yet.
The plan is to add functions similar to Javascript's setTimeout and setInterval functions that take functions as parameters and call them.
E2 currently does not have functions as objects, so this will be done at a later point, likely soon after #2555 is finished.

List of Events

Declaration Replacing
event tick() runOnTick, tickClk
event chat(Player:entity, Message:string, Team:number) runOnChat, chatClk
event keyPressed(Player:entity, Key:string, Down:number, Bind:string) runOnKeys, keyClk
event input(InputName:string) inputClk, inputClkName, Partially ~Input
event chipUsed(Player:entity) runOnUse, useClk
event removed(Resetting:number) runOnLast, last
event playerSpawn(Player:entity) runOnSpawn spawnClk
event playerDeath(Victim:entity, Inflictor:entity, Attacker:entity) runOnDeath, deathClk
event playerConnected(Player:entity) runOnPlayerConnect, playerConnectClk
event playerDisconnected(Player:entity) runOnPlayerDisconnect, playerDisconnectClk
event httpErrored(Error:string, Url:string) runOnHTTP, httpClk
event httpLoaded(Body:string, Size:number, Url:string) runOnHTTP, httpClk
event fileErrored(File:string, Error:number) runOnFile, fileClk
event fileLoaded(File:string, Data:string) runOnFile, fileClk
event playerLeftVehicle(Player:entity, Vehicle:entity) Nothing
event playerEnteredVehicle(Player:entity, Vehicle:entity) Nothing

Expression 2 ⚙️

Getting Started 🕊

Guides (In learning order) 🎓

Tools 🛠️

Click To Expand

Advanced

Beacon 💡

Control 🎛️

Data 💿

Detection 👀

Display 💻

Render 🖌

I/O 🔌

Physics 🚀

Utilities 🛠️

RFID 💳

Wireless 🛜

Gates 🚥

Click To Expand

TBD

Extras 🔭

Clone this wiki locally