Created
November 7, 2017 06:57
-
-
Save Anaminus/ce008d0690c0df867966771b05adac3f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --[[ | |
| Cond | |
| Block threads until a condition is met. Reusable. | |
| ]] | |
| local function pack(...) | |
| return {n = select('#', ...), ...} | |
| end | |
| local mtCond = {__index={}} | |
| function mtCond.__index:Fire(...) | |
| local id = self.nextID | |
| self.nextID = self.nextID + 1 | |
| self.res[id] = {self.threads, pack(...)} | |
| self.threads = 0 | |
| self.event:Fire(id) | |
| end | |
| function mtCond.__index:Wait() | |
| self.threads = self.threads + 1 | |
| local id = self.event.Event:Wait() | |
| local res = self.res[id] | |
| res[1] = res[1] - 1 | |
| if res[1] <= 0 then | |
| self.res[id] = nil | |
| end | |
| return unpack(res[2], 1, res[2].n) | |
| end | |
| local function Cond() | |
| return setmetatable({ | |
| res = {}, | |
| nextID = 0, | |
| threads = 0, | |
| event = Instance.new("BindableEvent"), | |
| }, mtCond) | |
| end | |
| return Cond |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment