HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

How to calculate if a unit is moving?

03-07-2007, 03:02 AM#1
Tiku
im trying to find out a way to check if a unit is moving or not... im prety sure its possible.. but i dont know how... does any1 have a script on them that checks if the unit moves?
03-07-2007, 03:31 AM#2
wantok
It's not particularly hard, you just have to compare the unit's current position to its position at some arbitrary time in the past. I made a system that does it for you. You can find it here.
03-07-2007, 03:50 AM#3
Tiku
alright well thanks =]
+ rep
03-07-2007, 04:05 AM#4
Pyrogasm
His way is the best way to do it, though you can just do a check with the minium wait time (use [ljass]TriggerSleepAction(0.00)[/lljass] and see if the unit's X/Y values have changed.

You can see this method utilized in this failed thread of mine. The system mentioned there is quite beyond what the code there looks like, since it was posted a while ago, but I'm still using the same movement check. I haven't had any problems with it... but then again, I haven't been testing things with blink.

EDIT: Wantok, your system requires vJass to use, correct?
03-07-2007, 04:09 AM#5
wantok
Yeah it does, although it probably wouldn't be that hard to rewrite it just using normal arrays.
03-07-2007, 04:13 AM#6
Tiku
well ill try to find whats easier, and maybe if "get more pro" (sorry for poor grammar) then ill use regular functions but yah thanks for the thread pyro! =] and for the system wantok! =]
03-07-2007, 04:19 AM#7
wantok
You're welcome.
03-07-2007, 04:24 AM#8
Pyrogasm
wankok, how sound is the system? Will it misfire or return an incorrect value ever/often? (Sorry I'm not asking in your thread)

If so, and you'd like me to be endebted to you, I would really appreciate a version of the system that doesn't require vJass and is capable of running without using Custom Values for said Movement System I am in the process of devising.

My way works, but I'm sure it will misfire, and I could use a sound system.
03-07-2007, 04:37 AM#9
wantok
I haven't tested it extensively, but assuming you set the timer period short enough, it should always return correctly unless you call the function on a unit not in the array.

I'm looking into making one that doesn't use vjass. It's so annoying to have to add global variables manually.
03-07-2007, 04:44 AM#10
Pyrogasm
My timer runs ever 0.20 seconds, checking each unit individually with a TriggerSleepAction(0.00) from a function called via ExecuteFunc(), and then does its actions regarding movement speed from there.

So, mine's as low as it can go ><
03-07-2007, 05:43 AM#11
wantok
Ok, I changed a few of the functions to work without vJass. I'll probably do the other functions later. To be honest, I don't know if this system is the best way to check movement for what you want, since it is constantly updating all the units positions. It was made more to find out if a unit is moving at some random time without having to use a wait or start a timer then. But I've attached it. I haven't tested it at all so it may need tweaking, but it doesnt have any errors when i save it.

It uses gamecache instead of custom values. You could use custom values though instead. Just make an integer array for whatever you want the custom value to hold.

Edit: There is a problem right now in the timer callback function. When it finds a unit == null and moves all the array values, the new index value is not updated on the moved unit. I'll fix it later.

Edit 2: I coded something much more specialized for your particular needs. Try this instead. You'll have to make the required globals of course. You wont have to do anything with unit groups anymore. Just add the unit to the array and let it do its thing. Also you no longer have to use the unit's custom value.
Collapse JASS:
globals
    integer udg_IUM_total = 1
    integer array udg_Fatigue //replaces custom value
    unit array udg_Unit
    real array udg_Xpos
    real array udg_Ypos
    timer udg_Timer1 = CreateTimer()
    timer udg_Timer2 = CreateTimer()
endglobals

function Timer2_callback takes nothing returns nothing
    local integer i = 1
    local real x
    local real y
    loop
        exitwhen i == udg_IUM_total
        if udg_Unit[i] == null then
            set udg_Unit[i] = udg_Unit[udg_IUM_total - 1]
            set udg_Fatigue[i] = udg_Fatigue[udg_IUM_total -1]
            set udg_Xpos[i] = udg_Xpos[udg_IUM_total - 1]
            set udg_Ypos[i] = udg_Ypos[udg_IUM_total - 1]
            set udg_IUM_total = udg_IUM_total-1
        else
            set x = GetUnitX(udg_Unit[i])
            set y = GetUnitY(udg_Unit[i])
            if udg_Xpos[i]== x and udg_Ypos[i] == y then
                //do actions for not moving
            else
                //do actions for moving
            endif
            set i = i + 1
        endif
    endloop
endfunction

function Timer1_callback takes nothing returns nothing
    local integer i = 1
    loop
        exitwhen i == udg_IUM_total
        if udg_Unit[i] == null then
            set udg_Unit[i] = udg_Unit[udg_IUM_total - 1]
            set udg_Fatigue[i] = udg_Fatigue[udg_IUM_total -1]
            set udg_Xpos[i] = udg_Xpos[udg_IUM_total - 1]
            set udg_Ypos[i] = udg_Ypos[udg_IUM_total - 1]
            set udg_IUM_total = udg_IUM_total-1
        else
            set udg_Xpos[i] = GetUnitX(udg_Unit[i])
            set udg_Ypos[i] = GetUnitY(udg_Unit[i])
            set i = i + 1
        endif
    endloop
    if i > 1 then
        call TimerStart(udg_Timer2, .04, FALSE, function Timer2_callback)
    endif
    if i == 1 then
        call PauseTimer(udg_Timer1)
    endif
endfunction

function EnableMoveCheck takes unit u returns nothing
    local integer i = udg_IUM_total
    set udg_Unit[i] = u
    set udg_Xpos[i] = GetUnitX(u)
    set udg_Ypos[i] = GetUnitY(u)
    set udg_Fatigue[i] = //whatever value you initialize to the custom value
    if i == 1 then
        call TimerStart(udg_Timer1, .2, TRUE, function Timer1_callback)
    endif
    set udg_IUM_total = i + 1
endfunction
Attached Files
File type: w3xIUM no vjass.w3x (17.1 KB)