A library for systems to keep track of player associated data, including offline players and import/export.
(It’s not actually published as a package yet, but once it is the following applies.)
Head to my VCC Listing and follow the instructions there.
The data structure for each player consists of a CorePlayerData plus a list of CustomPlayerData.
The CorePlayerData contains general information about the player like their player id, display name, a reference to the VRCPlayerApi if present, whether the player is online, is local, and so on.
The CorePlayerData also contains a persistent id, which is an id which remains the same for a given player when they leave and rejoin (if player data was kept alive while they were offline, which is up to the CustomPlayerData to decide). The persistent id can also be used to resolve player data references in imports.
CustomPlayerData contains any and all user defined data, may be game state safe, may be latency hidden.
To use CustomPlayerData, see example:
using JanSharp;
using UdonSharp;
namespace MySystem
{
[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
public class MyPlayerData : CustomPlayerData
{
public override string PlayerDataInternalName => "author.my-system";
public override string PlayerDataDisplayName => "My Data";
public override bool SupportsImportExport => false;
public override uint DataVersion => 0u;
public override uint LowestSupportedDataVersion => 0u;
#region Game State
private string myValue;
#endregion
public override bool PersistPlayerDataWhileOffline()
{
return myValue != null; // null is the default value anyway.
}
public override void Serialize(bool isExport)
{
lockstep.WriteString(myValue);
}
public override void Deserialize(bool isImport, uint importedDataVersion)
{
myValue = lockstep.ReadString();
}
}
}
using JanSharp;
using UdonSharp;
using UnityEngine;
namespace MySystem
{
public class MyManager : UdonSharpBehaviour
{
// This is the required part.
[HideInInspector][SerializeField][SingletonReference] private PlayerDataManagerAPI playerDataManager;
[PlayerDataEvent(PlayerDataEventType.OnRegisterCustomPlayerData)]
public void OnRegisterCustomPlayerData()
{
playerDataManager.RegisterCustomPlayerData<MyPlayerData>(nameof(MyPlayerData));
}
// And these are other examples which are commonly convenient.
// Primarily because, even though the player data manger API provides methods to achieve this already,
// due to the nature of UdonSharp, it's clunky. Making helper properties and methods hides that.
private int myPlayerDataIndex;
[PlayerDataEvent(PlayerDataEventType.OnAllCustomPlayerDataRegistered)]
public void OnAllCustomPlayerDataRegistered()
{
myPlayerDataIndex = playerDataManager.GetPlayerDataClassNameIndex<MyPlayerData>(nameof(MyPlayerData));
}
public MyPlayerData LocalPlayerData => (MyPlayerData)playerDataManager.LocalPlayerData.customPlayerData[myPlayerDataIndex];
public MyPlayerData SendingPlayerData => (MyPlayerData)playerDataManager.SendingPlayerData.customPlayerData[myPlayerDataIndex];
public MyPlayerData GetPlayerDataForPlayerId(uint playerId)
{
CorePlayerData core = playerDataManager.GetCorePlayerDataForPlayerId(playerId);
return core == null ? null : (MyPlayerData)core.customPlayerData[myPlayerDataIndex];
}
public MyPlayerData GetPlayerDataForPersistentId(uint persistentId)
{
CorePlayerData core = playerDataManager.GetCorePlayerDataForPersistentId(persistentId);
return core == null ? null : (MyPlayerData)core.customPlayerData[myPlayerDataIndex];
}
public MyPlayerData GetPlayerData(CorePlayerData core)
{
return (MyPlayerData)core.customPlayerData[myPlayerDataIndex];
}
public void WritePlayerDataRef(MyPlayerData playerData)
{
playerDataManager.WriteCorePlayerDataRef(playerData == null ? null : playerData.core);
}
public MyPlayerData ReadPlayerDataRef(bool isImport)
{
CorePlayerData core = playerDataManager.ReadCorePlayerDataRef(isImport);
return core == null ? null : (MyPlayerData)core.customPlayerData[myPlayerDataIndex];
}
}
}
This project uses custom git filters to reduce the amount of noise generated by Udon and UdonSharp. These filters are not required in order to contribute, though without them all UdonSharp asset files will show as modified and those “changes” should not be committed.
com.jansharp.player-data
Undefined
1.0.0 (Changelog)
2022.3 or later
No dependencies
No legacy packages