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

[JASS] First Jass Spell (Jump)

Status
Not open for further replies.
Level 5
Joined
Sep 16, 2008
Messages
47
Hi i tried to create my first Jass Spell.
Here is code:
JASS:
function Trig_Jump_Conditions takes nothing returns boolean
    return(GetSpellAbilityId() == 'A007')
endfunction

function Jump takes nothing returns nothing
    local timer  t               = GetExpiredTimer()
    local integer id = GetHandleId(t)
    local real   Speed           = LoadReal(udg_Jump_Table, id, 0)
    local real   Distance        = LoadReal(udg_Jump_Table, id, 1)
    local real   MaxHeight       = LoadReal(udg_Jump_Table, id, 2)
    local real   CurrentDistance = LoadReal(udg_Jump_Table, id, 3)
    local real   Real            = LoadReal(udg_Jump_Table, id, 4)
    local real   X               = LoadReal(udg_Jump_Table, id, 5)
    local real   Y               = LoadReal(udg_Jump_Table, id, 6)
    local real   X2              = LoadReal(udg_Jump_Table, id, 7)
    local real   Y2              = LoadReal(udg_Jump_Table, id, 8)
    local unit   Caster          = LoadUnitHandle(udg_Jump_Table, id, 9)
    local string a="This is a message"
    call DisplayTextToForce( GetPlayersAll(), a )
    set Real = ( 4 * MaxHeight / Distance) * ( Distance - CurrentDistance) * ( CurrentDistance / Distance )
    set X = GetLocationX(GetUnitLoc(Caster))
    set Y = GetLocationY(GetUnitLoc(Caster))
    set X2 = X + Speed
    set Y2 = Y + Speed
    call DisplayTextToForce( GetPlayersAll(), a )
    call SetUnitPosition(Caster, X2, Y2)
    call SetUnitFlyHeight(Caster, Real, 0)
    if ( CurrentDistance == Distance ) then
        call UnitRemoveAbility(Caster, 'Arav')
        call FlushChildHashtableBJ( GetHandleIdBJ(t), udg_Jump_Table )
        call DestroyTimer(t)
        return
    endif
    call SaveRealBJ(CurrentDistance + Speed, 3, GetHandleIdBJ(t), udg_Jump_Table)
    set t = null 
    set Caster = null  
endfunction

function Trig_Jump_Actions takes nothing returns nothing
    local unit Caster = GetTriggerUnit()
    local location CasterLoc = GetUnitLoc(Caster)
    local location TargetLoc = GetSpellTargetLoc()
    local real Hyp
    local real SINE
    local real COSINE
    local real Distance
    local real CurrentDistance
    local real Height
    local real HYPSpeed
    local real Speed
    local real Angle
    local real Real
    local real X
    local real Y
    local real X2
    local real Y2
    local timer t = CreateTimer()
    local real update = 0.03
    local real MaxHeight = 500.00
    local integer id = GetHandleId(t)
    set CurrentDistance = 0.00
    set Distance = DistanceBetweenPoints(CasterLoc, TargetLoc)
    set Angle = AngleBetweenPoints(CasterLoc, TargetLoc) 
    set Height = 0
    set Hyp = SquareRoot(( ( Distance * Distance ) + ( Height * Height ) ))
    set SINE = (Distance / Hyp)
    set COSINE = (Height / Hyp)
    set HYPSpeed = Hyp * 0.03
    set Speed = (HYPSpeed * SINE)
    call UnitAddAbility(Caster, 'Arav')
    call SaveReal(udg_Jump_Table, id, 0, Speed)
    call SaveReal(udg_Jump_Table, id, 1, Distance)
    call SaveReal(udg_Jump_Table, id, 2, MaxHeight)
    call SaveReal(udg_Jump_Table, id, 3, CurrentDistance)
    call SaveReal(udg_Jump_Table, id, 4, Real)
    call SaveReal(udg_Jump_Table, id, 5, X)
    call SaveReal(udg_Jump_Table, id, 6, Y)
    call SaveReal(udg_Jump_Table, id, 7, X2)
    call SaveReal(udg_Jump_Table, id, 8, Y2)
    call SaveUnitHandle(udg_Jump_Table, id, 9, Caster)
    call RemoveLocation(CasterLoc)
    call RemoveLocation(TargetLoc)
    call TimerStart(t, update, true, function Jump)
    set Caster = null
    set t = null
endfunction

//===========================================================================
function InitTrig_Jump takes nothing returns nothing
    set gg_trg_Jump = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Jump, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddCondition( gg_trg_Jump, Condition( function Trig_Jump_Conditions ) )
    call TriggerAddAction( gg_trg_Jump, function Trig_Jump_Actions )
endfunction
When i cast this spell just nothing happens
 
Last edited:
Level 15
Joined
Oct 16, 2010
Messages
941
JASS:
call SaveReal(udg_Jump_Table, GetHandleId(t), 5, X)
    call SaveReal(udg_Jump_Table, GetHandleId(t), 6, Y)
    call SaveReal(udg_Jump_Table, GetHandleId(t), 7, X2)
    call SaveReal(udg_Jump_Table, GetHandleId(t), 8, Y2)

You save these variables to the hashtable but you never set them to anything.
 
Level 12
Joined
Feb 22, 2010
Messages
1,115
If you attempt to use a non-initialized variable, trigger stops running, ex:

JASS:
local real x2
call SaveReal(hashtable, parent, child, x2)

You need to do something like that.

JASS:
local real x2 = 0
call SaveReal(hashtable, parent, child, x2)
//or just use 0 directly, since there is no x2 at start
call SaveReal(hashtable, parent, chidl, 0)
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
You are saving variables into the hashtable which have no value. This is pointless code and thus can be deleted.

What you are after doing is allocating a value to the variables to save.

It is far better to use global arrays in an index recycling system to store the grunt data (like what structs in vJASS do) and then store the array index that was used rather than storing each element separatly. Array lookups are quite a bit faster than hashtable lookups and especially since thise code fires 33 times a second such efficency is weclome.
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,468
Poor script handling

GetHandleId(t)

- Instead of calling repeatedly, do:

local integer id = GetHandleId(t)

- And reference "id" instead.

call DisplayTextToForce( GetPlayersAll(), a )

- Better off with BJDebugMsg(a)

set X = GetLocationX(GetUnitLoc(Caster))
set Y = GetLocationY(GetUnitLoc(Caster))


- Better off with:

set x = GetUnitX(caster)
set y = GetUnitY(caster)

Local variables must always start with a lowercase
 
Level 11
Joined
May 31, 2008
Messages
698
set x2 = x1 + distance*cos(angle*BJDEGTORAD)
set y2 = y1 + distance*sin(angle*BJDEGTORAD)

i believe thats what you should do for setting x and y

And for setting the change in height, you could do set v = v0 - a*t
v would be initial velocity
to set the initial velocity, do v0 = square root(2*a*maxheight)
"a" should be around 980 i think

At least thats how i would do it :p
And i dont really understand how you are setting the height xD
But this way is accurate cause it uses the actual physics equation
 
Status
Not open for further replies.
Top