-
Notifications
You must be signed in to change notification settings - Fork 333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add event
s to Expression 2
#2452
Conversation
Todo: * Fix global scope variables (staying default despite being assigned to) * Implement event arguments * Editor highlighting
* Replaced yeet with ``event`` * Network events to client * Editor highlighting * Add fileLoaded and fileErrored events
Might remove the http/file ones later, for the same reason im not implementing timers, their apis would be much better suited with a function that takes a lambda / function object |
Is it possible to assign multiple event handlers to a single event? It would be useful for libraries. |
Not right now, because I can't think of a syntax that would look decent for it |
I'm thinking of an alternative though, where you could have one event handler per file rather than per chip, so libraries would be able to handle events, it wouldn't require a new syntax for the name of the handler, and could still allow for removing the event handler in the future |
this seems like it's going to be seriously inconvenient to account for in older projects. is there no way to integrate events without rendering the existing tickClk(), chatClk(), etc functions obsolete? |
Rendering them obsolete doesn't mean it would remove them. It would just mark them deprecated and have old E2s give off hundreds of warnings. Functions that have been deprecated for over a decade still exist (case in point,
Me and @Derpius already tried to make successors to E2, we came to the conclusion that it'd be way too time consuming for a gmod project, and hardly anyone would use it (they're accustomed to either E2 or StarfallEx). You can find our attempts here: Expressive and MLang. @AbigailBuccaneer brought up discussion on making an E3, but they went MIA as well. Additionally a lot of people on the discord brought up the exact opposite of your argument, that it'd be better to just extend E2 rather than working on a new one. Since you wouldn't need to learn new syntax and every server already has it. (Also, for me, it's much easier to extend E2 than to work on a new project)
Once again this wouldn't be a breaking change. And by your argument of a "casual" language, compare these two code blocks, which one is easier to understand? @persist Var
if (chatClk()) {
print(lastSaid(), Var)
} elseif(keyClk(owner()) == -1) {
print("Key released")
} elseif (first()) {
print("First")
Var = 5
} @persist Var
print("First")
Var = 5
event chat(Ply:entity, Said:string, Team:number) {
print(Said, Var)
}
event keyPressed(Ply:entity, Key:string, Down:number, Bind:number) {
if (!Down) {
print("Key released")
}
} |
Triggers are also probably the most common things people get confused about in E2 on the discord, having this system would lead to less pointless questions and having to explain why E2 has the most complicated event system (when it could simply be replaced with a better one). And this would stop polluting the e2helper/globals with clk* functions |
Just need to implement the last few clk* functions as events and separate them by file |
Code is much less complicated now. Checks if you're the author of the message before letting you set the variable at all. And now it works with the event system.
* Added playerSpawn, playerDeath, playerConnected, playerDisconnected, chipUsed events * Color code autocompletions by their type. * Add keyword autocompletion * Add constructor/destructor system so that events can create and remove hooks as necessary per chip.
This is ready, would appreciate testing and feedback, especially on the bonus autocomplete stuff, and using this for libraries (multiple event handlers across files). |
I'm having trouble with the My scenario is that I want to run code only exactly when the number of players on the server changes, without needing a timer constantly running to retrieve a value from |
As the name suggests, they're event listeners, not functions. See the examples on the pr and on the wiki. All events are direct replacements of current runOn* functions and the parameters correspond to their clk() functions that return different arguments of the event, for example lastSaid() is passed as a string to the So you can think of events as basically this, but at compile time and much more efficient.
|
Thanks so much for taking the time to respond, @Vurv78. I was confused at first about how to actually use the new event listeners syntactically in my E2. Now I see it's much like creating an E2 function. So, the following code now gives me the intended result, and much more elegantly than any method involving the
Thanks again for helping me out, and for continuing to support and improve E2! |
Is it intentional that you can't |
Yes, for two reasons
Right now you can use the |
This PR adds events to E2. The hope is these will make the current confusing/limited/awful
clk
system obsolete.Event List
Pretty small right now, but eventually it should be 1:1 with the current clk setups.
tick()
fileErrored(sn)
&fileLoaded(ss)
input(s)
keyPressed(esns)
chat(ssn)
httpErrored(ss)
&httpLoaded(sns)
playerDeath(eee)
playerSpawn(e)
playerConnected(e)
playerDisconnected(e)
removed(n)
removed(n)
chipUsed(e)
Lua Api
These events can be called on every E2 chip with
E2Lib.triggerEvent(name: string, args: any[])
. There is a new method on the E2ENT
, beingENT:ExecuteEvent(name: string, args: any[])
(this is what triggerEvent calls on every single chip subscribed)To register an event, use
E2Lib.registerEvent(name: string, args: string[])
, for exampleThere are constructors and destructors for handling hook creation/deletion.
Potential Todos
This is practically done, but there's some QoL stuff that could be done.
Deprecation messages
This also shows you messages if any are provided for deprecation notices.
Nicer for dev so you don't need to jump between the function and the e2helper.
This is kind of unrelated to the PR, but I made the changes to avoid a ton of messages asking why all of the most common runOn* are deprecated now..
Extra autocompletion
There's now autocompletion for keywords and events. Autocompletion entries are now colored for their type (constant, function, variable, event, keyword).