Is there a way to speed this function

RangerX

I am justice!
Reaction score
69
i need to make this function run faster is there any way ?

JASS:
function SlideUnit takes unit u , location startloc , location endloc , real speed returns nothing
local real dx = GetLocationX(endloc) - GetLocationX(startloc)
local real dy = GetLocationY(endloc) - GetLocationY(startloc)
local real x
local real y
local real distreal
local real anglereal
local location a = startloc
local location b = endloc
local location c = GetUnitLoc(u)
set distreal = SquareRoot(dx * dx + dy * dy)
set anglereal = AngleBetweenPoints(a,b)
loop
    exitwhen distreal <= 50
    set x = GetLocationX(c) + 25 * Cos(anglereal * bj_DEGTORAD)
    set y = GetLocationY(c) + 25 * Sin(anglereal * bj_DEGTORAD)
    call SetUnitX(u,x)
    call SetUnitY(u,y)
    call SetUnitFacing(u,anglereal)
    set c = GetUnitLoc(u)
    set dx = GetLocationX(b)- GetLocationX(c)
    set dy = GetLocationY(b)- GetLocationY(c)
    set distreal = SquareRoot(dx * dx + dy * dy)
    set anglereal = AngleBetweenPoints(c,b)
    call TriggerSleepAction(speed)
    
endloop
endfunction


i used this custom script to run the function

Trigger:
  • Custom script: call SlideUnit(bj_lastCreatedUnit,udg_Temp_Loc,udg_Temp_Loc2,0.01)


but it's run very slow :|
it's just sliding the unit to a point :| ...
 

Jedi

New Member
Reaction score
63
That is because TriggerSleepAction has problems with low numbers(0.00 - 0.30, even 0.00 will run slowly).You should use timer instead.
 

Dinowc

don't expect anything, prepare for everything
Reaction score
223
and avoid using locations
they do nothing but converting 2 reals into 1 handle...
they shouldn't even be called natives :nuts:

also you don't need to recalculate reals that have the same value during the whole instance
for example:
set anglereal = AngleBetweenPoints(c,b)
this will always be the same angle as long as your unit doesn't steer during the slide
so put it outside the loop
 

RangerX

I am justice!
Reaction score
69
That is because TriggerSleepAction has problems with low numbers(0.00 - 0.30, even 0.00 will run slowly).You should use timer instead.

how can you please explain how can i use timer as an wait command ?
 

Ayanami

칼리
Reaction score
288
You could use a periodic timer, however, it would be difficult to transfer your data such as your unit, etc, if a global variable is used. Thus, I recommend using Structs and Key Timers 2.

JASS:
library Slide

struct Slide
    unit u
    real speed
    real angle
    real dist
endstruct

private function SlideUnit takes nothing returns boolean
    local Slide d = KT_GetData()
    if d.dist <= 0 then
        set d.u = null
        call d.destroy()
        return true
    endif
    call SetUnitX(d.u, GetUnitX(d.u) + d.speed * Cos(d.angle))
    call SetUnitY(d.u, GetUnitY(d.u) + d.speed * Sin(d.angle))
    set d.dist = d.dist - d.speed
    return false
endfunction

public function Register takes unit u, location endloc, real interval, real offset returns nothing
    local Slide d = Slide.create()
    local real x = GetUnitX(u)
    local real y = GetUnitY(u)
    local real lx = GetLocationX(endloc)
    local real ly = GetLocationY(endloc)
    local real dx = x - lx
    local real dy = y - ly
    set d.u = u
    set d.dist = SquareRoot(dx * dx + dy * dy)
    set d.angle = Atan2(ly - y, lx - x)
    set d.speed = offset
    call KT_Add(function SlideUnit, d, interval)
endfunction

endlibrary


And to call the function, it'll be:

Trigger:
  • Actions
    • Custom script: call Slide_Register(MyUnit, MyLoc, MyInterval, MyOffset)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top