Jump to content

[Question] isElementMoving


Backsage

Recommended Posts

Ok, well the thing is I've already added the function source in my code and I'm trying to detect if an object is moving from moveObject, but isElementMoving doesn't return anything from the object. You can try it yourself and you will see the state always returning idling.

  
function isElementMoving(theElement) 
    if isElement(theElement)then --First check if the given argument is an element 
        local x, y, z = getElementVelocity(theElement) --Get the velocity of the element given in our argument 
        if x == 0 and y == 0 and z == 0 then --When there is no movement on X, Y or Z return false because our element is not moving 
            return false 
        else 
            return true 
        end 
    end 
end 
  
local screenWidth, screenHeight = guiGetScreenSize () -- Get the screen resolution (width and height) 
  
function idleCheck () 
    local state   = "Unknown" 
    local element = getPedOccupiedVehicle ( localPlayer ) or localPlayer or object1 
  
    -- Check whether the player is moving or not. 
    if isElementMoving ( element ) then 
        state = "moving" 
    else 
        state = "idling" 
    end 
  
    -- Write our state string to the lower left corner of the screen 
    dxDrawText ( "You are " .. state .. "!", 40, screenHeight - 40, screenWidth, screenHeight, tocolor ( 255, 255, 255, 255 ), 1, "default" ) 
end 
  
-- Keep the text visible with onClientRender. 
addEventHandler ( "onClientRender", root, idleCheck ) 
  
addCommandHandler("test2", 
    function() 
        local x, y, z = getElementPosition(localPlayer) 
        object1 = createObject(1318, x, y, z) 
        attachElements(localPlayer, object1) 
        moveObject(object1, 20000, 100, y, z) 
    end 
) 
  

Link to comment
  • Moderators

objects that move with the moveObject function, do not have any velocity.

They simply get set at position every frame.

If you use the moveObject function, you should already know if it is moving or not, since you set the duration.

local movingData_ = movingData 
function movingData (...) 
    local movingData = {...} 
    moveObject(unpack(movingData_)) 
    setElementData(movingData[1], "moveObjectEndTime", getTickCount() + movingData[2], false) 
 end 

if  getElementData(object,"moveObjectEndTime") > getTickCount() then  
    outputChatBox("object is moving") 
else 
    outputChatBox("object is NOT moving")     
end 

Link to comment

Client side:

  
addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
        object = createObject(1318, 0, 0, 4) 
        moveObject(object, 5000, 0, 0, 20)   
    end 
) 
  
  
  
local movingData_ = movingData 
    function movingData (...) 
        local movingData = {...} 
        moveObject(unpack(movingData_)) 
        setElementData(movingData[1], "moveObjectEndTime", getTickCount() + movingData[2], false) 
     end 
      
      
local screenWidth, screenHeight = guiGetScreenSize () -- Get the screen resolution  
  
function thing() 
    if  getElementData(object,"moveObjectEndTime") > getTickCount() then 
        state = "moving" 
    else 
        state = "idling" 
    end 
  
    -- Write our state string to the lower left corner of the screen 
    dxDrawText ( "You are " .. state .. "!", 40, screenHeight - 40, screenWidth, screenHeight, tocolor ( 255, 255, 255, 255 ), 1, "default" ) 
end 
addEventHandler("onClientRender", root, thing) 
  

[2016-03-22 17:49:59] ERROR: test2\client3.lua:21: attempt to compare number with boolean [DUP x113]

[2016-03-22 17:49:59] ERROR: test2\client3.lua:21: attempt to compare number with boolean

Edit: I seem to have fixed the issue. The problem was the data was not being properly passed to the function. Thank you IIYAMA for your help. Now I know it's possible to detect if a object is moving from moveObject. Client side:

  
addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
        object = createObject(1318, 0, 0, 4) 
        time = 5000 
        local x, y, z = 0, 0, 20 
        moveObject(object, time, x, y, z) 
        --local table1 = {object, time, x, y, z} 
        --outputChatBox(tostring(unpack(table1))) 
        movingData(object, time, x, y, z) 
    end 
) 
  
  
  
  
    function movingData (...) 
        movingData1 = {...} 
        --moveObject(unpack(movingData1)) 
        outputChatBox(tostring(unpack(movingData1))) 
        setElementData(movingData1[1], "moveObjectEndTime", getTickCount() + movingData1[2], false) 
     end 
      
addCommandHandler("test1", 
    function()    
        if getElementData(movingData1[1], "moveObjectEndTime") then 
            outputChatBox(tostring((getElementData(movingData1[1], "moveObjectEndTime")))) 
        elseif not getElementData(movingData1[1], "moveObjectEndTime") then 
            outputChatBox("Not properly set") 
        end   
    end 
) 
      
local screenWidth, screenHeight = guiGetScreenSize () -- Get the screen resolution  
  
function thing() 
    if  getElementData(object,"moveObjectEndTime") > getTickCount() then 
        state = "moving" 
    else 
        state = "idling" 
    end 
  
    dxDrawText ( "You are " .. state .. "!", 40, screenHeight - 40, screenWidth, screenHeight, tocolor ( 255, 255, 255, 255 ), 1, "default" ) 
end 
addEventHandler("onClientRender", root, thing) 
  

Link to comment
  • Moderators

Nice!

Btw, it had to be:

    local moveObject_ = moveObject 
    function moveObject (...) 
        local movingData = {...} 
        moveObject_(unpack(movingData_)) 
        setElementData(movingData[1], "moveObjectEndTime", getTickCount() + movingData[2], false) 
     end 

-_-", my wrapper fails haha

Link to comment

That doesn't seem to work because unpack expects a table, not a function. But in this case, it said it was nil. Not sure why didn't say "got function". Client side:

addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
        --object = createObject(1318, 0, 0, 4) 
        --moveObject(object, 5000, 0, 0, 20) 
        object = createObject(1318, 0, 0, 4) 
        time = 5000 
        local x, y, z = 0, 0, 20 
        moveObject(object, time, x, y, z) 
        --local table1 = {object, time, x, y, z} 
        --outputChatBox(tostring(unpack(table1))) 
        movingData() 
    end 
) 
  
    local moveObject_ = moveObject 
    function moveObject (...) 
        movingData = {...} 
        moveObject_(unpack(movingData_)) 
        setElementData(movingData[1], "moveObjectEndTime", getTickCount() + movingData[2], false) 
     end 
      
addCommandHandler("test1", 
    function() 
         
        if getElementData(movingData1[1], "moveObjectEndTime") then 
            outputChatBox(tostring((getElementData(movingData1[1], "moveObjectEndTime")))) 
        elseif not getElementData(movingData1[1], "moveObjectEndTime") then 
            outputChatBox("Not properly set") 
        end 
         
    end 
) 
      
local screenWidth, screenHeight = guiGetScreenSize () -- Get the screen resolution  
  
function thing() 
    if  getElementData(movingData[1],"moveObjectEndTime") > getTickCount() then 
        state = "moving" 
    else 
        state = "idling" 
    end 
  
    dxDrawText ( "You are " .. state .. "!", 40, screenHeight - 40, screenWidth, screenHeight, tocolor ( 255, 255, 255, 255 ), 1, "default" ) 
end 
addEventHandler("onClientRender", root, thing) 

[2016-03-23 08:29:56] ERROR: test2\client3.lua:18: bad argument #1 to 'unpack' (table expected, got nil)

[2016-03-23 08:29:56] ERROR: test2\client3.lua:37: attempt to compare number with boolean

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...