Skip to content

Quick start

Kiminaze edited this page Apr 8, 2022 · 4 revisions

Adding your own options is quite easy once you get the gist of it.

Register a function to the menu

These lines are required and should not be changed.

-- save exports in a variable for easy access
local ECM = exports["ContextMenu"]

-- set up register function
ECM:Register(function(screenPosition, hitSomething, worldPosition, hitEntity, normalDirection)

end)

Adding items to the menu

local ECM = exports["ContextMenu"]

ECM:Register(function(screenPosition, hitSomething, worldPosition, hitEntity, normalDirection)
    -- adding the item
    local item = ECM:AddItem(0, "ItemText", function()
        -- what the item should do will go here
        print("Activated item!")
    end)
end)

Full example

While the above code is enough to have a new option, you might want to limit it to certain conditions. This is where all the variables from the registered function come into play.

For this example we check if the player is actually in a range of 10 meters from the interaction point.

local ECM = exports["ContextMenu"]

ECM:Register(function(screenPosition, hitSomething, worldPosition, hitEntity, normalDirection)
    -- get the player ped and its position
    local playerPed = PlayerPedId()
    local playerPosition = GetEntityCoords(playerPed)

    -- calculate the distance between the interaction point in the world and the player
    local distance = #(worldPosition - playerPosition)

    -- check if the player is too far away...
    if (distance > 10.0) then
        -- ...and stop execution of this function
        return
    end

    local item = ECM:AddItem(0, "ItemText", function()
       print("Activated item!")
    end)
end)

For more information, check the other tutorial sections!

Clone this wiki locally