TagObject usage?

Options
I have a problem and I think TagObject might be my solution. I cannot find any documentation anywhere on how to use it. If someone could write up a quick usage tutorial, I would be grateful.

However, if it ends up not being my solution, I will describe my problem and am asking for advice.

I need to access the gameobject of the player that created an object. The object is a UI button, and when the player on another client clicks it, it needs to call methods and change variables on the original player, the one that instantiated it. I was thinking I could access the player through the photonview somehow. How can I access a player's "player" gameobject through an object that they own that is not in the same hierarchy? I need to call a function back on the original player based on a different player's interactions with the object.

(Sorry if I haven't explained myself well. If there is some part that I need to explain in more detail, please ask me)

Comments

  • Tobias
    Tobias admin
    edited August 2015
    Options
    The TagObject is a object variable that can be used by your code any way you like. It's not synchronized or anything but each client can store something player-related in there.

    Let's say each player has a (single) character. The character gets instantiated via PhotonNetwork.Instantiate and on the prefab, a script implements OnPhotonInstantiate(PhotonMessageInfo info). This is called when the object gets created and the info variable contains who instantiated it. So you can store this.gameObject as PhotonPlayer.
    The benefit: If any code provides you with a PhotonPlayer, you have the reference to the GameObject which is that player's character.

    See: https://doc.photonengine.com/en/pun/current/tutorials/instantiation
  • prt0100
    Options
    Thank you Tobias. This worked as I was hoping. When I get a chance, I will come back to this thread and post an explanation and my solution for others who have a similar situation.
  • nomika
    Options
    This Link is no longer working and I would love access to it.
    https://doc.photonengine.com/en/pun/current/tutorials/instantiation

    public class ScriptAttachedToYourPrefabPlayer : PunBehaviour
    {
    public override void OnPhotonInstantiate(PhotonMessageInfo info)
    {
    //Assign this gameObject to player called instantiate the prefab
    info.sender.TagObject = this.gameObject;
    }
    }

    This works great but

    public override void OnPhotonPlayerDisconnected( PhotonPlayer playerWhoLeft )
    {
    Debug.Log( "OnPhotonPlayerDisconnected() " + playerWhoLeft.TagObject);

    my debug log shows my master client object. I'm sure who info.sender.TagObject ends up being stored in PhotonPlayer.TagObject nor why it seemed to store the wrong object for me.

    Note: I'm trying to access a gameObject right before it is destroyed when a player kills the game unexpectedly (to clean up scene objects that are not children but that are being affected by the player gameObject.)
  • nomika
    Options
    Edit.. Debug.Log( "OnPhotonPlayerDisconnected() " + playerWhoLeft.TagObject);
    does appear to be storing the name of the gameObject who disconnected.

    I'm just not sure how or where this happened: Playerobject.TagObject = Infomessage.sender.TagObject
  • [Deleted User]
    edited April 2017
    Options
    Hi @nomika,

    the Instantiation doc is still available, you can find it over here.

    TagObject actually stores a reference to a game object. When you use this game object in Debug.Log, its ToString() function gets automatically called and returns its name. Since names are always the same when being instantiated using PhotonNetwork.Instiatate - regardless of which player instantiated which object and non of these objects gets renamed - you can not say if "Prefab (Clone)" is actually your prefab or someone's other.