Attachment System Challenge - KillCounter

Reaction score
333
JASS:
library KillCounter initializer Init
    globals
        private group G = CreateGroup()
    endglobals
    
    private function UnitDies takes nothing returns boolean
        local unit u = GetKillingUnit()
        
        if (u != null and u != GetTriggerUnit()) then
            if IsUnitInGroup(u, G) then
                call SetCSData(u, GetCSData(u)+1)
            else
                call GroupAddUnit(G, u)
                call SetCSData(u, 1)
            endif
        endif
        
        set u = null
        return false
    endfunction
    
    private function Init takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_DEATH)
        call TriggerAddCondition(t, Condition(function UnitDies))
    endfunction
endlibrary


Solution with CSData.
 

quraji

zap
Reaction score
144
Just whipped this up really quick, it's alot smaller than it looks...a big chunk of the code is just displaying messages :p

It's copy+paste-able so you can just paste it into your demo map, or use the one I uploaded.
Thanks to good ol' Gamecache:

edit - just read that unit's kills don't reset on death, I removed the check (in this code anyways, too lazy to reupload map)

JASS:
library KillCounter initializer initKillCounter

    globals
    gamecache KC_Cache  //your gamecache
    endglobals

    //init cache
    private function initKillCounter takes nothing returns nothing
        set KC_Cache = InitGameCache("KillCounter")
    endfunction


    private function H2I takes handle h returns integer
        return h
        return 0
    endfunction

    
    private function SetKills takes nothing returns nothing
        local unit killer = GetKillingUnit()
        local integer killid = H2I(killer)
        local string sid = I2S(killid)
        local integer kills = GetStoredInteger(KC_Cache, sid, "kills")        
        local integer pid = GetPlayerId(GetOwningPlayer(GetTriggerUnit()))
        local integer pid2 = GetPlayerId(GetOwningPlayer(killer))
        
        if pid == pid2 then
            call DisplayTimedTextToPlayer(Player(pid), 0., 0., 4., "You just killed one of your own units, you psycho!")
        else
            call DisplayTimedTextToPlayer(Player(pid2), 0., 0., 5., "You just iced one of Player " + I2S(pid) + "'s units!")
            call DisplayTimedTextToPlayer(Player(pid), 0., 0., 5., "One of your units was victim number " + I2S(kills+1) + "for one of Player " + I2S(pid2) + "'s units!")
        endif
        
        if kills + 1 > 1 then
            call DisplayTimedTextToPlayer(Player(pid2), 0., 0., 5., "The killing unit has " + I2S(kills+1) + " kills, the bastard!")
        else
            call DisplayTimedTextToPlayer(Player(pid2), 0., 0., 5., "The killing unit has only " + I2S(kills+1) + " kill, the pussy!")
        endif
            
        if kills == null then
            call StoreInteger(KC_Cache, sid, "kills", 1)
        else
            call StoreInteger(KC_Cache, sid, "kills", kills + 1)
        endif
                
        set killer = null
        
    endfunction

    function InitTrig_Killcounter takes nothing returns nothing
        local trigger t = CreateTrigger()
        local integer i = 0
    
        loop
            exitwhen i == GetPlayers()
        
            call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_DEATH, null)
            set i = i + 1
        
        endloop
    
        call TriggerAddAction(t, function SetKills)
    
        set t = null

    endfunction
    
endlibrary


Smaller version (without messages):
JASS:
library KillCounter initializer initKillCounter

    globals
    gamecache KC_Cache  //your gamecache
    endglobals

    //init cache
    private function initKillCounter takes nothing returns nothing
        set KC_Cache = InitGameCache("KillCounter")
    endfunction


    private function H2I takes handle h returns integer
        return h
        return 0
    endfunction

    
    private function SetKills takes nothing returns nothing
        local unit killer = GetKillingUnit()
        local integer killid = H2I(killer)
        local string sid = I2S(killid)
        local integer kills = GetStoredInteger(KC_Cache, sid, "kills")        
            
        if kills == null then
            call StoreInteger(KC_Cache, sid, "kills", 1)
        else
            call StoreInteger(KC_Cache, sid, "kills", kills + 1)
        endif
                
        set killer = null
        
    endfunction

    function InitTrig_Killcounter takes nothing returns nothing
        local trigger t = CreateTrigger()
        local integer i = 0
    
        loop
            exitwhen i == GetPlayers()
        
            call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_DEATH, null)
            set i = i + 1
        
        endloop
    
        call TriggerAddAction(t, function SetKills)
    
        set t = null

    endfunction
    
