1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. The Lich King demands your service! We've reached the 19th edition of the Icon Contest. Come along and make some chilling servants for the one true king.
    Dismiss Notice
  4. The 4th SFX Contest has started. Be sure to participate and have a fun factor in it.
    Dismiss Notice
  5. The poll for the 21st Terraining Contest is LIVE. Be sure to check out the entries and vote for one.
    Dismiss Notice
  6. The results are out! Check them out.
    Dismiss Notice
  7. Don’t forget to sign up for the Hive Cup. There’s a 555 EUR prize pool. Sign up now!
    Dismiss Notice
  8. The Hive Workshop Cup contest results have been announced! See the maps that'll be featured in the Hive Workshop Cup tournament!
    Dismiss Notice
  9. 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,852
    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,149
    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,852
    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,149
    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,852
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    remade code
     
  6. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,149
    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,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    ^You're right.
     
  8. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,852
    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,149
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    use a linked list then
     
  10. Chaosy

    Chaosy

    Joined:
    Jun 9, 2011
    Messages:
    10,611
    Resources:
    18
    Maps:
    1
    Spells:
    11
    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,167
    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,006
    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,149
    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,006
    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,852
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    updated
     
  16. Magtheridon96

    Magtheridon96

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

    Fingolfin

    Joined:
    Jan 11, 2009
    Messages:
    3,191
    Resources:
    153
    Models:
    143
    Icons:
    1
    Packs:
    4
    Skins:
    2
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    153
    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,852
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    for the lolz
     
  19. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    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,191
    Resources:
    153
    Models:
    143
    Icons:
    1
    Packs:
    4
    Skins:
    2
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    153
    Scopes can also have initializers........