Boolean does not listen?

wraithseeker

Tired.
Reaction score
122
JASS:
scope Necromancy initializer Init

globals
    private constant integer SPELL = 'A00J'
    private constant integer HEROID = 'U000'
    private integer Count = 0
    private timer TIMER = CreateTimer()
    private constant real TIMEOUT = 1
    private group Casters = CreateGroup()
    private boolexpr b
    private boolexpr unitcheck
endglobals

private struct data
    implement AutoData
    static data temp
    unit target
    boolean used
    timer t
    
    
method onDestroy takes nothing returns nothing
    
endmethod
endstruct

private function CheckUnits takes nothing returns boolean
    local data d
    set d = d.temp
    return GetOwningPlayer(GetFilterUnit()) == GetOwningPlayer(d.target) and not IsUnitType(GetFilterUnit(),UNIT_TYPE_HERO)
endfunction

private function Check takes nothing returns boolean
    return GetUnitTypeId(GetFilterUnit()) == HEROID
endfunction

private function Conditions takes nothing returns boolean
    return GetLearnedSkill() == SPELL
endfunction

private function Remove takes nothing returns nothing
    local data d = GetTimerData(GetExpiredTimer())
    set Damage[d.target] = Damage[d.target] - 5
    set MaxMana[d.target] = MaxMana[d.target] - 50
    call ReleaseTimer(d.t)
    call BJDebugMsg("released")
    set d.used = false
endfunction

private function DoEffect takes nothing returns nothing
    local unit u = GetEnumUnit()
    local unit t
    local data d = data<u>
    local data c
    set d.temp = d
    call GroupEnumUnitsInRange(ENUM_GROUP,GetUnitX(u),GetUnitY(u),600,unitcheck)
    loop
        set t = FirstOfGroup(ENUM_GROUP)
        exitwhen t == null
        set c = data[t]
        if c == 0 then
            set c = data.create()
            set c.used = false
            set c.target = t
            set c.t = NewTimer()
            set data[t] = d
        endif
        if not c.used then
            set c.used = true
            set Damage<u> = Damage<u> + 5
            set MaxMana<u> = MaxMana<u> + 50
            call BJDebugMsg(&quot;adding&quot;)
            call SetTimerData(c.t,d)
            call TimerStart(c.t,5,false,function Remove)
        endif
        call GroupRemoveUnit(ENUM_GROUP,t)
    endloop
    set u = null
endfunction

private function Loop takes nothing returns nothing
    call ForGroup(Casters,function DoEffect)
endfunction

private function Actions takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local data d
    if not IsUnitInGroup(u,Casters) then
        set d = data.create()
        set d.target = u
        set data<u> = d
    endif
    if FirstOfGroup(Casters) == null then
        call TimerStart(TIMER,TIMEOUT,true,function Loop)
    endif
    call GroupAddUnit(Casters,d.target)
    set u = null
endfunction

//===========================================================================
private function Init takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_HERO_SKILL)
    call TriggerAddCondition(t,Condition(function Conditions))
    call TriggerAddAction(t,function Actions )
    set b = Filter(function Check)
    set unitcheck = Filter(function CheckUnits)
endfunction
endscope</u></u></u></u></u></u>


JASS:
  if not c.used then
            set c.used = true
            set Damage<u> = Damage<u> + 5
            set MaxMana<u> = MaxMana<u> + 50
            call BJDebugMsg(&quot;adding&quot;)
            call SetTimerData(c.t,d)
            call TimerStart(c.t,5,false,function Remove)
        endif</u></u></u></u>


This part is running 2 times although it's supposed to run once.
 

Frozenhelfir

set Gwypaas = Guhveepaws
Reaction score
56
I'd personally use "if c.used == false" rather than the not syntax.

Put this on the outside of your loop and take out the if.
JASS:
        if c == 0 then
            set c = data.create()
            set c.used = false
            set c.target = t
            set c.t = NewTimer()
            set data[t] = d
        endif
 

Frozenhelfir

set Gwypaas = Guhveepaws
Reaction score
56
Add an extra unit variable into the struct.

Set c.unitvariablename = FirstOfGroup(ENUM_GROUP)

Creating a new struct for each unit is kind of overkill. What does:

JASS:
implement AutoData
do?
 

Frozenhelfir

set Gwypaas = Guhveepaws
Reaction score
56
Would you mind saying what your spell is supposed to do? It's kind of hard to figure it out with the unfamiliar syntax.

What I see it doing is it takes a whole group, and for each unit in that group it creates another group of every unit within range and then does some action. That boolean running the second time could in fact be there because one unit is used more than once, i.e. it is within range of two units so it goes through that loop twice.

JASS:
private function Loop takes nothing returns nothing
    call ForGroup(Casters,function DoEffect)
endfunction


That does your loop for EVERY unit in the group individually.
 

wraithseeker

Tired.
Reaction score
122
This is how the spell goes

Every 1 second, pick a unit type of my hero and checks 600 range whether there is a animated dead beside him and then if there is add damage and mana, it is supposed to be added once and not twice or more.

Casers are the hero who learned the spell.

EDIT: it is within range of two units so it goes through that loop twice.

Not possible as I tested with only one hero.
 

Frozenhelfir

set Gwypaas = Guhveepaws
Reaction score
56
Then you do need to move that block of code out of the loop. For every unit it finds in that group it'll create another struct and go thru the actions even though you only want them done once. Creating a struct for each unit in range is still overkill for this spell, and because it is periodic it might cause your map to lag.

Instead of moving that creation block outside you could add an exitwhen in there like such:

JASS:
        if not c.used then
            set c.used = true
            set Damage<u> = Damage<u> + 5
            set MaxMana<u> = MaxMana<u> + 50
            call BJDebugMsg(&quot;adding&quot;)
            call SetTimerData(c.t,d)
            call TimerStart(c.t,5,false,function Remove)
            exitwhen true //&lt;-----------
        endif</u></u></u></u>
 

T.s.e

Wish I was old and a little sentimental
Reaction score
133
JASS:
if not c.used then
            set c.used = true
            set Damage<u> = Damage<u> + 5
            set MaxMana<u> = MaxMana<u> + 50
            call BJDebugMsg(&quot;adding&quot;)
            call SetTimerData(c.t,d)
            call TimerStart(c.t,5,false,function Remove)
            exitwhen true //&lt;-----------
        endif

</u></u></u></u>

Would that even work? Because...
JASS:
if not c.used then // If c.used is false...
exitwhen true // then exit if false is true?
 

Frozenhelfir

set Gwypaas = Guhveepaws
Reaction score
56
JASS:
if not c.used then
            set c.used = true
            set Damage<u> = Damage<u> + 5
            set MaxMana<u> = MaxMana<u> + 50
            call BJDebugMsg(&quot;adding&quot;)
            call SetTimerData(c.t,d)
            call TimerStart(c.t,5,false,function Remove)
            exitwhen true //&lt;-----------
        endif

</u></u></u></u>

Would that even work? Because...
JASS:
if not c.used then // If c.used is false...
exitwhen true // then exit if false is true?


c.used is false on initialization, see the if block above it. The code is really weird :X. Doesn't exitwhen true always exit the loop? or is the correct syntax exitwhen true == true. you got your noodles back! :D
 

T.s.e

Wish I was old and a little sentimental
Reaction score
133
The syntax is fine, I mixed up this with exitwhen false, which would not work. This however, worked.
 
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