endlibrary

(Disclaimer: Too lazy to test it much..I just loaded in and killed a unit then I was satisfied xD)
(Warning: Anyone who dislikes this because it uses gamecache will get face palmed!!! :p)

P.S. to Cohadar - You're an ass :p
 

Cohadar

master of fugue
Reaction score
209
@Trollvottel
JASS:
    private function Action takes nothing returns nothing
    local unit die      = GetTriggerUnit()
    local unit kill     = GetKillingUnit()
    local integer id1   = U2I(die)
    local integer id2   = U2I(kill)
    local integer kills = GetStoredInteger(CACHE, "kills", I2S(id2))
    // thread dies after this line

when you call GetStoredInteger and nothing is in cache is fucks up current thread.

@quraji
same as for Trollvottel

@TheDamien - great job!
 

Flare

Stops copies me!
Reaction score
662
Triggeractions leak, whereas triggerconditions don't (it's not really that big of a deal when you're only doing it once per trigger, but I guess some people just do it as if it were normal to them :p)

EDIT: Wait. Triggeractions leak when destroying triggers, since they aren't destroyed correctly. In the case of these triggers, it's not really necessary

Also, how do you detect if a unit is removed? And not just killed! =S

I usually do
JASS:
if IsUnitInGroup (whichGroup, whichUnit) then //It could be (whichUnit, whichGroup), check my code on first page to see
call BJDebugMsg ("This isn't doesn't exist")
endif


after adding the units to one particular global group
 

AdamGriffith

You can change this now in User CP.
Reaction score
69
Oh right.
I want a go to see how epically I fail!
I have no idea what any of this means but I'll just use one of the test maps to find out :p

P.S.

I never understood the:
JASS:
function H2I takes handle h returns integer
return h
return 0
endfunction
 
Reaction score
333
That is the return bug. It tricks the game into returning the value of the handle as an integer.
 

Flare

Stops copies me!
Reaction score
662
I have no idea what any of this means but I'll just use one of the test maps to find out

It doesn't mean anything, other than giving Cohadar an oppurtunity to gloat about PUI -.-'

I never understood the:

It's a bug :p Normally, you wouldn't be able to return a value if it's not of the correct type, but any function with 'return' in it will skip everything after it, but the game still recognises the 'return 0' part, even though it's going to be ignored since because of 'return h'

I think :D
 

Cohadar

master of fugue
Reaction score
209
Then how come it works?
It doesn't.

It doesn't mean anything, other than giving Cohadar an oppurtunity to gloat about PUI -.-'
Like I said purely educational, I hope you people look at other people's code here.

=================================
Question for all:
How many unit groups you need to attach 10 different properties to a unit? (using the group method obviously)

EDIT:
I won't mind if someone makes a PUI version, in fact I would be glad to see if solution is obvious to all.
 

Flare

Stops copies me!
Reaction score
662
How many unit groups you need to attach 10 different properties to a unit? (using the group method obviously)

Obvious answer would be 10 or 1. Either have a group for each property, and check them all individually, for the associated data, or have everything in the same group and check it for all associated data in a loop (if you could loop through X different arrays)?

I tagged in AceHart for some tag-team ownage!!

Most awesome thing I've seen in this thread so far :D
EDIT: Seems I have to spread some rep around before you get rewarded for this act of awesome :p
 

quraji

zap
Reaction score
144
IQuestion for all:
How many unit groups you need to attach 10 different properties to a unit? (using the group method obviously)

You need 0 groups to attach 10 different properties to a unit xD

(Note: I don't know what "group method" is)

edit: A couple people have repped me for my "tag-team ownage" comment...I never knew being a dick could be so rewarding! I'll have to try more often :p
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      • Ghan
        Administrator - Servers are fun

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top