• 🏆 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] Knockback

Status
Not open for further replies.
Level 8
Joined
Jul 28, 2008
Messages
211
I was following a tutorial on how to make a knockback, and I did it but it isn't working. I didn't do all of it the way I wanted cuz I wanted to follow the tutorial. Made a mistake somewhere, and I'm tired and sleepy so I don't have time to check it today. Could someone check it and tell me where I'm wrong?
I don't want to Copy & Paste it from the tutorial, cuz I wouldn't learn anything then.

Thanks in advance! :thumbs_up:

JASS:
scope Knockback initializer Init

globals
    item It = CreateItem('ciri', 0, 0)
endglobals
////////////////////Vexorian's Check Pathability////////////////////

function CheckPathabilityTrickGet takes nothing returns nothing
    set bj_rescueChangeColorUnit = bj_rescueChangeColorUnit or (GetEnumItem()!=bj_itemRandomCurrentPick)
endfunction

function CheckPathabilityTrick takes real x, real y returns boolean
    local integer i = 30
    local real X
    local real Y
    local rect r
    call SetItemPosition(It, x, y)
    set X = GetItemX(It) - x
    set Y = GetItemY(It) - y
    if X * X + Y * Y <= 100 then
        return true
    endif
    set r = Rect(x - i, y - i, x + i, y + i)
    set bj_rescueChangeColorUnit = false
    call EnumItemsInRect(r, null, function CheckPathabilityTrickGet)
    call RemoveRect(r)
    set r = null
    return bj_rescueChangeColorUnit
endfunction

function CheckPathability takes real x, real y returns boolean
    local boolean b = CheckPathabilityTrick(x,y)
    call SetItemVisible(It,false)
    return b
endfunction

///////////////////////////////////////////////////////////////////

private struct Knockback
    unit target
    real d1
    real d2
    real sin
    real cos
    real r
    string efect
endstruct

function Knockback_TreeFilter takes nothing returns boolean
    local integer d = GetDestructableTypeId(GetFilterDestructable())
    return d == 'ATtr' or d == 'BTtw' or d == 'KTtw' or d == 'YTft' or d == 'JTct' or d == 'YTst' or d == 'YTct' or d == 'YTwt' or d == 'JTwt' or d == 'JTwt' or d == 'FTtw' or d == 'CTtr' or d == 'ITtw' or d == 'NTtw' or d == 'OTtw' or d == 'ZTtw' or d == 'WTst' or d == 'LTlt' or d == 'GTsh' or d == 'Xtlt' or d == 'WTtw' or d == 'Attc' or d == 'BTtc' or d == 'CTtc' or d == 'ITtc' or d == 'NTtc' or d == 'ZTtc'
endfunction

function Knockback_KillTree takes nothing returns nothing
    call KillDestructable(GetEnumDestructable())
endfunction

globals
    timer Timer = CreateTimer()
    boolexpr filter
    Knockback array ar
    integer Total = 0
    constant real Interval = 0.04
endglobals

private function Loop takes nothing returns nothing
    local Knockback knock
    local integer i = 0
    local real x
    local real y
    local rect r
    loop
        exitwhen i >= Total
        set knock = ar[i]
        set x = GetUnitX(knock.target) + knock.d1 * knock.cos
        set y = GetUnitY(knock.target) + knock.d1 * knock.sin
        if knock.r != 0 then
            set r = Rect(x - knock.r, y - knock.r, x + knock.r, y + knock.r)
            call EnumDestructablesInRect(r, filter, function Knockback_KillTree)
            call RemoveRect(r)
        endif
        if CheckPathability(x, y) then
            call SetUnitX(knock.target, x)
            call SetUnitY(knock.target, y)
            call DestroyEffect(AddSpecialEffect(knock.efect, x, y))
        endif
        set knock.d1 = knock.d1 - knock.d2
        if knock.d1 <= 0 or not CheckPathability(x, y) then
            set ar[i] = ar[Total - 1]
            set Total = Total - 1
            call PauseUnit(knock.target, false)
            call knock.destroy()
        endif
        set i = i + 1
    endloop
    if Total == 0 then
        call PauseTimer(Timer)
    endif

endfunction

function KnockbackUnit takes unit target, real distance, real angle, real duration, real radius, string efect returns nothing
    local Knockback kd = Knockback.create()
    local integer q = R2I(duration / Interval)
    set kd.target = target
    set kd.d1 = 2 * distance / (q + 1)
    set kd.d2 = kd.d1 / q
    set kd.cos = Cos(angle * bj_DEGTORAD)
    set kd.sin = Sin(angle * bj_DEGTORAD)
    set kd.r = radius
    set kd.efect = efect
    call PauseUnit(target, true)
    if Total == 0 then
        call TimerStart(Timer, Interval, true, function Loop)
    endif
    set ar[Total] = kd
    set Total = Total + 1
endfunction

private function Init takes nothing returns nothing
    set filter = Filter(function Knockback_TreeFilter)
endfunction

endscope
 
Level 9
Joined
Nov 28, 2008
Messages
704
JASS:
item It = CreateItem('ciri', 0, 0)

I'm not sure, but I think you have to put the create item in your init function. I am probably wrong on that, but if your code isnt working that might be a good way to start.

Really though, you shouldn't follow that tutorial. The variables are named poorly (you spelled "effect" wrong), and more vJassy ways of handling things could really make the code easier to follow.

And don't get me started on Vex's check pathability.

JASS:
return bj_rescueChangeColorUnit
? Really. That could also be your problem, as my JNGP says that isn't a real constant. Change it to true or something.

Random creation of rects? Use a global one and the move rect functioon.

This concludes my rant.

Do explain more why it isnt working if you still cant figure it out. Is it just not doing anything? Is it randomly flinging your unit somewhere?
 
Level 8
Joined
Jul 28, 2008
Messages
211
It's not doing anything. I haven't checked the CheckPathability becouse it's a system for itself and I'm doing the knockback not CheckPathadility.

you spelled "effect" wrong
Yea, I know. I did it cuz I don't think it would work if I spelled it effect cuz effect is a variable type.

Really though, you shouldn't follow that tutorial.
Noticed that when I finished the system.

The variables are named poorly
One more reason why shouldn't I follow the tutorial.

EDIT: Moved
JASS:
It = CreateItem('ciri', 0, 0)
to Init and changed
JASS:
return bj_rescueChangeColorUnit
to
JASS:
return true
and it works now. Thanks.

EDIT 2: Returned
JASS:
return true
to
JASS:
return bj_rescueChangeColorUnit
cuz CheckPathability didn't work. Spell still works. Also made R a global rect.
Thanks alot, I always make little mistakes which ruin the whole spell.
 
Status
Not open for further replies.
Top