Passer au contenu principal
Version: latest - a1.64.x ⚖️

Valeurs des entités

Comment stocker des données dans des entités accessibles depuis n'importe où

Dans nanos world, il est possible de stocker des données sur des entités. Ces valeurs sont accessibles par n'importe quel Package et vous pouvez également déterminer si la valeur sera automatiquement synchronisée avec les clients.

In Base Entity page we can find the definitions of :SetValue() and :GetValue() methods. Also it is possible to store Values globally on Client and Server Static Classes with the same methods!

SetValue

In any entity, you can use the method :SetValue(key, value, sync) to set any kind of value on that entity. Exemple :

Server/Index.lua
-- Définit une valeur 'my_value' synchronisée.
my_player:SetValue("my_value", 100, true)
tip

It is possible to store any kind of value, except functions.

If you pass sync as true, that value will be automatically synchronized with all clients. Note that syncis an optional parameter only available on the Server side.

note

Client.SetValue() and Server.SetValue() won't have the last parameter sync. En effet, la valeur n'existera que sur le côté que vous définissez.

GetValue

After set, you can access any value from any entity using :GetValue(key, fallback) on any entity you want. Si vous définissez la valeur à synchroniser sur les Clients, vous serez en mesure d'obtenir les valeurs du côté Client également. You can pass an aditional parameter fallback which will be returned if the key doesn't exist!

-- Obtient la valeur 'my_value' ou retourne '0' si aucune valeur n'est définie
locale my_value = my_player:GetValue("my_value", 0)
attention

Be aware that storing entities itself will not nullify the value if the entity is destroyed, so it is a good practice to validate if values with :IsValid() after retrieving them.