Inventory and Item Pickup Script Help [C#]

Options
Hello,

I have a item pickup script that add's an item to the player's inventory when the player is over the object in game and clicks the left mouse button how it does this is by adding an item to an array and then deleting the object. This script works just fine when only one person is logged in on the server but if two people are on i get an error on only one of the clients:

Failed to 'network-remove' GameObject. Client is neither owner nor masterClient taking over for owner who has left: View (0)1002 on lumber(Clone)

and then the other client can take and remove the objects just fine.

I read about photon not letting clients destroy objects but is there anyway around this?

another small error i get is that when two people are joined it randomly gives the player two lumber instead of one.

Any idea's?

Thanks,
Joseph

Comments

  • vadim
    Options
    To network-destroy object you must call PhotonNetwork.Destroy from client which created that object.
    Another option is to send 'destroy' RPC to all clients and call non-network GameObject.Destroy method on every client.
    See \Assets\Photon Unity Networking\UtilityScripts\OnClickDestroy.cs script for reference (when DestroyByRpc = true).
    another small error i get is that when two people are joined it randomly gives the player two lumber instead of one.
    Need more details on what is 'gives the player lumber' to answer.
  • Thanks for replying,

    I currently use PhotonNetwork.Destroy and i still get the 'Network Remove' error, I will look into sending a destroy RPC.
    Shall I ask any further questions on the same topic here or in a new thread?

    Thanks,
    Joseph
  • coolfireking2
    edited August 2014
    Options
    Hello,

    Thanks so much! I got it to work using RPC's but i have another question!
    How my, 'give the player lumber' script works is by sending a message to the player to add lumber, here is the code:
    void CollectItem(){
    		if(Collect == true && Input.GetMouseButtonDown(0)){
    			clicks += 1;
    			player.SendMessage("Add", material);
    		}
    		
    		if(clicks == maxClicks){
    			this.photonView.RPC("DestroyResource", PhotonTargets.AllBuffered);
    		}
    	}
    

    my problem is the script doesn't know which player to send the Add command to. How would I tell the script who the nearest player is and send the add command to that player? Or maybe there is another way to do it.

    Thanks so much for the first bit of help!
    Joseph
  • Hello,

    One More Question:
    I have this code which PhotonNetwork.Instantiate's my lumber at its spawn points but when another player joins it spawns another set of lumber and i can't figure out why!
    here's the code:
    public class ResourceSpawn : MonoBehaviour {
    
    	Vector3 SpawnPoint;
    	bool ResourcesSpawned = false;
    	public GameObject ResourceSpawnPoint;
    
    	// Use this for initialization
    	void Start () {
    		SpawnPoint = ResourceSpawnPoint.transform.position;
    	}
    	
    	// Update is called once per frame
    	void Update () {
    
    	}
    
    	void OnJoinedRoom(){
    		if(ResourcesSpawned  == false){
    			PhotonNetwork.Instantiate ("lumber", SpawnPoint, Quaternion.identity, 0);
    			ResourcesSpawned = true;
    		} else if (ResourcesSpawned == true){
    			
    		}
    	}
    }
    
  • vadim
    Options
    my problem is the script doesn't know which player to send the Add command to. How would I tell the script who the nearest player is and send the add command to that player? Or maybe there is another way to do it.
    I guess you are looking for player who clicking the resource. Is this just local client player which can be accessed simply via PhotonNetwork.player?
    when another player joins it spawns another set of lumber
    You spawn lumber in OnJoinedRoom() each time new player joins. ResourcesSpawned var does not prevent that because it's not synchronized between clients. Moreover, lumber spawned by each client will disappear when client will leave room. If lumber is 'shared' resource at spawn time (no one owns it) then better instantiate it on master client with PhotonNetwork.InstantiateSceneObject method to prevent mentioned effects.
  • Hello,

    Thanks for the help, I will try out the new methods now :D

    Thanks,
    Joseph
  • How would I use PhotonNetwork.player? If i change the line, player.SendMessage("Add", material); to PhotonNetwork.player.SendMessage("Add", material);
    It comes up with this error:

    PhotonPlayer does not contain a definition for SendMessage.

    I also tried to define player as, player = PhotonNetwork.player

    but that comes up with the error, Cannot convert PhotonPlayer to UnityEngine.GameObject - same for transform aswell

    What am i doing wrong?

    EDIT: PhotonNetwork.InstantiateSceneObject works awesome! Thanks so much

    Thanks,
    Joseph
  • vadim
    Options
    I have no idea what is 'player' in your code.
    In PUN, PhotonNetwork.player is plain object (not GameObject) which represents your player in currently joined room. You can use it for storing various player data via custom properties. They will be available automatically on all joined clients. See PhotonPlayer.SetCustomProperties / PhotonPlayer.customProperties and PhotonNetwork.playerList
  • Hello,

    player is the players game object which I am trying to access. How would I go about using setCustomProperties for my inventory? would I set a custom property when I pick-up an object then read the list of custom properties in my inventory script?

    Thanks,
    Joseph
  • vadim
    Options
    would I set a custom property when I pick-up an object then read the list of custom properties in my inventory script
    Yes, it's possible solution. But remember that properties are for network sync first of all (to make that data available on other clients). If you have many values which are local then you probably want some sort of local storage (members of your game control class e.g.)