Unable to allocate viewID on client

Options
Hi everyone!

I'm currently maing a tower defence game but have run in to a slight problem. As it stands, whenever one of the players choose to build a tower, i want the towerManager script to manually allocate a view ID as well as change ownership of the view to the building player, It works fine for the master client, however when other clients do the same, the ownership successfully changes but the view ID remains at -1 which creates a whole slew of problems for me.

If anyone has a solution or workaround i would be super greatful!

This is what the problematic code looks like.



private void InstantiateTower(int selectedTower, Vector3 buildPosition)
{

playerInventory.RemoveResourcesFromInventory(Towers[selectedTower].GetTowerGoldCost,Towers[selectedTower].GetTowerMcCost);

int towerID = PhotonNetwork.AllocateSceneViewID();
photonView.RPC("BuildTower", PhotonTargets.AllBuffered, towerID, selectedTower, buildPosition);

activeTower = 4;

}

[PunRPC]
private void BuildTower(int towerID, int selectedTower, Vector3 buildPosition)
{

Tower instance = Instantiate(Towers[selectedTower], buildPosition, Quaternion.identity);
PhotonView view = instance.GetComponent();

view.TransferOwnership(playerAgent.GetComponent().photonView.ownerId);
view.viewID = towerID;

instance.MoveToInteract(playerAgent);

}



Comments

  • Hi @Olindstrom,

    int towerID = PhotonNetwork.AllocateSceneViewID();


    This is (one of) your problem(s). SceneViewIds can only be allocated by the MasterClient, other clients can only allocate 'normal' ViewIds. Instead of using this, please try using AllocateViewId function and see, if this works without applying further changes. If this works, you can also try removing the Ownership Transfer - I think this is not necessary when allocating a ViewId.
  • Hi @Olindstrom,

    int towerID = PhotonNetwork.AllocateSceneViewID();


    This is (one of) your problem(s). SceneViewIds can only be allocated by the MasterClient, other clients can only allocate 'normal' ViewIds. Instead of using this, please try using AllocateViewId function and see, if this works without applying further changes. If this works, you can also try removing the Ownership Transfer - I think this is not necessary when allocating a ViewId.
    This did the trick, thanks!

    Is there a specific reason why some RPCs might trigger on the master client and not on the other cliends and vice versa? I'm having a problem like that where i call an RPC to destroy a tower and it gets destroyed on the calling client but not on the recieving clients. It might be down to my code but then why would it work on the client calling it?
  • Is there a specific reason why some RPCs might trigger on the master client and not on the other cliends and vice versa?


    When calling the RPC function do you use PhotonTargets.Others as second parameter? In this case you have to use PhotonTargets.All instead in order to forward this call to each client. If you already use the correct PhotonTargets parameter, please check if the RPC gets executed on the local and the remote clients at all. You can do this for example by adding a Debug.Log or Debug.LogError message.