- Joined
- Apr 5, 2011
- Messages
- 245
There is an auto unit indexer in demo map below in attachments. I called it Very Simple Unit Indexer because it is very simple in my op. It's very short, check it please. (Look at trigger "vSUI" or just below) What really hurts me now it's using death event for unregister. (!) What kind of problems can it cause?
edit
vSUI -> vGUI.
vSUI code:
Temple:
edit
vSUI -> vGUI.
vSUI code:
JASS:
//======================
//=======[ vGUI ]=======
//======= v0.104 =======
library vGUI uses IndexedData, NExt, WorldBounds, optional Temple
//=== Settings ===
globals
private constant boolean INSTANT_INIT = true
private constant boolean REGISTER_START_UNITS = true
//----------------
private constant boolean DAMAGE_CONTROLLER = true
private constant integer REFRESH_COUNT = 15
//When <REFRESH_COUNT> units are registered damage trigger refreshes itself.
//----------------
private constant boolean TEST_MODE = true
endglobals
//================
globals
indexedUnit Unit
endglobals
struct vGDC
readonly static boolean enabled = false
private static trigger t
private static integer count = 0
method register takes unit u returns nothing
//Register damage event
call TriggerRegisterUnitEvent(damageController, u, EVENT_UNIT_DAMAGED)
//Count registered units to refresh damage trigger one fine day
set count = count + 1
if count == REFRESH_COUNT then
set count = 0
call refresh()
endif
endfunction
//=== Upon unit takes damage ===
private static method onDamage takes nothing returns boolean
local unit damaged = GetTriggerUnit()
local unit damager = GetEventDamageSource()
local real damage = GetEventDamage()
//Test
static if TEST_MODE then
call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Unit[" + I2S(Unit.indexOf(damaged)) + "] takes " + R2S(damage) + " damage from Unit[" + I2S(Unit.indexOf(damager)) + "]")
endif
//Send 1 templar
static if LIBRARY_Temple then
call onUnitTakesDamage(damaged, damager, damage)
endif
set damaged = null
set damager = null
return false
endmethod
endstruct
struct vGUI
readonly static boolean enabled = false
private static trigger enterTrigger
private static trigger deathTrigger
//=== Register filtered unit ===
private static method register takes nothing returns boolean
local unit u = GetFilterUnit()
call Unit.register(u)
//Test
static if TEST_MODE then
call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Unit[" + I2S(GetUnitUserData(u)) + "] has been registered.")
endif
//Register unit for damage controller
static if DAMAGE_CONTROLLER then
call vGDC.register(u)
endif
//Send 1 templar
static if LIBRARY_Temple then
call onUnitRegister(u)
endif
set u = null
return false
endmethod
//=== Unregister filtered unit ===
private static method unregister takes nothing returns boolean
local unit u = GetTriggerUnit()
call Unit.unregister(u)
//Test
static if TEST_MODE then
call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Unit[" + I2S(GetUnitUserData(u)) + "] has been unregistered.")
endif
//Send 1 templar
static if LIBRARY_Temple then
call onUnitUnregister(u)
endif
set u = null
return false
endmethod
//=== Register all units present at this moment ===
static method snapshot takes nothing returns nothing
local group g = CreateGroup()
call GroupEnumUnits(g, Condition(function thistype.register))
call DestroyGroup(g)
set g = null
endmethod
//=== Enable/disable dynamic register ===
static method turn takes boolean flag returns nothing
if flag then
call EnableTrigger(enterTrigger)
call EnableTrigger(deathTrigger)
set enabled = true
else
call DisableTrigger(enterTrigger)
call DisableTrigger(deathTrigger)
set enabled = false
endif
endmethod
//=== Refresh damage trigger ===
private static method refresh takes nothing returns nothing
local integer i = 0
//Clear triggers
call DestroyTrigger(damageController)
set damageController = CreateTrigger()
call TriggerAddCondition(damageController, Condition(function thistype.onDamage))
//Restore events
loop
exitwhen i == Unit.size
if Unit[i] != null then
call TriggerRegisterUnitEvent(damageController, Unit[i], EVENT_UNIT_DAMAGED)
endif
set i = i + 1
endloop
endmethod
//=== Enable/disable damage controller ===
static method turnDC takes boolean flag returns nothing
if flag then
call EnableTrigger(damageController)
set DC_enabled = true
else
call DisableTrigger(damageController)
set DC_enabled = false
endif
endmethod
endif
//=== Starts work with it ===
static method start takes nothing returns nothing
//Create all needed dynamic variables if it has not been done at struct init
static if not INSTANT_INIT then
set Unit = Unit.create()
set enterTrigger = CreateTrigger()
set deathTrigger = CreateTrigger()
static if DAMAGE_CONTROLLER then
set damageController = CreateTrigger()
endif
endif
//Register all present units if it is needed
static if REGISTER_START_UNITS then
call snapshot()
endif
//Enable dynamic register
call TriggerRegisterEnterRegion(enterTrigger, WorldBounds.worldRegion, Condition(function thistype.register))
set enabled = true
call TriggerAddCondition(deathTrigger, Condition(function thistype.unregister))
//Enable damage controller if it is needed
static if DAMAGE_CONTROLLER then
call TriggerAddCondition(damageController, Condition(function thistype.onDamage))
set damageControl = true
endif
endmethod
static method onInit takes nothing returns nothing
//Create all needed dynamic variables if vGUI init is set instant
static if INSTANT_INIT then
set Unit = Unit.create()
set enterTrigger = CreateTrigger()
set deathTrigger = CreateTrigger()
static if DAMAGE_CONTROLLER then
set damageController = CreateTrigger()
endif
endif
endmethod
endstruct
endlibrary
JASS:
//==============
//=== Temple ===
//==============
library Temple
function onUnitRegister takes unit u returns nothing
endfunction
function onUnitUnregister takes unit u returns nothing
endfunction
function onUnitTakesDamage takes unit damaged, unit damager, real damage returns nothing
endfunction
endlibrary
Attachments
Last edited: