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.