Let’s say I had some static data in my game that was defined in a set of scriptable objects. For example, in an RTS, there might be four races and and each player chooses one at the beginning of the game. Each of these races as a slew of assets that I only want to (pre)load and register with the client scene if a player is actually using that race in game. For example, say I had the following:
public class RaceDefinition : ScriptableObject
{
public List<Unit> UnitPrefabs;
public List<Building> BuildingPrefabs;
}
public class Player
{
public RaceDefinition SelectedRace;
}
And then elsewhere, I want to be able to say:
private void RegisterPrefabs()
{
foreach (var player in Players)
{
foreach (var unit in player.SelectedRace.UnitPrefabs)
{
ClientScene.RegisterPrefab(unit.gameObject);
}
}
}
[ClientRPC]
public void RpcAllPlayersSelectedRace(??? what can I pass here?)
{
foreach (var player in Players)
player.SelectedRace = ????;
RegisterPrefabs();
}
Unfortunately, I can’t pass a RaceDefinition across the network in an RPC. As far as I’m aware, I can only pass primitive types, Vector3s, and GameObjects. The data that I want to pass is a reference to a Scriptable Object somewhere in my project. What could I pass as a parameter to an RPC that would enable me to have the server notify each client of each player’s selected race?
One option is to put all of the RaceDefinitions inside Resources and then pass a string from the server to the client, at which point the client calls Resources.Load() to find the actual asset. This feels kind of brittle though, since string references tend to break when stuff gets moved around unless you’re very careful. Another potential solution would be to have an object in the scene that has a list of all of the game’s static data in it, and then I just pass an index to an element in that list. This seems a little better, but still kind of clunky. Is there an even better way to specify an asset across the network?
Thanks for any help!