Context Menu
Comment étendre et utiliser le menu contextuel de Sandbox.

Le menu contextuel est un menu à l'intérieur du mode de jeu Sandbox qui fournit des paramètres d'exécution tels que le réglage de la lumière du soleil ou le respawning. Il est accédé par la touche C par défaut et est activé/éteint en appuyant dessus.
Il est possible d'ajouter/supprimer des paramètres du menu contextuel directement via Lua !
Vous pouvez voir le code source du menu contextuel dans le dépôt GitHub de Sandbox.
Ajouter de nouvelles entrées
-- Adds new items to the Context Menu
-- It can be called multiple times with the same ID to append more items to the same category
---@param id string Unique ID used to identify this category
---@param title string Category title
---@param items table Table of items
function Sandbox.ContextMenu.AddItems(id, title, items)
The items parameter is a list of tables with the format for each type:
checkbox
{
id = "id-of-item",
type = "checkbox",
label = "my check box",
callback = function(value)
Console.Log("Checkbox value changed to: " .. value)
end
},
range
{
id = "id-of-item",
type = "range",
label = "slide me!",
min = 0,
max = 1440,
auto_update_label = false,
value = 720,
callback = function(value)
Console.Log("Range value changed to: " .. value)
end
},
button
{
id = "id-of-item",
type = "button",
label = "press me",
callback = function()
Console.Log("Button pressed!")
end
},
select_image
{
id = "id-of-item",
type = "select_image",
label = "select the image",
options = {
{ id = "id-opt-01", name = "Opt 01", image = "package://your-package/01.jpg" },
{ id = "id-opt-02", name = "Opt 02", image = "package://your-package/02.jpg" },
},
value = function()
return "id-opt-1"
end,
callback = function(value)
Console.Log("Select Image changed to: " .. value)
end
}
select
{
id = "id-of-item",
type = "select",
label = "select the option",
options = {
{ id = "id-opt-01", name = "Opt 01" },
{ id = "id-opt-02", name = "Opt 02" },
},
value = "id-opt-1",
callback = function(value)
Console.Log("Select changed to: " .. value)
end
}
color
{
id = "id-of-item",
type = "color",
label = "select the color",
value = function()
return my_current_color:ToHex(false)
end,
callback = function(value)
Console.Log("Color changed to: " .. value)
end
},
The value element can be either a direct value, or a function to be dynamically fetched when the entry is added to the Context Menu!
Supprimer des entrées
-- Removes the items from the Context Menu
---@param id string Unique ID used to identify this category
function Sandbox.ContextMenu.RemoveItems(id)
Adding Entries to an Entity/Tool Gun
Custom Entities and Base Pickable (e.g. Weapon) have the ability to have custom Context Menu entries shown automatically when they are selected or picked up.
When selecting an Entity
To add entries when an entity is selected, you need to define the selected_context_menu_items property on the Class of the Entity, with the same format as the items parameter of the AddItems function.
You can use the current selected entity set in Sandbox.ContextMenu.selected_entity to dynamically get/set values related to the selected entity in the Context Menu entries, for example:
MyEntityClass.selected_context_menu_items = {
{
id = "my_entity_input",
type = "text",
label = "text",
placeholder = "enter the text",
callback = function(value)
Sandbox.ContextMenu.selected_entity:CallRemoteEvent("SetMyValue", value)
end,
value = function()
return Sandbox.ContextMenu.selected_entity:GetValue("MyValue")
end,
}
}
When picking up an Entity
You can also define custom entries for Base Pickable classes, that will automatically show when picking up them. To do that, you need to define the picked_context_menu_items property on the Class of the Pickable:
MyGun.picked_context_menu_items = {
{
id = "my_gun_value",
type = "range",
label = "my value",
min = 0,
max = 100,
auto_update_label = true,
callback = function(value)
MyGun.my_value = value
end,
value = function()
return MyGun.my_value
end
}
}
Opening/Closing
-- Opens the Context Menu
function Sandbox.ContextMenu.Open()
-- Closes the Context Menu
function Sandbox.ContextMenu.Close()
Examples
You can find examples and how it works deeply at the ContextMenu.lua file.
Also, you can see a working example of dynamically adding an entry to the Context Menu in the BallonGun.