• 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.

Pls, can you pass me a unit move detector?

Status
Not open for further replies.
Level 19
Joined
Oct 17, 2012
Messages
860
JASS:
globals
    unit footman
endglobals

function print takes string s returns nothing
    call DisplayTimedTextToPlayer(Player(0), 0, 0, 10, s)
endfunction

function Trig_Example_Actions takes nothing returns nothing
    local real x = GetRandomReal(0, 9999)
    local real y = GetRandomReal(0, 9999)
    if GetUnitCurrentOrder(footman) == 0 then
        call IssuePointOrder(footman, "attack", x, y)
    endif
endfunction

function CheckMovement takes nothing returns nothing
    if IsUnitMoving(footman) then
        call print(GetUnitName(footman) + " is moving.")
    else
        call print(GetUnitName(footman) + " is not moving.")
    endif
endfunction

//===========================================================================
function InitTrig_Example takes nothing returns nothing
    set footman = CreateUnit(Player(0), 'hfoo', 0, 0, 0)
 
    //Register unit to system
    call SensorAddUnit(footman)
 
    call TimerStart(CreateTimer(), 1.0, true, function CheckMovement)
    call TimerStart(CreateTimer(), 1.0, true, function Trig_Example_Actions)
endfunction
JASS:
globals
    unit footman
endglobals

function print takes string s returns nothing
    call DisplayTimedTextToPlayer(Player(0), 0, 0, 10, s)
endfunction

function Trig_Example_Actions takes nothing returns nothing
    local real x = GetRandomReal(0, 9999)
    local real y = GetRandomReal(0, 9999)
    if GetUnitCurrentOrder(footman) == 0 then
        call IssuePointOrder(footman, "attack", x, y)
    endif
endfunction

function OnStart takes nothing returns nothing
    call print(GetUnitName(footman) + " has started moving.")
endfunction

function OnStop takes nothing returns nothing
    call print(GetUnitName(footman) + " has stop moving.")
endfunction

function OnChange takes nothing returns nothing
    call print(GetUnitName(footman) + " is preparing to stop or start moving.")
endfunction

//===========================================================================
function InitTrig_Example takes nothing returns nothing
    set footman = CreateUnit(Player(0), 'hfoo', 0, 0, 0)
 
    //Register unit to system
    call SensorAddUnit(footman)
 
    //Trigger an event when unit starts moving
    call RegisterMotionStartEvent(function OnStart)
 
    //Trigger an event when unit stops moving
    call RegisterMotionStopEvent(function OnStop)
 
    //Trigger an event when unit starts or stops moving
    call RegisterMotionChangeEvent(function OnChange)
 
    call TimerStart(CreateTimer(), 1.0, true, function Trig_Example_Actions)
endfunction

Actually, you will not need the footman global variable since AGD provides one to retrieve the triggering unit during the events.
 
Last edited:
Level 24
Joined
Jun 26, 2020
Messages
1,928
JASS:
globals
    unit footman
endglobals

function print takes string s returns nothing
    call DisplayTimedTextToPlayer(Player(0), 0, 0, 10, s)
endfunction

function Trig_Example_Actions takes nothing returns nothing
    local real x = GetRandomReal(0, 9999)
    local real y = GetRandomReal(0, 9999)
    if GetUnitCurrentOrder(footman) == 0 then
        call IssuePointOrder(footman, "attack", x, y)
    endif
endfunction

function CheckMovement takes nothing returns nothing
    if IsUnitMoving(footman) then
        call print(GetUnitName(footman) + " is moving.")
    else
        call print(GetUnitName(footman) + " is not moving.")
    endif
endfunction

//===========================================================================
function InitTrig_Example takes nothing returns nothing
    set footman = CreateUnit(Player(0), 'hfoo', 0, 0, 0)
 
    //Register unit to system
    call SensorAddUnit(footman)
 
    call TimerStart(CreateTimer(), 1.0, true, function CheckMovement)
    call TimerStart(CreateTimer(), 1.0, true, function Trig_Example_Actions)
endfunction
JASS:
globals
    unit footman
endglobals

function print takes string s returns nothing
    call DisplayTimedTextToPlayer(Player(0), 0, 0, 10, s)
endfunction

function Trig_Example_Actions takes nothing returns nothing
    local real x = GetRandomReal(0, 9999)
    local real y = GetRandomReal(0, 9999)
    if GetUnitCurrentOrder(footman) == 0 then
        call IssuePointOrder(footman, "attack", x, y)
    endif
endfunction

function OnStart takes nothing returns nothing
    call print(GetUnitName(footman) + " has started moving.")
endfunction

function OnStop takes nothing returns nothing
    call print(GetUnitName(footman) + " has stop moving.")
endfunction

function OnChange takes nothing returns nothing
    call print(GetUnitName(footman) + " is preparing to stop or start moving.")
endfunction

//===========================================================================
function InitTrig_Example takes nothing returns nothing
    set footman = CreateUnit(Player(0), 'hfoo', 0, 0, 0)
 
    //Register unit to system
    call SensorAddUnit(footman)
 
    //Trigger an event when unit starts moving
    call RegisterMotionStartEvent(function OnStart)
 
    //Trigger an event when unit stops moving
    call RegisterMotionStopEvent(function OnStop)
 
    //Trigger an event when unit starts or stops moving
    call RegisterMotionChangeEvent(function OnChange)
 
    call TimerStart(CreateTimer(), 1.0, true, function Trig_Example_Actions)
endfunction

Actually, you will not need the footman global variable since AGD provides one to retrieve the triggering unit during the events.
I see.
 
Status
Not open for further replies.
Top