Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

(SOLVED) Awaiting signals: coroutines or bindables?

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.

2 comments

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.

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.

1 answer

This discussion is preserved as it was. New answers, comments, and votes are closed.