简体   繁体   English

当条件满足时,在 Photon Room(4 个玩家)中显示一个玩家的字符串

[英]Displaying a string of one of the players in a Photon Room (4 players) when a condition is met

Playerprefs has a way of setting and getting players' info such as string, int, etc. Playerprefs 有一种设置和获取玩家信息的方法,例如字符串、整数等。

I am using photon to set and get each player's string but it only captures one local player!我正在使用光子来设置和获取每个玩家的字符串,但它只捕获一个本地玩家! The string which each player enters should, later on, be displayed to all other Players only if a condition is met (bool).每个玩家输入的字符串稍后应该仅在满足条件时才显示给所有其他玩家(布尔值)。

Does Playerprefs actually store each player's information and can it actually be retrieved (Every player)? Playerprefs 是否真的存储了每个玩家的信息,并且真的可以检索到(每个玩家)?

For example: When it's your turn to play then let's say a bool is set to true then display your string to all other players.例如:当轮到你玩时,假设一个 bool 设置为 true,然后向所有其他玩家显示你的字符串。 So a game over should be activated and prevent the game from continuing!所以应该激活游戏结束并阻止游戏继续!

Am I doing this through the following scripts below: Your suggestion or correction would really mean a lot.我是否通过以下脚本执行此操作:您的建议或更正确实意义重大。 I have been stuck for a while.我被困了一段时间。

My first Script (Input string):我的第一个脚本(输入字符串):

[SerializeField]
WordInfo wordInfo;

void Update()
{
    wordInfo.word = StringWord;
}

public void GetString()
{
    StringWord = PlayerPrefs.GetString("word", "no word");
}
//use Onclick to set the string 
public void SetString()
{
    saveWord = InputWord.text;
    PlayerPrefs.SetString("word", saveWord);
}

My second Script (Retrieve the entered string to be read from another scene (static))我的第二个脚本(检索要从另一个场景读取的输入字符串(静态))

public class WordInfo: MonoBehaviour
{
public string word;
public static WordInfo wordInfo;

private void Awake()
{
    if (wordInfo == null)
    {
        wordInfo = this;
        DontDestroyOnLoad(gameObject);
    }
    else if(wordInfo != this)
    {
        Destroy(wordInfo.gameObject);
        wordInfo = this;
        DontDestroyOnLoad(gameObject);
    }
}
}

My third script (Game script - different scene)我的第三个剧本(游戏剧本-不同场景)

public string TheSavedWord;
private const string DISPLAY_MY_STRING_PLAYER = "{0} Your Word is displayed to others!";
private const string DISPLAY_OTHER_STRING_PLAYER = "{0}'s word has been Displayed!";
private const string MY_WORD = "{0} your word is: ";

private void Update()
{
    TheSavedWord = WordInfo.wordInfo.word;

}
 
public void ShowPlayerString()
{
 //a bool to check when to display the string
 //if i am active and the isShow is set to true then display a notification
 Player_Notification.text = string.Format(isShow && iAmActive ? DISPLAY_MY_STRING_PLAYER : DISPLAY_OTHER_STRING_PLAYER, activePlayer.NickName);
 
 //display the string 
 WordToDisplay.text = string.Format(isShow && iAmActive ? MY_WORD+ " " + TheSavedWord: MY_WORD + " " + TheSavedWord, activePlayer.NickName);

}

This worked out fine.这工作得很好。 After multiple trials and understanding, I was able to get each player's string.经过多次尝试和理解,我能够得到每个玩家的字符串。

Here is the code:这是代码:

[SerializeField]
WordInfo wordInfo;

void Update()
{
    wordInfo.word = saveWord;
}

//use Onclick to set the string 
public void SetString()
{
    saveWord = InputWord.text;

    var hash = PhotonNetwork.LocalPlayer.CustomProperties;
    hash.Add("SaveWord", saveWord);
    PhotonNetwork.LocalPlayer.SetCustomProperties(hash);

    if (!PhotonNetwork.IsMasterClient) return;

    Debug.Log(PhotonNetwork.LocalPlayer.ToStringFull());
    
}
public override void OnPlayerPropertiesUpdate(Player targetPlayer, ExitGames.Client.Photon.Hashtable changedProps)
{
    base.OnPlayerPropertiesUpdate(targetPlayer, changedProps);

    if (!PhotonNetwork.IsMasterClient) return;
    if (!changedProps.ContainsKey("SaveWord")) return;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM