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

Status
Not open for further replies.
Level 15
Joined
Jan 16, 2008
Messages
1,244
I am making a spell for the hero contest #1 and i came up with a spell that knocks a unit back for a short distance. Here is my knockback code, it's kinda unefficent but i'm just a beginner. Could you tell me how to change minimum polledwait interval or something else to make the unit move smooter because now it looks as if the game is laggy, the unit moves too slow.
 
Level 15
Joined
Jan 16, 2008
Messages
1,244
Uh, sry i forgot, here it is:
JASS:
function rock_throw takes unit caster, unit target, real distperinterval returns nothing
    local location casterloc = GetUnitLoc(caster)
    local real facing = GetUnitFacing(caster)
    local real distance = DistanceBetweenPoints(GetUnitLoc(caster), GetUnitLoc(target))
    local integer loopcount = 0
    call PolledWait(1)
    loop
        set loopcount = loopcount + 1
    exitwhen GetUnitState(target, UNIT_STATE_LIFE) <= 0 or loopcount == 20
        set distance = distance + distperinterval
        call SetUnitPositionLoc(target, PolarProjectionBJ(casterloc, distance, facing))
        call PolledWait(0.1)
    endloop
    set casterloc = null
endfunction
Polled wait interval is 0.1 sec minimum and for smooth movement i need at least 0.05. Is there a way to reduce interval or maybe to do it without polled wait?
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Use a timer, not a loop.
Also its usually better to use coordinates (x,y) then points.

Here's like the crappiest/simplest knockback ever if you want (It uses Kattana's Local Handle Vars script):
JASS:
function knockbacktimer takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local unit whichUnit = GetHandleUnit(t, "whichUnit")
    local real knockback = GetHandleReal(t, "knockback")
    local real angle = GetHandleReal(t, "angle")
    local real a = angle * bj_DEGTORAD
    local real x = GetUnitX(whichUnit) + knockback * Cos(a)
    local real y = GetUnitY(whichUnit) + knockback * Sin(a)
    call SetUnitPosition(whichUnit, x, y)
    set knockback = knockback-0.5
    call SetHandleReal(t, "knockback", knockback)
    if knockback <= 0 then
        call FlushHandleLocals(t)
        call DestroyTimer(t)
    endif
    set t = null
    set whichUnit = null  
endfunction
function Knockback takes unit whichUnit, real knockback, real angle returns nothing
    local timer t = CreateTimer())
    call SetHandleHandle(t, "whichUnit", whichUnit)
    call SetHandleReal(t, "knockback", knockback)
    call SetHandleReal(t, "angle", angle)
    call TimerStart(t, 0.02, true, function knockbacktimer)
    set t = null
endfunction
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
By the way, check your leaks.
When you use a line like this for example: DistanceBetweenPoints(GetUnitLoc(caster), GetUnitLoc(target)), it leaks exacly like it does in GUI.

Thats why people usually prefer to use reals (coords) instead of points, reals don't leak.
Thats where you will want to create your own functions (such as distance between two points and angle between two points).

If you want this two functions here there are
JASS:
//****************************************************************************************
//               Distance between two points using coordinates
// call DistanceBetweenCoordinates(real x1, real y1, real x2, real y2)

function DistanceBetweenCoordinates takes real x1, real y1, real x2, real y2 returns real
    local real dx = x1 - x2
    local real dy = y1 - y2
    return SquareRoot(dx * dx + dy * dy)
endfunction

//****************************************************************************************
//               Angle between two points using coordinates
// call AngleBetweenCoordinates(real x1, real y1, real x2, real y2)

function AngleBetweenCoordinates takes real x1, real y1, real x2, real y2 returns real
    return bj_RADTODEG * Atan2(y2 - y1, x2 - x1)
endfunction
 
Level 15
Joined
Jan 16, 2008
Messages
1,244
Thank you for the warning, i didn't know that such things leak. As for mathematical functions, i'm studying physics and maths, pitagorean theorem will be no problem. I also made a spell for that hero called terraform. It's channeling with earthquake as base spell. Lasts 8 seconds during which, all enemy units in the area get bound to the ground. The spell also destroys the ground(ripples), seeds trees and roots. Now when i tried to make effects spawn using a loop(eg. i tried to create 4 trees at random locations inside the area), nothing would appear so i had to make all effects linear and without loops. Do you know why loops would cause problems? And is it possible to change tileset using jass? I found no function for that, only blighting.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
1. Loops shouldn't do any problems.
You probably wrote the string (effect name) like this "*\*\*.mdx" right ?
It should be "*\\*\\*.mdx" with two slashes every time. Took me hours to find that out >.<

[Edit] oh wait... did it work without the loop ?


2. native SetTerrainType takes real x, real y, integer terrainType, integer variation, integer area, integer shape returns nothing

Function list is your friend :gg:

By the way, if you do not know how the syntax for those things go, check it in the GUI action.
Its located in Enviourment actions.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
No but just put a terrain type in the GUI action and convert to Custom Script.

Example:
  • Environment - Change terrain type at (Center of (Playable map area)) to Lordaeron Summer - Dirt using variation -1 in an area of size 1 and shape Circle
Turns into

call SetTerrainTypeBJ( GetRectCenter(GetPlayableMapRect()), 'Ldrt', -1, 1, 0 )
 
Level 19
Joined
Aug 24, 2007
Messages
2,888
Creating an Handle Attachment System is very easy if you not supposed to use systems that didnt made by you

Creating an efficient wait is hard (someone in wc3c made one)
 
Status
Not open for further replies.
Top