• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Some dumb question... unit = unit[array]

Status
Not open for further replies.
Level 2
Joined
Aug 6, 2009
Messages
21
Hello there!

I'm kinda new to Jass.
Now i'm trying to do that when you create units they saves in array and then when one of the units kill enemy, trigger compare that unit with the array, and returns the unit index in array.

First trigger i've created, when unit's are remembered in array.

But check trigger fails...

JASS:
function Kill_Actions takes nothing returns nothing
local unit un
local unit unt
    set un = GetKillingUnit()
    set unt = u[1]

When i'm trying to test map, map just won't start... but when I delete
JASS:
set unt = u[1]
It starts normally...
So the question. What am I doing wrong, and what I've to do to make it work?

Thanks for response
 
Level 17
Joined
Sep 8, 2007
Messages
994
I suppose you mean this... (only capable for you if you use JNGP)

JASS:
globals
      constant integer MAX_HANDLES = 8191 //Set the size to whatever you need but don't go over 8191
      unit array SaveUnits [MAX_HANDLES] 
endglobals

function GetUnitIndex takes unit u returns integer
    local integer i = 1
    
    loop
       exitwhen i == MAX_HANDLES
       if (u == SaveUnits [i]) then
           return i
       endif
       set i = i + 1
    endloop

    return 0
endfunction

function SaveUnit takes unit u returns nothing
    local integer i = i

    loop 
        exitwhen i == MAX_HANDLES
        if (SaveUnits [i] == null) then
            set SaveUnits [i] = u
            return
        endif
        set i = i + 1
    endloop

    call BJDebugMsg("Too many handles saved!")
endfunction

For efficiency, it would be better to nullify the units from the indexing unless you need the unit's corpse.

JASS:
function Actions takes nothing returns nothing
    set SaveUnits [GetUnitIndex(GetTriggerUnit())] = null
endfunction

function InitTrig_SomeTrig takes nothing returns nothing
    local trigger tri = CreateTrigger()
   
    call TriggerRegisterAnyUnitEventBJ(tri, EVENT_PLAYER_UNIT_DEATH)
    call TriggerAddAction(tri, function Actions)
endfunction
 
Level 2
Joined
Aug 6, 2009
Messages
21
Thank you, but that's not exactly what i need.

Really, i thought that [array] are set as 8191 by default...

Hm... that trigger really gives the max index filled by units... But i need the exactly index of killing unit...
but that is only for me... I really don't understand the code... =(

JASS:
globals
constant integer max_units = 1000
unit array u[max_units]
integer count = 0 // when unit spawns, this makes count = count + 1
endglobals


function Kill_Actions takes nothing returns integer
local integer a = 1
local unit ub
    set ub = GetKillingUnit()
    
    loop
        exitwhen a == count
        if(ub == u[a]) then
            return a
        endif
        set a = a + 1
    endloop
    call DisplayTextToPlayer(GetLocalPlayer(),0,0,"killing unit is at index : "+I2S(a))
        return 0
endfunction

/////////

function InitTrig_Kill takes nothing returns nothing
local trigger t
    set t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_DEATH )
    call TriggerAddAction( t, function Kill_Actions )
endfunction

I try to do this myself, and then compare with that you've post, if you do that.
 
Last edited:
Status
Not open for further replies.
Top