Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

(SOLVED) Awaiting signals: coroutines or bindables?

Asked by
Speedmask 661 Moderation Voter
3 years ago
Edited 3 years ago

hello everybody,

recently I attempted to create my own signal class. I noticed that other users have had two different approaches for creating Event:Wait(); either they create a bindable and fire it along with the signal like so

function Signal.new()
    local self = setmetatable({}, Signal)
    self._Yield = Instance.new(“BindableEvent”, script)
    return self
end

function Signal:Fire(...)
    self._Yield:Fire()
    -- run listeners
end

function Signal:Wait()
    self._Yield:Wait()
end

or they use coroutine.yield on the current thread and create a listener to resume when the signal fires

-- same things without the bindable

function Signal:Wait()
    local thread = coroutine.running()
    local connection = Connection.new(self) -- object used for indexing
    local function resume()
        connection:Disconnect()
        coroutine.resume(thread)
    end
    self[connection] = resume
    return coroutine.yield()
end

I went with coroutines because it was based on pure lua. I haven’t really found anything comparing the two. is one really better practice than the other?

thanks.

1
I recommend coroutine.yield since if you say you create your own class then it just fits more rather than creating class that calls methods of existing class. Roblox :Wait() calls the same coroutine.yield, however, Roblox somehow manages to make it quite performance inefficient, not like it's that noticeable but making your class is still better since you have 100% control over it. imKirda 4491 — 3y
0
interesting, I did not know that they used the exact same procedure; both options seemed a bit hacky to me, haha. I agree that there is no point in creating an instance to carry out the same effect. thanks. Speedmask 661 — 3y

1 answer

Log in to vote
0
Answered by
Speedmask 661 Moderation Voter
3 years ago

answered in comments

Ad

Answer this question