[Mirror] Setting Player Variables After Connecting
Hi Everyone, I'm new to Mirror and I've read some conflicting posts regarding commands.
Currently, when the player connects to the server, I am trying to run a command to set their weapons etc. The player chooses which weapons they want to use before even connecting to the server (Like Call of Duty or Battlefield etc)
This information is stored in a database, retrieved and stored in a class like this, which is do not destroy on load:
public class PlayerProfile : MonoBehaviour {
public string DisplayName;
public List<Item> Inventory = new List<Item>();
public Item PrimaryWeapon;
public Item SecondaryWeapon;
}
Then, once the local client has started, I am using a Command to send this information to the server:
public override void OnStartLocalPlayer() {
if (!hasAuthority) { return; }
string DisplayName = PlayerProfile.instance.DisplayName;
Item PrimaryWeaponDetails = PlayerProfile.instance.PrimaryWeapon;
CmdSetupPlayer(DisplayName, PrimaryWeaponDetails);
}
This is the command:
[Command]
public void CmdSetupPlayer(string displayName, Item PrimaryWeaponDetails) {
DisplayName = displayName;
GameObject instance = Instantiate(ItemPrefab);
instance.GetComponent<Item>().SetItemDetailsFromPlayer(PrimaryWeaponDetails);
NetworkServer.Spawn(instance, connectionToClient);
PrimaryWeapon = instance;
}
Display name gets set fine, the instance of Item is spawned on the server, however I get an error saying "ID" is not set, in the SetItemDetailsFromPlayer method, which is a method on the Item script itself:
public void SetItemDetailsFromPlayer(Item Item) {
ID = Item.ID;
Name = Item.Name;
Price = Item.Price;
Description = Item.Description;
}
I have read that I can only pass certain types of data to commands/rpcs etc, but Mirror's documentation shows some examples of them using custom classes/objects so I'm a little confused as to what exactly I should be doing here.
I can only assume that the Item PrimaryWeaponDetails is actually null because the command isn't accepting it? Very unsure please help