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.
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.

      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