Scripting / VoltCS
Player Parameters
Named values every player carries in your world, such as a team, a role or a VIP flag, synced to everyone and saved between visits if you like.
Player Parameters work like an animator's parameters, except they belong to each player in your world. Every player has their own copy of each value. Your scripts read anyone's values and change their own. Synced values reach everyone in the instance. Saved values come back the next time that player visits the world.
Create them#
Open ParelVR SDK > Volt > Player Parameters, or the Player Parameters page of Volt > Settings, and press Create Player Parameters. This does three things:
- makes the asset
Assets/ParelVR/PlayerParameters.asset; - points your World Descriptor at it;
- installs the
PlayerParametersworld script atAssets/ParelVR/Volt/PlayerParameters.cs.
When you play or build the world, the SDK adds a Player Parameters object to the scene and copies your list into it. You do not need to set anything up by hand.
The list#
The page is laid out like the Animator's Parameters list:
- + adds a Float, Int or Bool.
- The search field filters the list. Drag rows to reorder them.
- Right-click a row to change its type, duplicate it or delete it.
| Column | What it does |
|---|---|
| Name | How scripts find the value. Letters, digits, dots, dashes and underscores, up to 32 characters; spaces become underscores. Names must be unique. |
| Default | The value every player starts with. |
| Saved | Kept on the player's account for this world and restored on their next visit. |
| Synced | Sent to everyone in the instance. Untick it for values only the player's own client needs. |
| Type | Values | Synced cost |
|---|---|---|
| Bool | false or true | 1 bit |
| Int | 0 to 255 | 8 bits |
| Float | -1 to 1, in steps of 1/127 | 8 bits |
Synced values share 256 bits, shown by the bar under the list. A world can have up to 128 parameters. Values that are only saved, or only local, cost nothing. Problems such as a repeated name or going over the budget are listed under the bar. Fix them before you build.
The schema hash under the bar is worked out from the synced names and types. Clients whose world has a different list ignore each other's values, so an old and a new version of your world never mix them up.
Read and change them#
Call the PlayerParameters script from any of your scripts:
public VoltGameObject vipDoor;
public override void OnPlayerEnterTrigger(VoltPlayer player)
{
if (PlayerParameters.GetBool(player, "IsVIP"))
vipDoor.SetActive(true);
}
public override void OnInteract()
{
PlayerParameters.SetInt("Team", 2); // the local player joins team 2
}
| Call | What it does |
|---|---|
Get(player, name) |
A player's value as a float. |
GetBool, GetInt, GetFloat |
The same, as that type. |
Set(name, value), SetBool, SetInt, SetFloat |
Changes a value of the local player. Out-of-range values are clamped. |
SetFor(player, name, value) |
Changes another player's value. See Changing other players. |
ResetLocal() |
Puts every value of the local player back to its default. |
Has(name), IndexOf(name) |
Whether the world has a parameter with that name. |
IsReady |
True once the parameters are running. |
An unknown name reads as 0 and logs a warning once. A player whose values have not arrived yet reads as the defaults.
Hear about changes#
public override void OnStart()
{
PlayerParameters.AddListener(this, nameof(OnParameterChanged));
}
public void OnParameterChanged()
{
if (PlayerParameters.ChangedName == "Team")
UpdateTeamColour(PlayerParameters.ChangedPlayer);
}
The method runs whenever any player's value changes, yours included. While it runs, ChangedPlayer and ChangedName say whose value it was and which one.
Changing other players#
Each player's own client sets that player's values and sends them. With SetFor, the instance master can change another player's synced value: their client is asked to make the change, and the result reaches everyone from there. Other players' requests are ignored. SetFor returns false when this client may not make the change.
A value that is not synced is never sent, so SetFor on one changes it only on your client.
How it syncs#
- Only changes are sent, gathered and sent at most ten times a second. Defaults are never sent, since every client starts from the same list.
- Players who join later get everyone's current values.
- Saved values are written to the player's own storage for this world at most every five seconds, and loaded when they arrive.
- The script needs the Networking and Storage capabilities. The SDK turns both on when it compiles.
Do not use Player Parameters for anything that must not be faked, such as currency or admin rights. A modified client can send values for itself, so check anything important on the master instead. A built-in version in the Volt API, checked by ParelVR itself, comes in a later update.
Updating the script#
The SDK owns PlayerParameters.cs. Edit the list on the Player Parameters page, not the script. When a newer SDK ships a newer script, the page shows Update.
