I have 2 module scripts. In the first one, there is a setter function that sets a variable and fires a bindableEvent when it’s called.
function module.setter(inVariable)
variable = inVariable
bindableEvent:Fire()
end
In the second one, there is another function (which I’ll refer to as just function) that connects a heartbeat function to RunService.Heartbeat, then yields until bindableEvent fires. The yielding is achieved with bindableEvent.Event:Wait(). heartbeat does some calculations and calls setter when called.
local function heartbeat()
-- Do some calculations that result in foo
otherModule.setter(foo)
end
function module.function()
RunService.Heartbeat:Connect(heartbeat)
bindableEvent.Event:Wait()
end
I set these module scripts up like this because when heartbeat is called, variable needs to be available immediately after heartbeat finishes executing for the script that called heartbeat. The problem is that this leaves bindableEvent firing every frame while not doing anything. I feel like this is a problem, even if I’m overestimating how much resource bindable events take to fire. Is there a better way to stop a script from executing until a variable is available?
Maybe you can try connecting the bindable event to a function in your second module (as an example). So it is triggered whenever the variable is ready. Call module.init() once to initialize the connection.
function module.function()
-- Do some calculations that result in foo
otherModule.setter(foo)
end
function module.init()
bindableEvent.Event:Connect(function()
-- What to do after the variable is available
end)
end
variable is set based on the player’s mouse ray whenever they’re holding their mouse button down. When I tried having heartbeat fire based on mouse movement, I didn’t like how it wouldn’t update when the player rotated their camera, hence why I’m using heartbeat instead. Is there another option less resource intensive than heartbeat, but can update every single time the mouse ray changes, regardless of camera orientation?
I’m not seeing how this would reduce the number of times I fire bindableEvent using this method. In my head, all I’m doing is adding more operations to justify firing bindableEvent over and over again, when really, I want to stop firing bindableEvent after the first firing. Could you explain how this would do so?
Strange idea, but use signals to do it, you can bind function to signal and fire it when you set the variable, you can use open sourced signals or create your own, it’s simple, you have to store callback in a table and fire this table when required, you can also use OOP to make it more flexible
I think I’ll just steal stravant’s GoodSignal implementation instead. That is what I’m currently doing. If I successfully implement it, I’ll mark this as the correct answer.
I don’t want heartbeat to run once. I want bindableEvent to fire once.
I gave up trying to implement that and figured I could just run heartbeat once before connecting it to heartbeat. I continue adhering to DRY by just making heartbeat a local function.
local function heartbeat()
-- Make calculations
otherModule.setter(calculationResult)
end
heartbeat()
RunService.Heartbeat:Connect(heartbeat)