• 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] Jumping Code

Status
Not open for further replies.
Level 11
Joined
Sep 12, 2008
Messages
657
okay, i made a code that makes a unit jump in a realistic way, but for some reason, when i added the same unit to the same loop(i dont use values, hashtables, groups, or w/e, only loops.), it made him go down faster, if i wrote it 10 times, he just jumped down. can any 1 tell me whats wrong with this code?

JASS:
library SpeedGenerator
    globals
        integer array SCRealityCrush
        real array SCRealityStatement
        real array SCFacing
        real array SCHeightDecrease
        integer array SCCurrentCount
        unit array SCUnit
        real array SCStartingHeight
        boolean array SCGeneratedUnit
    endglobals
    
function Trig_Generator_Actions takes nothing returns nothing
local integer i = 0
local real f
local unit u

    loop
    set i = i + 1
    set u = SCUnit[i]
    set f = GetUnitFlyHeight(u)
    
    
    if SCGeneratedUnit[i] == true then
    if f >= 0 then
    //--Doesnt Work?--\\
    call SetUnitFlyHeight(u, f-SCHeightDecrease[i]*0.10, 0) 
    //--Doesnt Work?--\\
    
    if SCRealityCrush[i] >= 0 then
    
    set SCRealityCrush[i] = SCRealityCrush[i]-1
    set SCHeightDecrease[i] = SCHeightDecrease[i]-SCRealityStatement[i]
    set SCStartingHeight[i] = SCStartingHeight[i]-SCRealityStatement[i]
    call SetUnitFlyHeight(u, SCStartingHeight[i], 0)
    
    endif
    
    else
    
    call UnitRemoveAbility(u, 'Amrf')
    call SetPlayerAbilityAvailable(GetOwningPlayer(u), 'Amrf', true)
    set SCRealityCrush[i] = 0
    set SCRealityStatement[i] = 0
    set SCFacing[i] = 0
    set SCHeightDecrease[i] = 0
    set SCStartingHeight[i] = 0
    set SCUnit[i] = u
    set SCCurrentCount[1] = SCCurrentCount[1] - 1
    set SCCurrentCount[2] = 0
    set SCGeneratedUnit[i] = false
    
    endif
    endif
    
    exitwhen i >= SCCurrentCount[2]
    endloop

endfunction

function GenerateAdd takes unit u returns nothing

    set SCCurrentCount[1] = SCCurrentCount[1] + 1
    set SCCurrentCount[2] = SCCurrentCount[2] + 1
    set SCRealityCrush[SCCurrentCount[2]] = udg_SC_RealityCrushing
    set SCRealityStatement[SCCurrentCount[2]] = udg_SC_RealityStatement
    set SCFacing[SCCurrentCount[2]] = udg_SC_Facing
    set SCHeightDecrease[SCCurrentCount[2]] = udg_SC_HeightDecreasePerSecond
    set SCStartingHeight[SCCurrentCount[2]] = udg_SC_StartingHeight
    set SCUnit[SCCurrentCount[2]] = u
    set SCGeneratedUnit[SCCurrentCount[2]] = true
    call UnitAddAbility(u, 'Amrf')
    call SetPlayerAbilityAvailable(GetOwningPlayer(u), 'Amrf', false)
    call SetUnitFlyHeight(u, SCStartingHeight[SCCurrentCount[2]], 0)

endfunction
endlibrary

function InitTrig_Generator takes nothing returns nothing
    set gg_trg_Generator= CreateTrigger(  )
    call TriggerRegisterTimerEventPeriodic( gg_trg_Generator, 0.10 )
    call TriggerAddAction( gg_trg_Generator, function Trig_Generator_Actions )
endfunction



im pretty sure the problem is in:
call SetUnitFlyHeight(u, f-SCHeightDecrease*0.10, 0)

thank you in advance :D
 
Level 11
Joined
Sep 12, 2008
Messages
657
nah, thats fixed.. my problem is now how to set X/Y.. since it wont let me


JASS:
library SpeedGenerator
    globals
        integer array SCRealityCrush
        real array SCRealityStatement
        real array SCFacing
        real array SCHeightDecrease
        integer SCCurrentCount = 0
        unit array SCUnit
        real array SCStartingHeight
        boolean array SCGeneratedUnit
        integer SCChecker = 0
    endglobals
    
