- Home /
How to access caller of command from from server?
I spawn a ship object using a command and want my player have ship as a property, but I can't access player from server.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace Assets._Scripts
{
public class PlayerController : NetworkBehaviour
{
public GameObject ShipPrefab;
public GameObject Ship;
void Start()
{
if (!isLocalPlayer)
return;
CmdCreateShip(netId);
}
void Update()
{
if (!isLocalPlayer)
return;
var shipController = Ship.GetComponent<ShipController>();
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
shipController.CmdAdvance();
}
[Command]
private void CmdCreateShip(NetworkInstanceId id)
{
var ship = Instantiate(ShipPrefab);
NetworkServer.SpawnWithClientAuthority(ship, connectionToClient);
}
}
}
Answer by Bunny83 · Aug 03, 2018 at 06:54 PM
A client can only call commands on objects they have authority over. So the object this command is sent to has to belong to the client that has send the command. So just check NetworkIdentity.clientAuthorityOwner.
If you actually sending the command from the player object of a client (which is the usual case in 90% of all cases) you can also use NetworkIdentity.connectionToClient to get the connection and NetworkIdentity.playerControllerId to get the controller ID of the owning player. Note that the "playerControllerId" is basically the index into the playerControllers list
I understand, but can't figure out the code that will do this: (player whose connectionId == NetworkIdentity.connectionToClient).Ship = ship
could you help with this.I don't know how to use connectionId and playerControllerId to get player object.
You are confusing a few things here. The "Ship" you talk about needs to be set on the client. Commands are methods that are executed on the server only. Since you spawn that ship with client authority you should simply attach a NetworkBehaviour to that ship that actually controls that ship. On that NetworkBehaviour on the ship you can use OnStartAuthority. This method will only be called on the client that owns the object after it has been spawned.
$$anonymous$$eep in $$anonymous$$d that every peer in a networking game has it's own set of gameobjects and every script exists on every computer again. However those scripts are seperate instances. If you want / need to exchange any data between different peers (that includes client - server) you need some sort of syncing. If clients want to talk to the server you use Commands, if the server wants to talk to clients he uses ClientRPCs or TargetRPCs. Furthermore you can use SyncVars which are automatically synchronised between sever and client.
While networking isn't blackmagic even the smallest example is rather complex (complex, not complicated). There are several objects involved, input is detected locally, events need to be send between peers, etc. $$anonymous$$ost questions about networking that get asked here only include a tiny fraction of the whole setup. However it's important to know all the details to actually understand what's happening or how the setup looks like. Individual help becomes quite difficult. If you want to start a discussion about what may be a good approach for a certain problem you may want to start a thread on the forum.
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
what is the best way to implement server side of a multiplayer game? 1 Answer
MMO server side programming 0 Answers
UNET OnCollisionEnter issues 0 Answers