IsElementCollidableWith: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Added example)
Line 1: Line 1:
{{Client function}}
{{Client function}}
{{Needs Example}}
__NOTOC__  
__NOTOC__  
This function can be used to check whether specified element is collidable with another element.<br>
This function can be used to check whether specified element is collidable with another element.<br>
Line 22: Line 21:


==Example==  
==Example==  
This example adds the command ''togglecol'' which toggles the collision between the player and ped.
<section name="Client" class="client" show="true">
<section name="Client" class="client" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- TODO
local tPed = {}
addEventHandler( "onClientPlayerSpawn", localPlayer,
    function()
        local x, y, z = getElementPosition(source)
        if isElement(tPed["thePed"]) then
            destroyElement(tPed["thePed"])
        end
        -- Creates a random ped near player
        tPed["thePed"] = createPed(math.random(209, 238), x+1, y+1, z)
    end
)
 
function toggleColisionFunc()
    if not isElement(tPed["thePed"]) then
        return
    end
    -- Is the local player collidable with the ped?
    local isCollidable = isElementCollidableWith( localPlayer, tPed["thePed"] )
    -- Toggles the colision with the ped.
    setElementCollidableWith( localPlayer, tPed["thePed"], not isCollidable )
end
-- Adds a command handler to enable/disable colisions with the ped
addCommandHandler("togglecol", toggleColisionFunc)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>

Revision as of 07:01, 19 March 2014

This function can be used to check whether specified element is collidable with another element.
Note: You can only use this function with the element types listed below.

Syntax

bool isElementCollidableWith ( element theElement, element withElement ) 

Required Arguments

  • theElement: The element which colliding you want to get
  • withElement: The other element which colliding with the first entity you want to get

Returns

Returns true if the elements collide with eachother, false otherwise.

Example

This example adds the command togglecol which toggles the collision between the player and ped.

Click to collapse [-]
Client
local tPed = {}
addEventHandler( "onClientPlayerSpawn", localPlayer,
    function()
        local x, y, z = getElementPosition(source)
        if isElement(tPed["thePed"]) then
            destroyElement(tPed["thePed"])
        end
        -- Creates a random ped near player
        tPed["thePed"] = createPed(math.random(209, 238), x+1, y+1, z)
    end
)

function toggleColisionFunc()
    if not isElement(tPed["thePed"]) then
        return
    end
    -- Is the local player collidable with the ped?
    local isCollidable = isElementCollidableWith( localPlayer, tPed["thePed"] )
    -- Toggles the colision with the ped.
    setElementCollidableWith( localPlayer, tPed["thePed"], not isCollidable )
end
-- Adds a command handler to enable/disable colisions with the ped
addCommandHandler("togglecol", toggleColisionFunc)

See Also