Wrda
Spell Reviewer
- Joined
- Nov 18, 2012
- Messages
- 2,010
A very simple script in lua that mimics BlzPauseUnitEx, with the ability to get the internal stun counter of a unit. (similar to the VJASS version [vJASS] - PauseUnitEx)
Lua:
--[[
PauseUnits v1.0
Lua version by Wrda
---------------------------------------------------------------
- Mimics BlzPauseUnitEx while providing an actual counter -
- which is able to be manipulated. -
---------------------------------------------------------------
API
PauseUnits.pauseUnit(u:unit, flag:boolean)
- Self-explanatory. Pausing the unit more than one time will result the counter rise up.
- Unpausing does the reverse.
PauseUnits.getUnitPauseCounter(u:unit)
- Returns the pause counter of the unit.
PauseUnits.setUnitPauseCounter(u:unit, new:integer)
- Sets the unit pause counter to the new desired value while calling BlzPauseUnitEx internally.
- O(n)
PauseUnits.isUnitPaused(u)
- Checks if the unit is paused.
]]
do
PauseUnits = {}
local paused = setmetatable({}, {__mode = "k",
__index = function(_, k) return 0 end}) -- default counter as 0 while first time accessing by user or the system.
---Pauses the unit, with an internal counter. Calls BlzPauseUnitEx internally.
---@param u u
---@param flag boolean
function PauseUnits.pauseUnit(u, flag)
if not paused[u] then paused[u] = 0 end
if flag then
paused[u] = paused[u] + 1
else
paused[u] = paused[u] - 1
end
BlzPauseUnitEx(u, flag)
end
---Gets the pause counter of the unit.
---@param u unit
---@return integer
function PauseUnits.getUnitPauseCounter(u)
return paused[u]
end
---Sets the pause counter of the unit to the new desired value. Calls BlzPauseUnitEx internally.
---O(n)
---@param u unit
---@param new integer
function PauseUnits.setUnitPauseCounter(u, new)
local sign = 0
local flag = false
local counter = paused[u]
if new > counter then
sign = 1
flag = true
elseif new < counter then
sign = -1
flag = false
end
while new ~= counter do
counter = counter + sign
BlzPauseUnitEx(u, flag)
end
paused[u] = counter
end
---Checks if the unit is paused.
---O(n)
---@param u unit
---@return boolean
function PauseUnits.isUnitPaused(u)
return paused[u] > 0
end
end