function Trig_Generator_Actions takes nothing returns nothing
local integer i = 0
local real f
local unit u
local integer counter
local real x
local real y

    loop
            set i = i + 1
            set u = SCUnit[i]
            set f = GetUnitFlyHeight(u)
            set x = GetUnitX(u)
            set y = GetUnitY(u)
    
        if SCGeneratedUnit[i] == true then
    
        if f != 0 then
            call SetUnitFlyHeight(u, f-SCHeightDecrease[i], 0) 
            call SetUnitX(u, x + 15 * Cos (SCFacing[i]) * bj_DEGTORAD)
            call SetUnitY(u, y + 15 * Sin (SCFacing[i]) * bj_DEGTORAD)
        endif
    
        if f <= 10 then
            call UnitRemoveAbility(u, 'Amrf')
            call SetPlayerAbilityAvailable(GetOwningPlayer(u), 'Amrf', true)
            set SCRealityCrush[i] = 0
            set SCRealityStatement[i] = 0
            set SCFacing[i] = 0
            set SCHeightDecrease[i] = 0
            set SCStartingHeight[i] = 0
            set SCUnit[i] = u
            set SCChecker = SCChecker - 1

            set SCGeneratedUnit[i] = false
        endif
        
        if SCChecker == 0 then 
            loop
            set counter = counter + 1
            //-------------------------------------\\
            set SCRealityCrush[counter] = 0
            set SCRealityStatement[counter] = 0
            set SCFacing[counter] = 0
            set SCHeightDecrease[counter] = 0
            set SCStartingHeight[counter] = 0
            set SCUnit[counter] = u
            set SCCurrentCount = SCCurrentCount - 1
            set SCGeneratedUnit[counter] = false
            //------------------------------------\\
            exitwhen counter == SCCurrentCount
            endloop

        endif
        endif
    
        exitwhen i >= SCCurrentCount
    endloop

endfunction

function GenerateAdd takes unit u returns nothing

    set SCCurrentCount = SCCurrentCount + 1
    set SCChecker = SCChecker + 1
    set SCRealityCrush[SCCurrentCount] = udg_SC_RealityCrushing
    set SCRealityStatement[SCCurrentCount] = udg_SC_RealityStatement
    set SCFacing[SCCurrentCount] = udg_SC_Facing
    set SCHeightDecrease[SCCurrentCount] = udg_SC_HeightDecreasePerSecond *0.10
    set SCStartingHeight[SCCurrentCount] = udg_SC_StartingHeight
    set SCUnit[SCCurrentCount] = u
    set SCGeneratedUnit[SCCurrentCount] = true
    call UnitAddAbility(u, 'Amrf')
    call SetPlayerAbilityAvailable(GetOwningPlayer(u), 'Amrf', false)
    call SetUnitFlyHeight(u, SCStartingHeight[SCCurrentCount], 0)

endfunction
endlibrary

function InitTrig_Generator takes nothing returns nothing
    set gg_trg_Generator= CreateTrigger(  )
    call TriggerRegisterTimerEventPeriodic( gg_trg_Generator, 0.10 )
    call TriggerAddAction( gg_trg_Generator, function Trig_Generator_Actions )
endfunction


this whole code is for reaistic jump and knock..

(If he is knockback, he will get knocked from the earth and jump backwards..)

RealisticCrush = amount of jumps on ground,
RealisticStatement = Amount of height/height losse on each jump.
 
Your indentation is a nightmare. Every block should be four spaces indented, with every nested block following the same rules.

Also, libraries have initializers and private members for a reason. Don't waste your time prefixing things when you've got NewGen.

What's with the udg_ variables? Where are they being set?

Needs more data before changes can be made. If you mix in GUI with JASS, you get ugly results. Just stay with JASS and throw away GUI in almost every situation (exceptions made for visual optimizations such as deciding which model string to use for a special effect, editing terrain/object editor... the basics).

