• 🏆 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!
  • ✅ The POLL for Hive's Texturing Contest #33 is OPEN! Vote for the TOP 3 SKINS! 🔗Click here to cast your vote!

[Solved] Local Variable Inside IF/THEN/ELSE

Status
Not open for further replies.
Level 33
Joined
Mar 27, 2008
Messages
8,035
Hey, tell me why does this trigger won't work;
  • SD Loop
    • Events
    • Conditions
    • Actions
      • Custom script: local real x1
      • Custom script: local real x2
      • Custom script: local real y1
      • Custom script: local real y2
      • Custom script: local real z1
      • Custom script: local real z2
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (SD_Group is empty) Equal to True
        • Then - Actions
          • Trigger - Turn off (This trigger)
        • Else - Actions
          • Unit Group - Pick every unit in SD_Group and do (Actions)
            • Loop - Actions
              • Set TempUnit = (Picked unit)
              • Custom script: set udg_SD_Key = GetHandleId(udg_TempUnit)
              • Set SD_Duration = (Load 3 of SD_Key from SD_Hashtable)
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • SD_Duration Greater than 0.00
                • Then - Actions
                  • Set SD_Caster = (Load 1 of SD_Key in SD_Hashtable)
                  • Set SD_Lightning = (Load 0 of SD_Key in SD_Hashtable)
                  • Set TempLoc = (Position of TempUnit)
                  • Set TempLoc2 = (Position of SD_Caster)
                  • Custom script: set x1 = GetUnitX(udg_SD_Caster)
                  • Custom script: set y1 = GetUnitY(udg_SD_Caster)
                  • Custom script: set z1 = GetLocationZ(udg_TempLoc2) + GetUnitFlyHeight(udg_SD_Caster)
                  • Custom script: set x2 = GetUnitX(udg_TempUnit)
                  • Custom script: set y2 = GetUnitY(udg_TempUnit)
                  • Custom script: set z2 = GetLocationZ(udg_TempLoc) + GetUnitFlyHeight(udg_TempUnit)
                  • Custom script: call MoveLightningEx(udg_SD_Lightning, true, x1, y1, z1, x2, y2, z2)
                  • Hashtable - Save (SD_Duration - SD_Interval) as 3 of SD_Key in SD_Hashtable
                • Else - Actions
                  • Unit - Kill TempUnit
This trigger is mostly about those setting of local variable, when I deleted all of them, this trigger does not compile any error oh and the error said "Expected a variable name".

What amazed me was that if I put all those set values of local variable outside of the IF/THEN/ELSE block, it works.

Is there any way to do this or do I really have to create 6 TempReal variable just so I can store those values for use with MoveLightningEx ?

EDIT:
I noticed it could not function within Unit Group, not with IF/THEN/ELSE as I tried to put those functions just below the Unit Group Pick function.
But how can I set those each local variable for Unit in that Group then ?
 
Level 33
Joined
Mar 27, 2008
Messages
8,035
If you just need to have local variables within the loop for internal work, declare them directly below "Loop - Actions".

But that's what I did ?

I noticed it could not function within Unit Group, not with IF/THEN/ELSE as I tried to put those functions just below the Unit Group Pick function.
As I put the settings of the local variable directly under the Pick Unit Group and still fails.
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
  • SD Loop
    • Events
    • Conditions
    • Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (SD_Group is empty) Equal to True
        • Then - Actions
          • Trigger - Turn off (This trigger)
        • Else - Actions
          • Unit Group - Pick every unit in SD_Group and do (Actions)
            • Loop - Actions
              • Custom script: local real x1
              • Custom script: local real x2
              • Custom script: local real y1
              • Custom script: local real y2
              • Custom script: local real z1
              • Custom script: local real z2
              • Set TempUnit = (Picked unit)
              • Custom script: set udg_SD_Key = GetHandleId(udg_TempUnit)
              • Set SD_Duration = (Load 3 of SD_Key from SD_Hashtable)
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • SD_Duration Greater than 0.00
                • Then - Actions
                  • Set SD_Caster = (Load 1 of SD_Key in SD_Hashtable)
                  • Set SD_Lightning = (Load 0 of SD_Key in SD_Hashtable)
                  • Set TempLoc = (Position of TempUnit)
                  • Set TempLoc2 = (Position of SD_Caster)
                  • Custom script: set x1 = GetUnitX(udg_SD_Caster)
                  • Custom script: set y1 = GetUnitY(udg_SD_Caster)
                  • Custom script: set z1 = GetLocationZ(udg_TempLoc2) + GetUnitFlyHeight(udg_SD_Caster)
                  • Custom script: set x2 = GetUnitX(udg_TempUnit)
                  • Custom script: set y2 = GetUnitY(udg_TempUnit)
                  • Custom script: set z2 = GetLocationZ(udg_TempLoc) + GetUnitFlyHeight(udg_TempUnit)
                  • Custom script: call MoveLightningEx(udg_SD_Lightning, true, x1, y1, z1, x2, y2, z2)
                  • Hashtable - Save (SD_Duration - SD_Interval) as 3 of SD_Key in SD_Hashtable
                • Else - Actions
                  • Unit - Kill TempUnit

