• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Jass spell question

Status
Not open for further replies.
Level 5
Joined
Aug 14, 2020
Messages
118
hello there! it’s been a while since I started to learn jass but one thing about local variables is unclear for me and couldn't find a good tutorial to explain that for me.

local variables stays in the function but I want to know,does that mean two casters can cast the jass spell at the same time? or it will face bug?
 
Level 20
Joined
Feb 27, 2019
Messages
594
A local variable would be like using (Triggering unit) which does not cause any conflicts if several instances of the trigger are running. Remember that local variables have to be nulled except for integers, reals and booleans.

EDIT: I guess functions dont really "trigger" so its a bad comparison really lol.
 
Last edited:
Level 5
Joined
Aug 14, 2020
Messages
118
A local variable would be like using (Triggering unit) which does not cause any conflicts if several instances of the trigger are running. Remember that local variables have to be nulled except for integers, reals and booleans.

EDIT: I guess functions dont really "trigger" so its a bad comparison really lol.

so you mean I can use it for my spells that have too many casters?
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,585
Yes, local variables will allow you to make your spells MUI (Multi-Unit-Instanceable). In other words, multiple units can use it at the same time. That being said, some types of spells will still need global variables to work properly.

Also, Lua makes local variables even easier to use, so if you plan on learning Jass I would recommend taking a look at Lua before doing so. It's pretty much objectively better than Jass. The only downside it has is with compatibility issues since you can't use Jass + Lua at the same time. However, I've read that there are ways to get around that.
 
Last edited:
Level 26
Joined
May 18, 2018
Messages
397
When you use local variables, you are declaring them inside a specific function. Local variables, unlike globals, allow multiple instances of the function to run simultaneously.

Just remember that local variables belong to the function in which they were declared, and can only be used within it.

JASS:
function Actions takes nothing returns nothing
    local unit caster = GetSpellAbilityUnit()
    local unit target = GetSpellTargetUnit()
    local real casterCurrentHP = GetUnitState(caster, UNIT_STATE_LIFE)
    local real dmgAmount = 200
    local real healAmount= 100

    call UnitDamageTarget(caster, target, dmgAmount, true, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_MAGIC, null)
    call SetUnitState(caster, UNIT_STATE_LIFE, casterCurrentHP + healAmount)
                  
    set caster = null
    set target = null
endfunction

This is a basic example, as you can see I declared some local variables of different types. At the end of the function, you should null any variable you are using (except for integers, reals and booleans) to avoid memory leaks.

Another thing to remember is that local variables must be created before any other code. So this example is BAD:

JASS:
function Actions takes nothing returns nothing
    local unit caster = GetSpellAbilityUnit()
    local unit target = GetSpellTargetUnit()
    local real dmgAmount = 100

    call UnitDamageTarget(caster, target, dmgAmount, true, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_MAGIC, null)
   
    local real casterCurrentHP = GetUnitState(caster, UNIT_STATE_LIFE)
    local real healAmount= 75

    call SetUnitState(caster, UNIT_STATE_LIFE, casterCurrentHP + healAmount)
                  
    set caster = null
    set target = null
endfunction

Instead, you have to create the local variables first and then set a value for them, as shown below:

JASS:
function Actions takes nothing returns nothing
    local unit caster = GetSpellAbilityUnit()
    local unit target = GetSpellTargetUnit()
    local real casterCurrentHP
    local real dmgAmount
    local real healAmount

    set dmgAmount = 100

    call UnitDamageTarget(caster, target, dmgAmount, true, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_MAGIC, null)

    set casterCurrentHP = GetUnitState(caster, UNIT_STATE_LIFE)
    set healAmount = 75

    call SetUnitState(caster, UNIT_STATE_LIFE, casterCurrentHP + healAmount)
                  
    set caster = null
    set target = null
endfunction
 
Status
Not open for further replies.
Top