Great question! Yield Prioritization+ is a simple, light-weight and easy-to-implement module that is mainly used to structure functions on a priority list.
It’s pretty straight-forward and efficient for making stuffs like complex AI or any algorithm.
As seen in this image:
To explain briefly, you can either have multiple or one functions in a priority position; after a method is called, the program will then start from the first priority over to the very last priority position, which makes it easier to organize how you want to structure your yielding-code.
[1] Here I’ll show an example of a function where I’ve positioned the functions accordingly, three functions at priority one (1) that waits for 1s, and one function at priority three (3) that waits for 0.25s
In total, it should’ve yielded over 3.45s, but I’ve disable yielding on all accorded functions.
local YP = require(script.YieldPrioritization)
local new = YP.new()
local startTick = tick()
-- new:AccordPriorities(priority: number, function: any, willYield: boolean)
new:AccordPriorities(1, function()
print(tick() - startTick) -- 0.0003287792205810547
wait(1)
end, false) -- false, so the function is non-yielding
new:AccordPriorities(1, function()
print(tick() - startTick) -- 0.0004315376281738281
wait(1)
end, false)
new:AccordPriorities(1, function()
print(tick() - startTick) -- 0.0004928112030029297
wait(1)
end, false)
new:AccordPriorities(2, function()
print(tick() - startTick) -- 0.0005922317504882812
wait(0.25)
end, false)
new:StartPriorities() -- Start all of it at once
print(tick() - startTick) -- 0.0007202625274658203 (none of the functions yield)
[2] Now If I set these functions to yield instead, it will have very different results. It will take 3.45s, which is just as expected!
local YP = require(script.YieldPrioritization)
local new = YP.new()
local startTick = tick()
-- new:AccordPriorities(priority: number, function: any, willYield: boolean)
new:AccordPriorities(1, function()
print(tick() - startTick) -- 0.0002598762512207031
wait(1)
end, true) -- true, so the function will yield
new:AccordPriorities(1, function()
print(tick() - startTick) -- 1.191523551940918
wait(1)
end, true)
new:AccordPriorities(1, function()
print(tick() - startTick) -- 2.2076990604400635
wait(1)
end, true)
new:AccordPriorities(2, function()
print(tick() - startTick) -- 3.458249092102051
wait(0.25)
end, true)
new:StartPriorities()
print(tick() - startTick) -- 3.4585940837860107
The example below also displays how the order of priority works:
as expected, it prints “World Hello!” first, followed by “Hello World!” not too long after.
module:AccordPriorities(2, function()
print("Hello World!")
end, true)
module:AccordPriorities(1, function()
print("World Hello!")
end, true)
module:StartPriorities() -- World Hello! -- Hello World!
You can access the most up-to-date documentation over here in the GitHub link provided.
Contacts:
- Mememoxzine#9290 (discord)
Feedback is much appreciated! This has been my first module since the past 2 years!