This way? Has nothing to do with If/Then/Else.
 
Level 33
Joined
Mar 27, 2008
Messages
8,035
OMFG IT WORKED.

I thought local variable should be put as main priorities in each case/trigger, that's why I decided to just put anything that relates with local variable, will be put at the top of the trigger function no matter what.

But strange enough, this case, it works.

Can you explain to me why it works ?

When local variable should be put at the most top of trigger function before doing anything else (in this case, the check condition).
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
As I have said, group-loops are externalized in a new function. The declarations are at the top of the function. Your trigger looks like this in jass:

JASS:
function Trig_SD_Loop_Func001Func001Func003C takes nothing returns boolean
    if ( not ( udg_SD_Duration > 0.00 ) ) then
        return false
    endif
    return true
endfunction

function Trig_SD_Loop_Func001Func001A takes nothing returns nothing
    local real x1
    local real x2
    local real y1
    local real y2
    local real z1
    local real z2
    set udg_TempUnit = GetEnumUnit()
    set udg_SD_Key = GetHandleId(udg_TempUnit)
    set udg_SD_Duration = LoadRealBJ(3, udg_SD_Key, udg_SD_Hashtable)
    if ( Trig_SD_Loop_Func001Func001Func003C() ) then
        set udg_SD_Caster = LoadUnitHandleBJ(1, udg_SD_Key, udg_SD_Hashtable)
        set udg_SD_Lightning = LoadLightningHandleBJ(0, udg_SD_Key, udg_SD_Hashtable)
        set udg_TempLoc = GetUnitLoc(udg_TempUnit)
        set udg_TempLoc2 = GetUnitLoc(udg_SD_Caster)
        set x1 = GetUnitX(udg_SD_Caster)
        set y1 = GetUnitY(udg_SD_Caster)
        set z1 = GetLocationZ(udg_TempLoc2) + GetUnitFlyHeight(udg_SD_Caster)
        set x2 = GetUnitX(udg_TempUnit)
        set y2 = GetUnitY(udg_TempUnit)
        set z2 = GetLocationZ(udg_TempLoc) + GetUnitFlyHeight(udg_TempUnit)
        call MoveLightningEx(udg_SD_Lightning, true, x1, y1, z1, x2, y2, z2)
        call SaveRealBJ( ( udg_SD_Duration - udg_SD_Interval ), 3, udg_SD_Key, udg_SD_Hashtable )
    else
        call KillUnit( udg_TempUnit )
    endif
endfunction

function Trig_SD_Loop_Func001C takes nothing returns boolean
    if ( not ( IsUnitGroupEmptyBJ(udg_SD_Group) == true ) ) then
        return false
    endif
    return true
endfunction

function Trig_SD_Loop_Actions takes nothing returns nothing
    if ( Trig_SD_Loop_Func001C() ) then
        call DisableTrigger( GetTriggeringTrigger() )
    else
        call ForGroupBJ( udg_SD_Group, function Trig_SD_Loop_Func001Func001A )
    endif
endfunction

//===========================================================================
function InitTrig_SD_Loop takes nothing returns nothing
    set gg_trg_SD_Loop = CreateTrigger(  )
    call TriggerAddAction( gg_trg_SD_Loop, function Trig_SD_Loop_Actions )
endfunction
 
Status
Not open for further replies.
Top