How do a synch a name change?
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
How do a synch a name change?
BlitzBurn
2019-10-25 18:03:28
I want o change the names of certain gameobjects during runtime, does anyone know how to synch this?
Comments
Maybe with an RPC function ? have you tried that ?
I have tried it, but the name change does only goes through on my client. Not on the other one.
I have tried to setup a onphotonserializeview to sync it, but to no avail
Hi @BlitzBurn,
I'm interested to know why you need this?
What information does the GameObject's name hold?
RPC or RaiseEvent or PhotonView serialization are different ways to do this.
You could share more details about what you have tried, code snippets.
@JohnTube
Hi,
I am currently developing a small scale multiplayer game using photon as a school project.
We choose our own projects, and I originally intended to use Unet as Photon had not been part of the cariculum, but switched after Unet started causing problems.
I originally wanted to know how to switch names of gameobjects since another function depended on it. The function itself was used to ensure that the projectiles fired by the player would not hurt the player that fired it. The projectile would compare recieve the name of the player that fired it and then compare that name to whatever it collided with to determine if it should deal damage or not.
As of right know I have scrapped the whole system for the sake of my own sanity and for the sake of time.
This is the code that would recieve the players username in the login scene:
public void UserNameInput_OnClick()
{
//Generates a hashtable to store the name input
ExitGames.Client.Photon.Hashtable _customName = PhotonNetwork.LocalPlayer.CustomProperties;
//Takes the text in the input field and puts it in a string
userName = userNameInput.text.ToString();
//saves the input in the hashtable
_customName["CustomPlayerName"] = userName;
PhotonNetwork.LocalPlayer.CustomProperties = _customName;
//This line was just used for testing the code
string SavedUsername = (string)PhotonNetwork.LocalPlayer.CustomProperties["CustomPlayerName"];
}
//This code was attached to the player prefab and would load the name in the gameplayscene
[PunRPC]
private void ChangeName()
{
newName = (string)PhotonNetwork.LocalPlayer.CustomProperties["CustomPlayerName"];
gameObject.name = newName;
}
I believe the Change Name() method was called correctly, although the line that did call it is long gone.
The exact issue was that every instance of the player prefab would recieve the new name that was saved, but this name would not be synched with the other clients. If user #1 entered the name BlitzBurn and user #2 entered JohnTube, #1 would see that both players were named BlitzBurn while #2 would see that both players were named JohnTube.
There were more variations of the code, as I tried several thing over the course of several days, but all of them had pretty much the same result.
Hi @BlitzBurn,
Player.CustomProperties
should be used as "read-only". Do not modify it directly. Instead, usePlayer.SetCustomProperties()
method to add, modify or remove custom actor properties.
public void UserNameInput_OnClick()
{
//Generates a hashtable to store the name input
ExitGames.Client.Photon.Hashtable _customName = new ExitGames.Client.Photon.Hashtable(1);
//Takes the text in the input field and puts it in a string
userName = userNameInput.text;
//saves the input in the hashtable
_customName["CustomPlayerName"] = userName;
PhotonNetwork.LocalPlayer.SetCustomProperties(_customName);
}
- Photon already has a built-in
Player.Nickname
.