Jump to content

[Question] onElementPositionChanged


Backsage

Recommended Posts

Is there any such event on the MTA wiki? Something like that might be useful. I wanna try to make a marker (and object) move up and down using moveObject and I wanna do something so that when the position has changed, I can do something else (e.g. move it down).

Link to comment

Well, if the you mean that the marker and object will be in the same position and you want them both to go up and down at the same time, you can simply use:

attachElements -- Attach the marker to the object. 

And once you've done that, you could just move the object using moveObject to whatever position you'd like, and then use setTimer so 1 second after the object & marker is completely moved to the position you'd like it to be in , then you would be able to do whatever you'd like to the object in a new function.

Hope this helped.

Link to comment
He wants the marker to move up and down, no there is no function for on object finish moving, but you can use a timer for it.

Erm, can you give an example? I understand to use timers, but I don't know what event to use. It would be cool if something like onElementPositionChanged existed though.

Link to comment

Try this:

_moveObject = moveObject 
function moveObject(element, time, x, y, z) 
    if isElement(element) then 
        local hasMoved = _moveObject ( element, time, x, y, z ) 
        if ( hasMoved ) then 
            triggerEvent("onElementPositionChanged", element, x, y, z) 
            return true 
        end 
    end 
end 
  
addEvent("onElementPositionChanged", true) 
addEventHandler("onElementPositionChanged", root, 
function(x, y, z) 
    local model = getElementType(source) -- source is the element that has changed his position 
    outputChatBox("The "..model.."'s position was changed to: "..x..", "..y..", "..z, root) 
end) 

moveObject(SOME_OBJECT, TIMER, X, Y, Z) 

Link to comment
Try this:
_moveObject = moveObject 
function moveObject(element, time, x, y, z) 
    if isElement(element) then 
        local hasMoved = _moveObject ( element, time, x, y, z ) 
        if ( hasMoved ) then 
            triggerEvent("onElementPositionChanged", element, x, y, z) 
            return true 
        end 
    end 
end 
  
addEvent("onElementPositionChanged", true) 
addEventHandler("onElementPositionChanged", root, 
function(x, y, z) 
    local model = getElementType(source) -- source is the element that has changed his position 
    outputChatBox("The "..model.."'s position was changed to: "..x..", "..y..", "..z, root) 
end) 

moveObject(SOME_OBJECT, TIMER, X, Y, Z) 

Kinda works, except that it says that z is a nil value.

[2016-03-20 01:03:02] ERROR: test2\client2.lua:65: attempt to concatenate local 'z' (a nil value)

If I remove z, it'll say "The object's position was changed to: 0, 2" and "The object's position was changed to: 0, 5" because 5 and 2 are the z positions I wanted the objects to move. It's not returning the actual x value. And I don't know why that function has to be called moveObject in order for it to work.

Edited by Guest
Link to comment
  
addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
        marker = createMarker(0, 0, 2, "arrow", 1, 255, 0, 6) 
        player1 = getPlayerFromName("[NSM]Backsage") 
        object = createObject(1318, 0, 0, 4) 
        attachElements(player1, object) 
        x, y, z = getElementPosition(marker) 
        setElementData(marker,"originalZ",z) 
        setElementAlpha(object, 0) 
        setTimer(function() destroyElement(object) end, 1000, 1) 
        moveObject(object, 1000, 0, 0, 5)    
    end 
) 
  
  
addEventHandler("onClientElementDestroy", resourceRoot, 
    function() 
        x, y, z = getElementPosition(object) 
        if z >= 4.8 then 
            object = createObject(1318, x, y, z) 
            setTimer(function() destroyElement(object) end, 1000, 1) 
            setElementAlpha(object, 0) 
            attachElements(player1, object) 
            moveObject(object, 1000, 0, 0, 2) 
        elseif z >= 2  then 
            object = createObject(1318, x, y, z) 
            setTimer(function() destroyElement(object) end, 1000, 1) 
            setElementAlpha(object, 0) 
            attachElements(player1, object) 
            moveObject(object, 1000, 0, 0, 5) 
        end 
    end 
) 
  
  
     
  
function escapeMe () 
  
    local x, y, z = getElementPosition(marker) 
    outputChatBox("" .. x .. ", " .. y .. ", " .. z) 
    if isElementWithinMarker(player1, marker) then 
        outputChatBox("Marker's moving") 
    end 
     
end     
addCommandHandler ( "test2", escapeMe ) 
  
 _moveObject = moveObject 
    function moveObject(element, time, x, y, z) 
        if isElement(element) then 
            local hasMoved = _moveObject ( element, time, x, y, z ) 
            if ( hasMoved ) then 
                triggerEvent("onElementPositionChanged", element, x, y, z) 
                return true 
            end 
        end 
    end 
      
    addEvent("onElementPositionChanged", true) 
    addEventHandler("onElementPositionChanged", root, 
    function(model, x, y, z) 
        local model = getElementType(source) -- source is the element that has changed his position 
        outputChatBox("The "..model.."'s position was changed to: "..x..", "..y..", ".. z) 
    end) 
  
  

Link to comment
    function(x, y, z) 
        local model = getElementType(source) -- source is the element that has changed his position 
        outputChatBox("The "..model.."'s position was changed to: "..x..", "..y..", ".. z) 
    end) 

Change last part of code to thus

Thanks man. Now it works with no problems.

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