1. A slave to two rhythms, the 22nd Terraining Contest is here.
    Dismiss Notice
  2. The heavens smile on the old faithful. The 16th Techtree Contest has begun.
    Dismiss Notice
  3. The die is cast - the 6th Melee Mapping Contest results have been announced. Onward to the Hive Cup!
    Dismiss Notice
  4. The glory of the 20th Icon Contest is yours for the taking!
    Dismiss Notice
  5. Shoot to thrill, play to kill. Sate your hunger with the 33rd Modeling Contest!
    Dismiss Notice
  6. Do you hear boss music? It's the 17th Mini Mapping Contest!
    Dismiss Notice
  7. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[Snippet] GetUnitCount

Discussion in 'JASS Resources' started by edo494, Jan 9, 2013.

  1. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,846
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    Code (vJASS):

    library GetUnitCount uses UnitIndexer/*
    by edo494 v1.2
       
        requires - UnitIndexer: http://www.hiveworkshop.com/forums/jass-resources-412/system-unit-indexer-172090/

        API:
           
            function GetUnitCount takes nothing returns integer
                - returns current number of indexed units on the map
           
    */

    private struct UnitCount extends array
        readonly static integer count = 0
       
        private static method onIndex takes nothing returns boolean
            set count = count + 1
            return false
        endmethod
       
        private static method onDeindex takes nothing returns boolean
            set count = count - 1
            return false
        endmethod
       
        private static method onInit takes nothing returns nothing
            call RegisterUnitIndexEvent(Condition(function thistype.onIndex), UnitIndexer.INDEX)
            call RegisterUnitIndexEvent(Condition(function thistype.onDeindex), UnitIndexer.DEINDEX)
        endmethod
    endstruct

    function GetUnitCount takes nothing returns integer
        return UnitCount.count
    endfunction
       
    endlibrary
     


    change notes

    1.1

    • added function GetUnitCountEx, takes:
      • boolean countLocust - whether or not we want to count locust units
      • boolean countDeadUnit - whether or not we want to count dead units
      • boolean countDeadHero - whether or not we want to count dead heroes

    1.2

    • remade whole code, now fires on INDEX/DEINDEX events

    1.3

    • rewrote code to mag's version

     
    Last edited: Oct 30, 2013
  2. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,146
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    Why not just make this really simple and use UnitIndexer?
     
  3. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,846
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    this would be redundant as is if the unitindexer had method/function to get the highest value(the biggest current index) but as I didnt find it, I had to make this for the GetKiller to loop enough times but not too many
     
  4. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,146
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    huh??

    then why don't you just use UnitList? It has a list of every unit on the map, lol


    this just wasn't written very well, if you want a count

    onIndex
    count + 1

    onDeindx
    count - 1

    : \

    if you want to handle dead/alive units, then use UnitEvent as well to detect revival and mag's event player unit thing to detect death
     
  5. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,846
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    remade code
     
  6. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,146
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    This will not count units ; )

    if GetUnitUserData(GetIndexedUnit()) > unitCount then


    is wrong

    it should just be

    +1 on increment
    -1 on decrement

    nothing else needed
     
  7. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,003
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    ^You're right.
     
  8. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,846
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    no, GetKiller wont work anymore, because it repeats loop for unitCount times, but if I just made enters: +1, leaves: -1, and I get lets say 20, when 2 units with indexes other than 20, 19 leaves, lets say unit at index 6 and 7 leaves, than it would return 18, but GetKiller needs to loop 20 times because the biggest index of unit is 20, so it would only run 18 times leaving unit [19] and unit [20] without checking
    I should change the name but cant find something simple yet normal
     
  9. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,146
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    use a linked list then
     
  10. Chaosy

    Chaosy

    Tutorial Reviewer

    Joined:
    Jun 9, 2011
    Messages:
    11,041
    Resources:
    18
    Icons:
    1
    Maps:
    1
    Spells:
    10
    Tutorials:
    6
    Resources:
    18
    Nice simple script you got there but rename the function to "GetUnitCount" it is more jass-ish :D
     
  11. Anachron

    Anachron

    Joined:
    Sep 9, 2007
    Messages:
    6,220
    Resources:
    66
    Icons:
    49
    Packs:
    2
    Tools:
    1
    Maps:
    3
    Spells:
    9
    Tutorials:
    1
    JASS:
    1
    Resources:
    66
    I don't see how this can work?
    Code (vJASS):

    if GetUnitUserData(GetIndexedUnit()) == unitCount then
     


    So if I remove unit with index 0 this will not trigger???
    It's as simple as Nes said it. Index = +1, Deindex = -1.
     
  12. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,003
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    edo, you're not supposed to make the code in this script be interconnected with the script in another resource.

    This is a bad software engineering practice.

    This should get the unit count on the map, and that's it, meaning that it will return accurate unit counts.
    In your other resource, you should use a linked list as Nestharus said. You could use a stack if you want too.
     
  13. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,146
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    A stack wouldn't work

    Honestly, he could just use UnitList...
     
  14. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,003
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    For a proper GetUnitCount resource, this would be the code :p

    Code (vJASS):
    private struct UnitCount extends array
        readonly static integer count = 0
       
        private static method onIndex takes nothing returns boolean
            set count = count + 1
            return false
        endmethod
       
        private static method onDeindex takes nothing returns boolean
            set count = count - 1
            return false
        endmethod
       
        private static method onInit takes nothing returns nothing
            call RegisterUnitIndexEvent(Condition(function thistype.onIndex), UnitIndexer.INDEX)
            call RegisterUnitIndexEvent(Condition(function thistype.onDeindex), UnitIndexer.DEINDEX)
        endmethod
    endstruct

    function GetUnitCount takes nothing returns integer
        return UnitCount.count
    endfunction


    This "GetKiller" resource would have to use its own internal stack or count.
    The semantics of this should be intuitive and global :/
     
  15. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,846
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    updated
     
  16. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,003
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
  17. Fingolfin

    Fingolfin

    Joined:
    Jan 11, 2009
    Messages:
    3,247
    Resources:
    154
    Models:
    144
    Icons:
    1
    Packs:
    4
    Skins:
    2
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    154
    Why is this a struct if you only have static methods and no instances? You might aswell just make it a scope.
     
  18. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,846
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    for the lolz
     
  19. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,003
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    A library is required here.

    The reason behind the presence of the struct is simply because of the preferred style of initialization with the onInit struct method. It really doesn't add or remove anything from the resource.
     
  20. Fingolfin

    Fingolfin

    Joined:
    Jan 11, 2009
    Messages:
    3,247
    Resources:
    154
    Models:
    144
    Icons:
    1
    Packs:
    4
    Skins:
    2
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    154
    Scopes can also have initializers........