Event:connect(func, [priority])

I’d like something like BindToRenderStep’s priority parameter. Listeners for some event would be called in descending order of the priority integer they were bound with when that event fires. If multiple listeners are bound with the same priority then they would be called in the order that they were bounded in. The priority parameter would be optional and default to 0.

9 Likes

YES YES YES YES YES YES YES!

please let this be a thing.

Use case?

6 Likes

RBXScriptSignal used to have connectFirst and connectLast members but they were deprecated. In general if you want something to run before/after certain listeners you should make that explicit (e.g. a listener fires a BindableEvent after it is done processing)

2 Likes

This wouldn’t allow us to do anything new functionally, since we could have one master listener that calls all the other listeners in the desired order. Or we could bind all the listeners in order from a single script. However, this would allow us to separate bindings across multiple scripts.

Great, but what are you going to do with that, which you couldn’t do otherwise?

2 Likes

I would rather just have Connect and Disconnect

Separate bindings across multiple scripts where the order of the listeners matters.

For example, I have an in-game shop that needs to do a lot of things on PlayerAdded. First it needs to set up the player’s wallet and inventories. Then it needs to load all of the purchases the player has made. When loading the purchases, the wallet and inventories need to be set up or else it will fail.

Like I said, this could already be done, but the new priority parameter could allow us to do these kinds of things in a different way that a lot of developers would find more intuitive and readable.

1 Like

Sounds like a module script is in order. If modules are out of the question, bindables can be used. The point is that one function activates after another, right? So include code that makes those functions execute in the proper order: Either have a function call separate sub-functions in order, or use tail calls to call the next function.

If I found “PlayerAdded:connect(func, 3)” in a script I’d have no clue how to find 2 or 1 easily. I could search for PlayerAdded, but then I’d have to look through all of the connections to that event in the place. Depending on the event, I may have to look through tons of those (Heartbeat/RenderStepped come to mind). If I found “waitForPlayerAdded2BindableFunction()” it would be significantly easier for me to find where PlayerAdded2 was because I could just Ctrl+F for PlayerAdded2BindableFunction.

3 Likes

If the order of the listeners matter, your code is not decoupled. You can set up custom events like PlayerInventoryLoaded so anything which relies on such information being loaded can listen to that event, or you can have methods which yield until the inventory is loaded. Either of those approaches is better than specifying the order in which listeners are fired.

Also, this proposal wouldn’t help you in the case where one of the earlier listeners needs to yield. All listeners are fired in the same frame so later listeners would be fired before any asynchronous operations like loading data from a DataStore could occur.

So basically adding a priority wouldn’t even solve your problem even if it was implemented.

6 Likes