Добавлен LordDracula
Я попытался создать библиотеку, которая даёт специальный ид юниту по его хэндлу, но ничего не работает, хотя ошибок я не замечаю. В чем проблема?
library UnitSpecialId
private integer CountedUnits
private integer array HandleIdOfUnit
private integer array SpecialId
private function CheckUnitId takes unit u returns boolean
call BJDebugMsg("2")
local integer i = GetHandleId(u)
local integer index = 1
local boolean b = false
loop
if (i==HandleIdOfUnit[index]) then
set b = true
set index = CountedUnits
endif
call BJDebugMsg(I2S(index)+" - index")
exitwhen (index >= CountedUnits)
set index = index+1
endloop
call BJDebugMsg("3")
return b
endfunction
public function GiveUnitId takes unit u returns nothing
call BJDebugMsg("1")
if CheckUnitId(u) then
return
endif
call BJDebugMsg("4")
set CountedUnits = CountedUnits+1
set HandleIdOfUnit[CountedUnits] = GetHandleId(u)
set SpecialId[CountedUnits] = CountedUnits
endfunction
public function GetUnitId takes unit u returns integer
local integer i = GetHandleId(u)
local integer index = 1
local integer id = 0
loop
if (i==HandleIdOfUnit[index]) then
set id = SpecialId[index]
set index = CountedUnits
endif
exitwhen (index >= CountedUnits)
set index = index+1
endloop
return id
endfunction
endlibrary
library test initializer testfunc
function testfunc takes nothing returns nothing
call BJDebugMsg("start")
local unit u = CreateUnit(Player(0),'h000',0,0,0)
call UnitSpecialId_GiveUnitId(u)
call BJDebugMsg(I2S(UnitSpecialId_GetUnitId(u)))
call BJDebugMsg("end")
endfunction
endlibrary
После того, как выводит переменную index(там 1) в функции CheckUnitId - перестаёт работать. 3 не выводится в чат.
Принятый ответ
пустые integer по дефолту равны
тому, что было в памяти на момент её выделения под переменную. Выделение памяти и её очистка - разные процессы, именно поэтому считается хорошим тоном задавать значения перед тем, как планируешь их использовать.
Чтобы оставить комментарий, пожалуйста, войдите на сайт.
Отредактирован ledoed