System PUI - Perfect Unit Indexing

Sooda

Diversity enchants
Reaction score
318
5.1 seems good, so good that I have twice tried to use it and failed, someone has manged to make simple spells using PUI 5.1, because:
JASS:
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

part looks out of space to me :p

Especially line:
JASS:
        set UnitData[killer] = dat // and attach it

Operator? vJASS feature, interesting, very high tech. 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).
 

SerraAvenger

Cuz I can
Reaction score
234
JASS:
    //-----------------------------------------------------------------------
    //  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


I thought there was the possibility of an static onDestroy method here, which would replace this and enable .destroy() instead - or didn't I understand something there?
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.
 

Cohadar

master of fugue
Reaction score
209
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).

I am not sure what you are asking me to do here?
I can't fix blizzards problems.
create with arguments will work.
onDestroy will work, but you must not call .destroy directly, use .release instead. (or even better don't use anything, let PUI do it.)

If map does not load I have no idea why is that, I use older wc3 patch.
 

cleeezzz

The Undead Ranger.
Reaction score
268
i just used your system this morning and in your example struct code, you did local Data (something) = Data[Unit], then checked if Data = 0 (to check if struct is already created).

my question is, can you attach multiple structs onto a unit?

(if i had two different codes that followed your example struct, and first one runs, detects d = 0 and creates a struct and sets it as Data, then the 2nd trigger runs and detects that struct is already created (but lets say struct in trigger 2 was different) then wouldn't it screw up?)
 

Builder Bob

Live free or don't
Reaction score
249
let's say you have two structs named Data and Info.

Data[Unit] != Info[Unit]

GetUnitIndex(Unit) will return the same index in both cases. The difference between them is that they store and retrieve struct instances from separate arrays.

So you can say that all units can have one instance of each struct type attached to them.
 

cleeezzz

The Undead Ranger.
Reaction score
268
oh i just name the structs different?

Data
and
Info

?
but if i name them both Data, then it will mess up?
 

Cohadar

master of fugue
Reaction score
209
Not if they are in different scopes or libraries.
I name all my structs as Data (private mostly)
 

cleeezzz

The Undead Ranger.
Reaction score
268
sorry im a bit confused on which question you were answering lol, first or second?
 

Builder Bob

Live free or don't
Reaction score
249
sorry im a bit confused on which question you were answering lol, first or second?

oh i just name the structs different?

short answer: yes

long answer: you can reuse the same name as long as you make the structs private/public in different scopes/libraries. Private/public really just changes their names, so they won't be the same.

but if i name them both Data, then it will mess up?


no

The structs must be private/public of course.
 

Builder Bob

Live free or don't
Reaction score
249
I don't fully understand how the PUI recycler work, so I have to ask. Would the same principle that you use for PUI be equally suitable for items? I need something like a Perfect Item Indexing system.

I have another system in place to handle item indexes, but I just like the simplicity of PUI, so I thought I might as well ask.
 

Cohadar

master of fugue
Reaction score
209
Yes the same principle would work for items.
You just need to replace names.
 

Zwiebelchen

You can change this now in User CP.
Reaction score
60
What if I want to remove a unit index? Is it enough to set the UnitUserData of the unit to 0? Maybe you should write a RemoveUnitInde function ...
 

Cohadar

master of fugue
Reaction score
209
It is enough to set it to zero.
Why would you want to remove index manually anyways?
 

Zwiebelchen

You can change this now in User CP.
Reaction score
60
It is enough to set it to zero.
Why would you want to remove index manually anyways?

Hm, basicly I use the GetUnitIndex() on any unit that comes in range of 2000 of one of 8 player controlled heroes (required for the aggro system i use).
I know that when a unit dies, the index gets recycled ... however; I have a trigger that removes the unit from the aggro system when the unit leaves a certain distance to all player heroes (to "clean" the system up and avoid having too much units on the aggro system). This trigger would be perfect to also remove the unit index, since the index is only used for the aggro system at my map.

I didn't check the code of PUI completely (because I trust you coding skills :D). Is it a problem if there are hundrets of units indexed? I mean; are there loopcalls, handle creations, etc. that will increase the memory usage or processing power usage? My RPG has an enormous size (480x480) and thus also has a lot of creeps. I just don't want to use up all the 8190 handle slots so fast with loads of units indexed.
 

Azlier

Old World Ghost
Reaction score
461
Well, I think you'll need over 8000 living units on the map for that to happen.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top