OnElementClicked: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Replaced spaces with tabs to follow Wiki conventions)
Line 48: Line 48:
     end
     end
end
end
addEventHandler( "onElementClicked", getRootElement( ), elementClicked ) -- Add the event handler
addEventHandler( "onElementClicked", getRootElement( ), repairClickedVehicle ) -- Add the event handler
</syntaxhighlight>
</syntaxhighlight>
{{See also/Server event|Element events}}
{{See also/Server event|Element events}}

Revision as of 09:42, 12 August 2017

This event is triggered when an element is clicked on by the client. These events can only trigger when the client has its cursor enabled. It triggers for all three mousebuttons in both their up and down states.

Parameters

string mouseButton, string buttonState, player playerWhoClicked, float clickPosX, float clickPosY, float clickPosZ
  • mouseButton: A string representing the mousebutton that was clicked. This might be left, middle or right.
  • buttonState: A string representing what state the button clicked is in. This might be up or down.
  • playerWhoClicked: The player that clicked on the element
  • clickPosX: The X position in the world the player clicked at
  • clickPosY: The Y position in the world the player clicked at
  • clickPosZ: The Z position in the world the player clicked at

Source

The source of this event is the element that got clicked by the player.

Examples

This example prints type of the element you clicked to chatbox when you click it.

function elementClicked( theButton, theState, thePlayer )
    if theButton == "left" and theState == "down" then -- if left mouse button was pressed down
        outputChatBox( "You clicked " .. getElementType( source ), thePlayer ) -- print the element type to players chatbox
    end
end
addEventHandler( "onElementClicked", getRootElement(), elementClicked ) -- add a handler function for the event

This example check if the clicked element is a vehicle. If is, then repairs it.

function repairClickedVehicle( button, state, player ) -- Add the function
    if button == "left" and state == "down" then
        if getElementType( source ) == "vehicle" then -- If the clicked element is a vehicle...
            local x, y, z = getElementPosition( player )
            local x1, y1, z1 = getElementPosition( source ) 
            local distance = getDistanceBetweenPoints3D( x, y, z, x1, y1, z1 ) -- Some distance calculations
            if distance < 4 then -- Check if the player is near the vehicle
                if getElementHealth( source ) < 1000 then
                    fixVehicle( source )
                    outputChatBox( "You have repaired a "..getVehicleNameFromModel( getElementModel( source ) ), player, 0, 255, 0 )
                else
                    outputChatBox( "Vehicle is not damaged!", player, 255, 0, 0 )
                end
            end
        end
    end
end
addEventHandler( "onElementClicked", getRootElement( ), repairClickedVehicle ) -- Add the event handler

See Also

Element events


Event functions