• 🏆 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!

[Solved] Check Prior to Local Variable

Status
Not open for further replies.
I need to check if the target unit is in water and deny the cast if it is not. Typically I have a separate trigger for this but the cast on this seems to be instant.

"locals are only supported at the top of the function"

  • Dematerialize Copy
    • Events
      • Unit - A unit Starts the effect of an ability
    • Conditions
      • (Ability being cast) Equal to Dematerialize
    • Actions
      • Set ErrorPlayer = (Owner of (Triggering unit))
      • Set Dematerialize_Terrain_Point = (Position of (Target unit of ability being cast))
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Terrain pathing at Dematerialize_Terrain_Point of type Floatability is off) Equal to True
        • Then - Actions
          • Unit - Order (Triggering unit) to Stop
          • Set ErrorMessage = Not enough water at target location.
          • Custom script: call ErrorMessage(udg_ErrorMessage,udg_ErrorPlayer)
        • Else - Actions
          • Custom script: local unit Dematerialize_Unit_A = GetTriggerUnit()
          • Custom script: local unit Dematerialize_Unit_B = GetSpellTargetUnit()
          • Custom script: set udg_Dematerialize_Point = GetUnitLoc(Dematerialize_Unit_B)
          • Custom script: set udg_Dematerialize_Point_B = GetUnitLoc(Dematerialize_Unit_A)
          • Custom script: call SetUnitX(Dematerialize_Unit_A, GetUnitX(Dematerialize_Unit_B) + 100)
          • Custom script: call SetUnitY(Dematerialize_Unit_A, GetUnitY(Dematerialize_Unit_B) + 100)
          • Unit - Create 1 Dummy (Dematerialize) for Neutral Passive at Dematerialize_Point_B facing Default building facing degrees
          • Set Dematerialize_Unit = (Last created unit)
          • Unit - Add a 1.00 second Generic expiration timer to Dematerialize_Unit
          • Custom script: call RemoveLocation(udg_Dematerialize_Point)
          • Custom script: call RemoveLocation(udg_Dematerialize_Point_B)
          • Custom script: set Dematerialize_Unit_A = null
          • Custom script: set Dematerialize_Unit_B = null
      • Custom script: call RemoveLocation(udg_Dematerialize_Terrain_Point)
 
There are 2 local unit declarations inside the "Else" part, but: "locals are only supported at the top of the function"
so the solution is to move those 2 lines on the very top of the function, right after "Actions".

You even can seperate declaration (define variable type and name) and initialization (assingn first value):
  • Custom script: local unit Dematerialize_Unit_A
  • ...
  • ...
  • Custom script: set Dematerialize_Unit_A = GetTriggerUnit()
  • But why you need these 2 unit locals?
 
Level 37
Joined
Jul 22, 2015
Messages
3,485
I need to check if the target unit is in water and deny the cast if it is not. Typically I have a separate trigger for this but the cast on this seems to be instant.
iirc, A unit Starts the effect of an ability is fired after cooldown / mana consumption, so it's basically impossible to interrupt them. You are going to want to use an issued order event, as I do here in the "AB Cancel" trigger.

"locals are only supported at the top of the function"
This would only make sense if you see how the GUI looks when it's converted into JASS.
 
It still takes the mana:

  • Dematerialize Copy
    • Events
      • Unit - A unit Is issued an order targeting an object
    • Conditions
      • (Issued order) Equal to (Order(blink))
      • (Unit-type of (Triggering unit)) Equal to Hopper
    • Actions
      • Custom script: local unit Dematerialize_Unit_A = GetTriggerUnit()
      • Custom script: local unit Dematerialize_Unit_B = GetOrderTargetUnit()
      • Set ErrorPlayer = (Owner of (Triggering unit))
      • Set Dematerialize_Terrain_Point = (Position of (Target unit of issued order))
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Terrain pathing at Dematerialize_Terrain_Point of type Floatability is off) Equal to True
        • Then - Actions
          • Unit - Order (Triggering unit) to Stop
          • Set ErrorMessage = Not enough water at target location.
          • Custom script: call ErrorMessage(udg_ErrorMessage,udg_ErrorPlayer)
        • Else - Actions
          • Custom script: set udg_Dematerialize_Point = GetUnitLoc(Dematerialize_Unit_B)
          • Custom script: set udg_Dematerialize_Point_B = GetUnitLoc(Dematerialize_Unit_A)
          • Custom script: call SetUnitX(Dematerialize_Unit_A, GetUnitX(Dematerialize_Unit_B) + 100)
          • Custom script: call SetUnitY(Dematerialize_Unit_A, GetUnitY(Dematerialize_Unit_B) + 100)
          • Unit - Create 1 Dummy (Dematerialize) for Neutral Passive at Dematerialize_Point_B facing Default building facing degrees
          • Set Dematerialize_Unit = (Last created unit)
          • Unit - Add a 1.00 second Generic expiration timer to Dematerialize_Unit
          • Custom script: call RemoveLocation(udg_Dematerialize_Point)
          • Custom script: call RemoveLocation(udg_Dematerialize_Point_B)
          • Custom script: set Dematerialize_Unit_A = null
          • Custom script: set Dematerialize_Unit_B = null
      • Custom script: call RemoveLocation(udg_Dematerialize_Terrain_Point)
 
Status
Not open for further replies.
Top