This is how to properly indent your code and exploit library initializers to their advantage, (and check the initializer too because it's greatly optimized):

JASS:
library SpeedGenerator initializer init
    globals
        integer array SCRealityCrush
        real array SCRealityStatement
        real array SCFacing
        real array SCHeightDecrease
        integer SCCurrentCount = 0
        unit array SCUnit
        real array SCStartingHeight
        boolean array SCGeneratedUnit
        integer SCChecker = 0
    endglobals
    
    private function onExpire takes nothing returns nothing
        local integer i = 0
        local real f
        local unit u
        local integer counter
        local real x
        local real y

        loop
            set i = i + 1
            set u = SCUnit[i]
            set f = GetUnitFlyHeight(u)
            set x = GetUnitX(u)
            set y = GetUnitY(u)
        
            if SCGeneratedUnit[i] == true then
        
                if f != 0 then
                    call SetUnitFlyHeight(u, f-SCHeightDecrease[i], 0) 
                    call SetUnitX(u, x + 15 * Cos (SCFacing[i]) * bj_DEGTORAD)
                    call SetUnitY(u, y + 15 * Sin (SCFacing[i]) * bj_DEGTORAD)
                endif
            
                if f <= 10 then
                    call UnitRemoveAbility(u, 'Amrf')
                    call SetPlayerAbilityAvailable(GetOwningPlayer(u), 'Amrf', true)
                    set SCRealityCrush[i] = 0
                    set SCRealityStatement[i] = 0
                    set SCFacing[i] = 0
                    set SCHeightDecrease[i] = 0
                    set SCStartingHeight[i] = 0
                    set SCUnit[i] = u
                    set SCChecker = SCChecker - 1

                    set SCGeneratedUnit[i] = false
                endif
                
                if SCChecker == 0 then 
                    loop
                        set counter = counter + 1
                        //-------------------------------------\\
                        set SCRealityCrush[counter] = 0
                        set SCRealityStatement[counter] = 0
                        set SCFacing[counter] = 0
                        set SCHeightDecrease[counter] = 0
                        set SCStartingHeight[counter] = 0
                        set SCUnit[counter] = u
                        set SCCurrentCount = SCCurrentCount - 1
                        set SCGeneratedUnit[counter] = false
                        //------------------------------------\\
                        exitwhen counter == SCCurrentCount
                    endloop

                endif
                
            endif
        
            exitwhen i >= SCCurrentCount
        endloop

    endfunction

    function GenerateAdd takes unit u returns nothing
    
        set SCCurrentCount = SCCurrentCount + 1
        set SCChecker = SCChecker + 1
        set SCRealityCrush[SCCurrentCount] = udg_SC_RealityCrushing
        set SCRealityStatement[SCCurrentCount] = udg_SC_RealityStatement
        set SCFacing[SCCurrentCount] = udg_SC_Facing
        set SCHeightDecrease[SCCurrentCount] = udg_SC_HeightDecreasePerSecond *0.10
        set SCStartingHeight[SCCurrentCount] = udg_SC_StartingHeight
        set SCUnit[SCCurrentCount] = u
        set SCGeneratedUnit[SCCurrentCount] = true
        call UnitAddAbility(u, 'Amrf')
        call SetPlayerAbilityAvailable(GetOwningPlayer(u), 'Amrf', false)
        call SetUnitFlyHeight(u, SCStartingHeight[SCCurrentCount], 0)
    
    endfunction
    
    private function init takes nothing returns nothing
        call TimerStart(CreateTimer(), 0.10, true, function onExpire)
    endfunction
    
endlibrary
 
Level 14
Joined
Nov 18, 2007
Messages
816
screw this, heres a version using structs:
JASS:
library SpeedGenerator
    
    globals
        private constant real TICK = 1./40
    endglobals
    
    private struct Data
        unit whichUnit
        integer realityCrush
        real realityStatement
        real facing
        real heightDecrease
        real startingHeight
        real height
        boolean generatedUnit
        
        private integer i
        
        private static thistype array Structs
        private static integer Count=0
        private static timer T=CreateTimer()
        
        private static method Callback takes nothing returns nothing
        local integer i=Count-1
        local thistype s
        
            loop
                exitwhen i<0
                set s=Structs[i]
                
                call SetUnitX(s.whichUnit, GetUnitX(s.whichUnit)+15*Cos(s.facing)) // still bad form, the magic 15 bugs me
                call SetUnitY(s.whichUnit, GetUnitY(s.whichUnit)+15*Sin(s.facing))
                set s.height=s.height-s.heightDecrease
                call SetUnitFlyHeight(s.whichUnit, s.height, 0)
                if s.height<=0 then
                    call s.destroy()
                endif
                
                set i=i-1
            endloop
        endmethod
        
        public method onDestroy takes nothing returns nothing
            set whichUnit=null
            
            set Count=Count-1
            set Structs[i]=Structs[Count]
            set Structs[i].i=i
            if Count==0 then
                call PauseTimer(T)
            endif
        endmethod
        
        public method create takes unit u, integer crush, real statement, real facing, real heightDecrease, real startingHeight, boolean generated returns thistype
        local thistype s=allocate()
            set s.whichUnit=u
            set s.realityCrush=crush
            set s.realityStatement=statement
            set s.facing=facing*bj_DEGTORAD
            set s.heightDecrease=heightDecrease*TICK
            set s.startingHeight=startingHeight
            set s.generatedUnit=generated
            
            call UnitAddAbility(u, 'Amrf')
            call UnitRemoveAbility(u, 'Amrf')
            call SetUnitFlyHeight(u, startingHeight, 0)
            
            set Structs[Count]=s
            set s.i=Count
            if Count==0 then
                call TimerStart(T, TICK, true, function thistype.Callback)
            endif
            set Count=Count+1
            return s
        endmethod
    endstruct

    function GenerateAdd takes unit u returns nothing
        call Data.create(u, udg_SC_RealityCrushing, udg_SC_RealityStatement, udg_SC_Facing, udg_SC_HeightDecreasePerSecond, udg_SC_StartingHeight, true)
    endfunction
    
endlibrary

There you go.
 
Status
Not open for further replies.
Top