• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] local variables

Status
Not open for further replies.
Level 22
Joined
Feb 3, 2009
Messages
3,292
Hello, I have a question:

local variables are automatic MUI if used correctly, but they can only be used in the function that they are declared...

But for example, when making spells with timers and things like that, you need to give it a function, and then those actions in it are executed. So problem is, I need my locals from the main action function in the timer function... Is there any way to send the locals into that timer function and use them in there?

Hope I make sense :O
 
Level 15
Joined
Oct 18, 2008
Messages
1,591
And about the hashtables: if you know how to make the easyest functions in excel, then you can use it xD It's just like a coordinate system: At every coodrinate there can be a value ;) See, not that complex xD The only thing you must learn is the call functions, although you can easily copy-paste it's function and rewrite the spaces you want :D You see, I always do that if I don't know a script :D
 
Level 22
Joined
Feb 3, 2009
Messages
3,292
Yeah I'm very good with excel

That's true, but locals don't have a value in array, they are all same named, so they would overwrite themselves... it's different with auto increment arrays since you can make it store into field 1 + current array of caster variable for example...

EDIT: and offcourse I'll never give up, I never do, I just try to find an easy path :p
 
Level 9
Joined
Nov 28, 2008
Messages
704
Yeah I'm very good with excel

That's true, but locals don't have a value in array, they are all same named, so they would overwrite themselves... it's different with auto increment arrays since you can make it store into field 1 + current array of caster variable for example...

EDIT: and offcourse I'll never give up, I never do, I just try to find an easy path :p

what. does. this. even. mean.

Also, to the OP: if you don't understand something, LEARN. I don't understand why "I don't know how" is a reason for anything.
 
A basic usage of hashtables to attach in jass is like this:
JASS:
globals
    hashtable MyHash
endglobals
function MySpellKnock takes nothing returns nothing
    local timer t    = GetExpiredTimer()
    local integer id = GetHandleId(t)
    local unit tar   = LoadUnitHandle(MyHash,id,0)
    local integer c  = LoadInteger(MyHash,id,5) //counts the # of times ran
    local real x     = GetUnitX(tar)
    local real y     = GetUnitY(tar)
    local real Mdist = LoadReal(MyHash,id,4)
    local real ang   = LoadReal(MyHash,id,3)
    local real newX  = x+(Mdist/c)*Cos(ang)
    local real newY  = y+(Mdist/c)*Sin(ang)
    local real fDx   = LoadReal(MyHash,id,1)-newX
    local real fDy   = LoadReal(MyHash,id,2)-newY
    local real fDist = fDx*fDx+fDy*fDy
    
    call SetUnitX(tar,newX)
    call SetUnitY(tar,newY)
    call DestroyEffect(AddSpecialEffect("Your effect's path",newX,newY))
    
    if fDist <= 1225 then
        call PauseTimer(t)
        call DestroyTimer(t)
        call FlushChildHashtable(MyHash,id)
    endif
    call SaveInteger(MyHash,id,5,c+1)
    
    set t   = null
    set tar = null
endfunction

function MySpellAct takes nothing returns nothing
    local timer t    = CreateTimer()
    local integer id = GetHandleId(t)
    local unit cast  = GetTriggerUnit()
    local unit tar   = GetSpellTargetUnit()
    local real x     = GetUnitX(cast)
    local real y     = GetUnitY(cast)
    local real tx    = GetUnitX(tar)
    local real ty    = GetUnitY(tar)
    local real distX = tx-x
    local real distY = ty-y
    local real ang   = Atan2(distY,distX)
    local real dist  = (SquareRoot(distX*distX+distY*distY))/2
//will send the unit away half the distance between the caster and target 
    
    call SaveUnitHandle(MyHash,id,0,tar)
    call SaveReal(MyHash,id,1,tx+dist*Cos(ang))
    call SaveReal(MyHash,id,2,ty+dist*Sin(ang))
    call SaveReal(MyHash,id,3,ang)
    call SaveReal(MyHash,id,4,dist) 
    call SaveInteger(MyHash,id,5,0)
    
    call TimerStart(t,0.035,true,function MySpellKnock)
    
    set t    = null
    set cast = null
    set tar  = null
endfunction
function MySpellCond takes nothing returns boolean
    return GetSpellAbilityId() == 'A000'
endfunction
function InitTrig_MySpell takes nothing returns nothing
    local trigger t = CreateTrigger()
    set MyHash = InitHashtable()
    call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
    call TriggerAddCondition(t,Condition(function MySpellCond))
    call TriggerAddAction(t,function MySpellAct)
    set t = null
endfunction

Just an example though, I haven't tested if it works. But you get the idea.. For every new timer created, a new handle id will be assigned. This kind of allows you to save each hashtable's data into a separate container.

Say you have to package away a bunch of boxes. You load the same inventory over and over doing backbreaking labor. As each timer is created, there will be a new box. On the box will be its very own label. Now, you must store items in the box (hashtable). So you start stuffing the data, all the paperwork and numbers and everything and squeeze it into the box. The box gets shipped out now to a conveyor belt, this is kind of like the timer initiating.

Now you get the data processed, they will do what they need to do with the inventory. They will go and polish it, move it around, fix it, and ship it. But once they are done using the box and the time is right, they'll destroy it. They'll destroy the box and it's inventory will void. (FlushChildHashtable) The box will then stop its transport (Timer destroyed) and it will just fall off the conveyor belt into a recycling bin. Now that the label for the box is gone, the people are now allowed to use that number again. (As each timer is destroyed, their handle id becomes free to use once again) Now the box is put back together from its recycled parts and the process begins again.

Not the clearest explanation, but it might help a bit to understand what the values are meant to do.
 
There is nothing about "auto MUI". Locals are just created each time again and again, making it impossible to collide. Structs are ARRAYS, not normal globals. vJass automaticly allocates new instances and calculates the correct array index, you don't see anything of this. Structs ARE MUI as long as you keep creating and destroying them correctly.
 
you don't need to type this.u when you are inside the struct instance.
You can just type .u or even u.

When it isn't a local the struct variable will be used. When there is no struct variable, a global will be used. If there isn't a global, an error occurs.

Thistype.allocate() creates a new struct instance. A new object.
You work with objects.

Library is used for Systems.
Scopes are used for Spells and System tests.

Scope are automatically declared after librarys.
 
Status
Not open for further replies.
Top