• 🏆 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!

IsUnitMoving()

Level 17
Joined
Jun 28, 2008
Messages
776
This is a little code snippet I created for a map I am creating. I could not find a working one anywhere.

Description :

This little piece of code checks if a unit is moving.

Requires :

  • JNGP


JASS:
library IsUnitMoving initializer init

globals 

    private group   CHECKUNITS  = CreateGroup()
    private trigger MAINTRIGGER = CreateTrigger()
    private integer counter     = 0
    MoveData array ARRMove

endglobals

struct MoveData

    unit witchunit = null
    real cX        = 0
    real cY        = 0
    real nX        = 0
    real nY        = 0
    boolean moving = false

endstruct

private function GetIndex takes unit u returns integer

    local integer i = 0
    
    loop
    
        set i = i + 1
        
            if ARRMove[i].witchunit == u then
                return i
            endif
        
        exitwhen i == counter
    
    endloop

    return 0
    
endfunction

function IsUnitMoving takes unit u returns boolean

    local integer i = GetIndex(u)

    return ARRMove[i].moving

endfunction

private function EnumUnits takes nothing returns nothing

    local integer i = GetIndex(GetEnumUnit())
    
    set ARRMove[i].cX = GetUnitX(GetEnumUnit())
    set ARRMove[i].cY = GetUnitY(GetEnumUnit())
    
    if (ARRMove[i].cX == ARRMove[i].nX) and (ARRMove[i].cY == ARRMove[i].nY) then
        set ARRMove[i].moving = false
    else
        set ARRMove[i].moving = true
    endif
    
    set ARRMove[i].nX = GetUnitX(GetEnumUnit())
    set ARRMove[i].nY = GetUnitY(GetEnumUnit())

endfunction

private function onLoop takes nothing returns nothing

    call ForGroup(CHECKUNITS, function EnumUnits)

endfunction

function AddUnit takes unit u returns nothing

    set counter = counter + 1
    set ARRMove[counter] = MoveData.create()
    set ARRMove[counter].witchunit = u
    set ARRMove[counter].cX = GetUnitX(u)
    set ARRMove[counter].cY = GetUnitY(u)
    
    call GroupAddUnit(CHECKUNITS, u)

endfunction

private function init takes nothing returns nothing

    call TriggerRegisterTimerEvent(MAINTRIGGER, 0.05, true)
    call TriggerAddAction(MAINTRIGGER, function onLoop)

endfunction

endlibrary

Use
JASS:
AddUnit(WichUnit)
to add a unit to the check list and use
JASS:
IsUnitMoving( unit WitchUnit)
to check if that unit is moving.
 
I'm sorry, this cannot be approved.
  • You use O(n) searches which are very slow and are limited to 8190 registered units.
  • Like Anachron said, you could just trigger order events.
  • The fact you even need to register units is lame.
  • You don't even have recycling.
  • You're calling GetEnumUnit() five times when you could just store it in a local.
  • "AddUnit" is a to generic name.
  • What's the point of using the timer event? Why not just use a timer?
  • The struct should be private.
  • You could inline IsUnitMoving.
 
Top