• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Snippet] GetUnitCount

Level 23
Joined
Apr 16, 2012
Messages
4,041
JASS:
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



  • 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

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


  • rewrote code to mag's version

 
Last edited:
Level 23
Joined
Apr 16, 2012
Messages
4,041
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
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
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
 
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.
 
For a proper GetUnitCount resource, this would be the code :p

JASS:
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 :/
 
Well, why not just do this???
JASS:
library GetUnitCount uses UnitIndexer initializer Init

    globals
        private integer count = 0
    endglobals
   
    private function onIndex takes nothing returns boolean
        set count = count + 1
        return false
    endfunction
   
    private function onDeindex takes nothing returns boolean
        set count = count - 1
        return false
    endfunction
   
    private function Init takes nothing returns nothing
        call RegisterUnitIndexEvent(Condition(function onIndex), UnitIndexer.INDEX)
        call RegisterUnitIndexEvent(Condition(function onDeindex), UnitIndexer.DEINDEX)
    endfunction

    function GetUnitCount takes nothing returns integer
        return count
    endfunction
    
endlibrary

Cause the struct will create an array which you are not using.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
you maybe missed the extends array

this only generates the following globals:

JASS:
constant integer si__GetUnitCount___UnitCount=6
integer s__GetUnitCount___UnitCount_count= 0

I actually went and compiled it to check myself
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
The struct has not to be private so then you can use UnitCount.count if you can't live with such syntax.

And it should even exist other ways to abuse vJass and make it much more like a textmacro of jass !

Seriouly, at least there is this useless constant, but i suppose a good script optimizer will get rid of this, such as wc3mapoptimizer.
But still, i would go for the proper way, just for the sake of sense.

Some times using structs even when you don't need instances is neat, just for the "." syntax, but here it doesn't improve anything.
 
Last edited:
Level 17
Joined
Apr 27, 2008
Messages
2,455
If JASS was more powerful, I'd opt for structs only as PODs and generic functions that operate on concepts like Boundable, Positionable, etc... (All on compile time so errors are not encountered on run-time.)

Only then could we alleviate the terrible design imposed by practically every vJASS resource on this site.

If jass was more powerfull, vJass would not even exist.
The point of vJass is not to make things faster to run, but faster/easier to write.

But here vJass is abused just for the sake of it, not even to win some pointless nano seconds.
Now, i'm not making some drama, just bored of all this shit. *shrugs*
 
Top