Cohadar
master of fugue
- Reaction score
- 209
Version 5.1
Improved recycling function.
Better statistics display "-puistats"
Improved recycling function.
Better statistics display "-puistats"
scope StructExample initializer Init
//===========================================================================
// If we want struct to be attachable to units we run PUI macro inside it.
// Putting 5-6 unit properties inside struct is more efficient
// than declaring 5 PUI_PROPERTY each for itself
//===========================================================================
private struct UnitData
//! runtextmacro PUI()
integer KillCounter = 0
// you can add any of your own stuff here
endstruct
//===========================================================================
private function Actions takes nothing returns nothing
local unit killer = GetKillingUnit()
local UnitData dat = UnitData[killer]
// is struct attached?
if dat == 0 then
set dat = UnitData.create() // if not create it
set UnitData[killer] = dat // and attach it
endif
// again it is simple
set dat.KillCounter = dat.KillCounter + 1
// do some other stuff with your struct...
endfunction
//===========================================================================
// WARNING: never destroy PUI structs directly use release() method instead
// In fact it is best that you never destroy them manually,
// they will be destroyed automatically when unit is removed from game.
//===========================================================================
//===========================================================================
private function Conditions takes nothing returns boolean
return true
endfunction
//===========================================================================
private function Init takes nothing returns nothing
local trigger trig = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ( trig, EVENT_PLAYER_UNIT_DEATH )
call TriggerAddCondition( trig, Condition( function Conditions ) )
call TriggerAddAction( trig, function Actions )
endfunction
endscope
set UnitData[killer] = dat // and attach it
//! runtextmacro PUI()
//-----------------------------------------------------------------------
// If you do not call release struct will be destroyed when unit handle gets recycled
//-----------------------------------------------------------------------
method release takes nothing returns nothing
local integer pui= .pui_id[integer(this)]
call .destroy()
set .pui_unit[pui] = null
set .pui_data[pui] = 0
endmethod
Jasshelpermanual said:The onDestroy method
There is no actual syntax for destructors, but there is a rule and it is that if the struct has a method called onDestroy, it is always automatically called when .destroy() is issued on an instance.
It is useful to have onDestroy when an instance of the type may hold things that have to be correctly cleaned, it saves time and even makes things safer.
struct sta
real a
real b
endstruct
struct stb
sta H=0
sta K=0
method onDestroy takes nothing returns nothing
if (H!=0) then
call sta.destroy(H)
endif
if (K!=0) then
call sta.destroy(K)
endif
endmethod
endstruct
In the above example, it is only needed to destroy the object of type stb and it would automatically destroy the attached objects of type sta if present.
I tried to add static method 'create' which took arguments and also there were 'onDestroy' method. With new PUI 5.1 hacks implemented
JASS://! runtextmacro PUI()
my map simply didn't loaded, after hitting 'start map' button in game (Because with JNGP and latest Blizzard patch there are small complications which drive me mad).
sorry im a bit confused on which question you were answering lol, first or second?
oh i just name the structs different?
but if i name them both Data, then it will mess up?
Yes the same principle would work for items.
You just need to replace names.
It is enough to set it to zero.
Why would you want to remove index manually anyways?