- Joined
- Sep 6, 2013
- Messages
- 6,742
To loop over abilities a unit currenly has.
JASS:
library UnitAbilityListDemo uses UnitAbilityList, CheatsJ
private struct Demo extends array
private static method onSelect takes nothing returns nothing
local integer currentIndex = 0
local string objedtIdString
local unit u = GetTriggerUnit()
call UnitAbilityList.Get(u)
call ClearTextMessages()
call BJDebugMsg("|c00FFD700" + GetUnitName(u) + "|r currently has:" )
loop
exitwhen ( currentIndex > UnitAbilityList.MaxIndex )
set objedtIdString = ObjectId2String(UnitAbilityList.AbilityId[currentIndex])
call BJDebugMsg("- " + GetObjectName(UnitAbilityList.AbilityId[currentIndex]) + " ('" + objedtIdString + "')" )
set currentIndex = currentIndex + 1
endloop
set u = null
endmethod
public static method onAbilityUnitListEnabled takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterPlayerUnitEvent(t, GetLocalPlayer(), EVENT_PLAYER_UNIT_SELECTED, null)
call TriggerAddAction(t, function thistype.onSelect)
call BJDebugMsg("|c00FFD700UnitAbilityList|r Registered " + I2S(UnitAbilityList.MaxIndex_Total + 1) + " abilities\n
Select a unit to list its abilities")
endmethod
private static method onInit takes nothing returns nothing
call UnitAbilityList.register(function thistype.onAbilityUnitListEnabled)
endmethod
endstruct
endlibrary
JASS:
library UnitAbilityList uses ObjectIdLoop
struct UnitAbilityList extends array
readonly static integer array AbilityId
readonly static integer array AbilityListElements
readonly static integer AbilityListMaxIndex
readonly static integer AbilityCount
private static boolean enabled = false
private static timer clock = CreateTimer()
private static constant real TIMEOUT = 0.0
private static trigger eventHandler = CreateTrigger()
private static integer currentIndex
private static integer MaxIndex
private static unit Dummy = null
public static method Get takes unit u returns nothing
local integer currentIndex
if ( not enabled ) then
debug call BJDebugMsg("|c00FFD700" + SCOPE_PREFIX + "|r System is not enabled, yet")
return
endif
set AbilityCount = 0
set currentIndex = 0
loop
exitwhen ( currentIndex > AbilityListMaxIndex )
if ( GetUnitAbilityLevel(u, AbilityListElements[currentIndex]) > 0 ) then
set AbilityId[AbilityCount] = AbilityListElements[currentIndex]
set AbilityCount = AbilityCount + 1
endif
set currentIndex = currentIndex + 1
endloop
endmethod
public static method register takes code c returns nothing
call TriggerAddCondition(eventHandler, Condition(c))
endmethod
private static method CleanUp takes nothing returns nothing
call PauseTimer(clock)
call DestroyTimer(clock)
call RemoveUnit(Dummy)
call DestroyTrigger(eventHandler)
set eventHandler = null
set clock = null
set Dummy = null
endmethod
private static method FireEvents takes nothing returns nothing
set enabled = true
call TriggerEvaluate(eventHandler)
endmethod
private static method VarifyAbiliy takes integer abilitycode returns boolean
return UnitAddAbility(Dummy, abilitycode) or UnitRemoveAbility(Dummy, abilitycode)
endmethod
private static method AddAbilityToList takes integer abilitycode returns nothing
set AbilityListElements[AbilityListMaxIndex] = abilitycode
set AbilityListMaxIndex = AbilityListMaxIndex + 1
endmethod
private static method VarifyAbilities takes nothing returns nothing
local integer stopIndex = currentIndex + 2000
loop
exitwhen ( currentIndex > MaxIndex or currentIndex > stopIndex)
call AddAbilityToList(ObjectIdLoop.ObjectId[currentIndex])
set currentIndex = currentIndex + 1
endloop
if ( currentIndex > MaxIndex ) then
call FireEvents()
call CleanUp()
endif
endmethod
private static method onObjectLoopFinish takes nothing returns nothing
set currentIndex = 0
set AbilityListMaxIndex = 0
set MaxIndex = ObjectIdLoop.MaxIndex
call TimerStart(clock, TIMEOUT, true, function thistype.VarifyAbilities)
endmethod
private static method onInit takes nothing returns nothing
set Dummy = CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE), 'Hpal', 0, 0, 0)
call UnitAddAbility(Dummy, 'Aloc')
call ShowUnit(Dummy,false)
call ObjectIdLoop.register(function thistype.onObjectLoopFinish)
endmethod
endstruct
endlibrary
JASS:
library ObjectIdLoop uses NextPrevObjectId // https://www.hiveworkshop.com/threads/nextprev-objectid.313328/
struct ObjectIdLoop extends array
readonly static integer array ObjectId
readonly static integer MaxIndex = -1
private static constant integer RECURSION_COUNTER_MAX = 20000
private static constant real THREAD_TIMEOUT = 0.00
private static integer RunCounter = 0
private static integer FirstObjectId
private static integer LastObjectId
private static integer recursionCounter
private static integer currentObjectId
private static timer clock = CreateTimer()
private static trigger eventHandler = CreateTrigger()
private static method StartNextRun takes integer first, integer last returns nothing
set FirstObjectId = first
set LastObjectId = last
set RunCounter = RunCounter + 1
set recursionCounter = 0
set currentObjectId = first
call PauseTimer(clock)
call TimerStart(clock, THREAD_TIMEOUT, true, function thistype.fillArray)
endmethod
private static method CleanUp takes nothing returns nothing
call PauseTimer(clock)
call DestroyTimer(clock)
call DestroyTrigger(eventHandler)
set clock = null
set eventHandler = null
endmethod
private static method FireEvent takes nothing returns nothing
call TriggerEvaluate(eventHandler)
endmethod
private static method fillArray takes nothing returns nothing
local string objectName
loop
exitwhen ( recursionCounter > RECURSION_COUNTER_MAX or currentObjectId > LastObjectId )
set objectName = GetObjectName(currentObjectId)
if ( objectName != "Default string" and objectName != null ) then
set MaxIndex = MaxIndex + 1
set ObjectId[MaxIndex] = currentObjectId
endif
set currentObjectId = GetNextObjectId(currentObjectId)
set recursionCounter = recursionCounter + 1
endloop
set recursionCounter = 0
if ( currentObjectId > LastObjectId ) then
if ( RunCounter == 1 ) then
call StartNextRun('S000', 'Sczz')
else
call FireEvent()
call CleanUp()
endif
endif
endmethod
private static method onInit takes nothing returns nothing
call StartNextRun('A000', 'Azzz')
endmethod
public static method register takes code c returns nothing
call TriggerAddCondition(eventHandler, Condition(c))
endmethod
endstruct
endlibrary
Attachments
Last edited: