System PUI - Perfect Unit Indexing

Discussion in 'Systems and Snippets' started by Cohadar, Dec 1, 2007.

  1. Cohadar

    Cohadar master of fugue

    Ratings:
    +209 / 0 / -0
    Version 5.1
    Improved recycling function.
    Better statistics display "-puistats"
     
    • Like Like x 1
  2. Sooda

    Sooda Diversity enchants

    Ratings:
    +318 / 0 / -0
    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).
     
  3. SerraAvenger

    SerraAvenger Cuz I can

    Ratings:
    +236 / 0 / -0
    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?
     
  4. Cohadar

    Cohadar master of fugue

    Ratings:
    +209 / 0 / -0
    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.
     
  5. waaaks!

    waaaks! Zinctified

    Ratings:
    +256 / 0 / -0
    how to make use of this for spell making?
     
  6. Cohadar

    Cohadar master of fugue

    Ratings:
    +209 / 0 / -0
    There are example spells in the map I attached to the first post.
     
  7. waaaks!

    waaaks! Zinctified

    Ratings:
    +256 / 0 / -0
    ok
     
  8. cleeezzz

    cleeezzz The Undead Ranger.

    Ratings:
    +267 / 0 / -0
    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?)
     
  9. Builder Bob

    Builder Bob Live free or don't

    Ratings:
    +249 / 0 / -0
    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.
     
  10. cleeezzz

    cleeezzz The Undead Ranger.

    Ratings:
    +267 / 0 / -0
    oh i just name the structs different?

    Data
    and
    Info

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

    Cohadar master of fugue

    Ratings:
    +209 / 0 / -0
    Not if they are in different scopes or libraries.
    I name all my structs as Data (private mostly)
     
  12. cleeezzz

    cleeezzz The Undead Ranger.

    Ratings:
    +267 / 0 / -0
    sorry im a bit confused on which question you were answering lol, first or second?
     
  13. Builder Bob

    Builder Bob Live free or don't

    Ratings:
    +249 / 0 / -0
    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.



    no

    The structs must be private/public of course.
     
  14. Builder Bob

    Builder Bob Live free or don't

    Ratings:
    +249 / 0 / -0
    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.
     
  15. Cohadar

    Cohadar master of fugue

    Ratings:
    +209 / 0 / -0
    Yes the same principle would work for items.
    You just need to replace names.
     
  16. Builder Bob

    Builder Bob Live free or don't

    Ratings:
    +249 / 0 / -0
    Easy as that, eh?
    That's excellent! Thanks
     
  17. Zwiebelchen

    Zwiebelchen You can change this now in User CP.

    Ratings:
    +60 / 0 / -0
    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 ...
     
  18. Cohadar

    Cohadar master of fugue

    Ratings:
    +209 / 0 / -0
    It is enough to set it to zero.
    Why would you want to remove index manually anyways?
     
  19. Zwiebelchen

    Zwiebelchen You can change this now in User CP.

    Ratings:
    +60 / 0 / -0
    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.
     
  20. Azlier

    Azlier Old World Ghost

    Ratings:
    +461 / 0 / -0
    Well, I think you'll need over 8000 living units on the map for that to happen.
     

Share This Page