Fonctionnalités supplémentaires
Quelques fonctionnalités et concepts supplémentaires du mode de jeu Sandbox
Le mode de jeu Sandbox fournit aussi plusieurs autres sous-systèmes utilisables et extensibles par script, comme :
WebUI du Sandbox
Vous pouvez accéder directement à la WebUI du Sandbox via la variable Sandbox.HUD.
Tutoriels

L'interface des tutoriels peut afficher un titre, une courte description et des touches. Le système de Tool Gun l'utilise automatiquement pour afficher les instructions de l'outil équipé.
Vous pouvez aussi l'utiliser dans vos propres packages pour donner des instructions aux joueurs :
-- Shows the Tutorials UI with a title, a description and a list of keys
-- Each key can be a Key Binding name (e.g. "Fire") or a raw key name (e.g. "E")
Sandbox.Tutorials.Show("My Tool", "Helps you build awesome things", {
{ key = "Fire", text = "Spawn a block" },
{ key = "E", text = "Rotate the block" },
})
-- Hides the Tutorials UI
Sandbox.Tutorials.Hide()
SpawnHistory

C'est un système côté serveur qui garde la trace de tous les éléments que chaque joueur a fait apparaître. Il ajoute aussi des informations aux éléments apparus, pour que vous puissiez savoir qui a fait apparaître un élément (et quand).
Vous pouvez appeler ces fonctions côté serveur pour ajouter un élément à l'historique d'un joueur, qui pourra alors l'annuler en appuyant sur X.
-- Adds an item to the history of a player
Sandbox.SpawnHistory.AddItemToHistory(player, my_prop)
-- Destroys the item at the given position in the history of a player (the last one if index is nil)
-- Returns true if an item was destroyed, otherwise returns false
local destroyed = Sandbox.SpawnHistory.DeleteItemFromHistory(player, nil)
-- Updates an item ownership data
-- I.e. in cases an item gets modified and we need to update the new owner
Sandbox.SpawnHistory.UpdateItemOwnership(other_player, my_prop)
Appeler Sandbox.SpawnHistory.AddItemToHistory() définit sur l'entité une valeur "SpawnedBy", synchronisée avec tous les clients, qui contient les données suivantes :
{
player_name = "...", -- Player name
player_steam_id = "...", -- Player Steam ID
time = 123, -- Time that it was spawned (Server.GetTime())
}
Notifications
Même si le jeu intègre déjà un système de notifications (Client.ShowNotification()), le Sandbox en propose un plus avancé, qui permet d'afficher des notifications façon "tutoriel" qui n'apparaissent qu'une seule fois :
-- Adds a Notification on the Screen. Parameters:
-- type NotificationType Type of the notification to display
-- id string Unique ID used to store if the notification was already displayed to the player
-- message string The message to display
-- duration number Duration in seconds of the notification
-- delay number Time in seconds to wait until display the notification
-- force boolean? To force it to be displayed regardless if it was already displayed before
Sandbox.Notifications.Add(NotificationType.Info, "my_package_welcome", "Welcome! Press Q to open the Spawn Menu", 5, 2)
PersistentConfig
Nous avons créé un système qui aide à conserver les réglages des Tool Guns (ou de tout autre élément personnalisé). Nos Tool Guns l'utilisent pour conserver d'une session à l'autre les réglages modifiés dans le menu contextuel :
-- Saves a config for a class
Sandbox.PersistentConfigSystem.SaveConfig("MyToolGun", "power", 50)
-- Gets a config of a class
local power = Sandbox.PersistentConfigSystem.GetConfig("MyToolGun", "power")
EntityInput
C'est un système utilisé par nos entités pour associer des touches personnalisées à des actions sur les entités :
-- Registers a Key Binding (players can remap it in the settings)
Input.Register("MyEntityActivate", "E")
-- Adds an input binding to an entity, using the Key Binding name (callbacks receive the entity)
Sandbox.EntityInputSystem.AddEntityBinding(my_entity, "activate", "MyEntityActivate", function(entity) end, function(entity) end)
-- Removes an input binding from an entity
Sandbox.EntityInputSystem.RemoveEntityBinding(my_entity, "activate")
En interne, il suffit de définir input_bindings sur la classe d'entité voulue : quand l'entité est sélectionnée, le menu contextuel propose alors les touches à associer. Par exemple, dans l'entité Thruster du Sandbox (une classe héritée de Prop) :
-- Input Bindings for this Entity
Thruster.input_bindings = {
{
label = "activate / deactivate",
callback_pressed = function(entity)
entity:CallRemoteEvent("SetActive", Reliability.Reliable, true)
end,
callback_released = function(entity)
entity:CallRemoteEvent("SetActive", Reliability.Reliable, false)
end,
},
{
label = "toggle",
callback_pressed = function(entity)
entity:CallRemoteEvent("SetActive", Reliability.Reliable, not entity:GetValue("Active"))
end,
},
}