As a Roblox developer, it is currently impossible to activate a condition dependent on future events that might occur within the same event scheduling cycle.
I have two properties that pair together. Most of the time they are both updated together, but sometimes only one of them is updated. Whenever either of them is updated, I need to insert a new model into the game.
I want to wait until the end of the current scheduling cycle in order to check if both properties change, or if only one property changed. This allows me to make only one update to the model instead of two or more!
This used to be possible with the old coroutine.yield
behavior:
local object = OBJECT
local updating = false
local function update()
if updating then
return
end
updating = true
coroutine.yield() -- wait until end of current event scheduling cycle
updating = false
-- do stuff with object.Property1
-- do stuff with object.Property2
end
object:GetPropertyChangedSignal("Property1"):connect(update)
object:GetPropertyChangedSignal("Property2"):connect(update)
---
object.Property1 = "example" --update is queued to end of scheduling cycle
object.Property2 = "example-prop-2"
-- update runs before the cycle ends
coroutine.yield() -- wait until the end of the cycle, after update has ran
-- the model is now updated, do anything with model.
This is no longer possible. Now we either have to update on both Property1 and Property2 (bad performance), or update after a frame or step has passed (not instantaneous).
Scheduling explanation
Whenever Roblox runs scripts, it goes through a cycle of resuming scripts that need to be resumed. This is an event scheduling cycle. Generally, when waiting, you wait until some other cycle, so you wait a significant amount of time where the game may have updated, network may have sent, rendering might have changed, etc.
By running programming in the same cycle, you can assure that rendering, networking, physics, etc. is all in the same state, and that there is no delay between event and response from the player’s perspective.
A wait to the end of a cycle would be a very small wait and not be useful for waiting. It’s only useful for scheduling operations between multiple scripts that should “all occur at once” from the player’s perspective. As in the example, sometimes some programming should always run last, because it’s dealing with multiple changes. As it is now, we have no way to do that without adding a delay between event and response.
Also worth noting that I don’t know what this is actually called internally. I think Roblox Staff probably have an official name for it other than “event scheduling cycle”.
If Roblox is able to address my issue, it would allow me to write more efficient programming when dealing with multiple related events.