简体   繁体   English

unity 3d PhotonEngine 其他玩家如何获取masterclient生成的数据

[英]Unity 3d PhotonEngine How other player can get data generated by masterclient

hello I need help with an online 2 players game with photon.您好,我需要有关带有光子的在线 2 人游戏的帮助。 Each player receives a generated ticket number, player 1, will guest player2' s ticket number and player2 will guest also player1' s ticket number.每个玩家都会收到一个生成的票号,玩家 1 将客串 player2 的票号,而 player2 将客串 player1 的票号。

Here is a script for the master client to generate the tickets.这是主客户端生成票证的脚本。 When player 2 joins the room he will get his tickets from the ticket generated by player 1 (the master client).当玩家 2 加入房间时,他将从玩家 1(主客户端)生成的票中获得票。 I created a game object with photo view attached to it and the script here我创建了一个游戏 object 附有照片视图和脚本here

void Start()
{
    if (PhotonNetwork.IsMasterClient)
    {
        player1ticket = UnityEngine.Random.Range(1000, 5000);
        player2ticket = UnityEngine.Random.Range(1000, 5000);
    }
    getTicket = true;
}

// Update is called once per frame
void Update()
{
    if (getTicket)
    {
        if (PhotonNetwork.IsMasterClient)
        {
            myTicket = player1ticket;
        }
        else
        {
            myTicket = player2ticket;
        }
        getTicket = false;
    }
}

But When playing the game the player 2 doesn't have any ticketnumber但是玩游戏时玩家2没有票号

First all you know that it might happen that both get the same ticket number - not very probable but possible.首先,您知道两者可能会获得相同的票号 - 不太可能但有可能。 You might want to repeat the second random in such a case在这种情况下,您可能想重复第二次随机

player1ticket = UnityEngine.Random.Range(1000, 5000);
do
{
    player2ticket = UnityEngine.Random.Range(1000, 5000); 
} 
while(player2ticket == player1ticket);   

Then well you only generate the values at the master.那么你只在主服务器上生成值。 So on the client getTicket is never true .所以在客户端getTicket永远不会是true

And even if it would the client doesn't have the player2ticket value neither.即使客户端也没有player2ticket值。

There is also no need/sense of assigning them every frame in Update.也没有必要/感觉在更新中的每一帧都分配它们。


I would rather use the Custom Room Properties and on the master use Room.SetCustomProperties which will trigger OnRoomPropertiesUpdate(Hashtable on all clients. This call is also triggered once after a client joins a room so you can be sure a newly connected clients won't miss it.我宁愿使用自定义房间属性,并在主服务器上使用Room.SetCustomProperties ,这将在所有客户端上触发OnRoomPropertiesUpdate(Hashtable 。客户端加入房间后也会触发一次此调用,因此您可以确定新连接的客户端不会错过它。

void Start()
{
    if (PhotonNetwork.IsMasterClient)
    {
        player1ticket = UnityEngine.Random.Range(1000, 5000);
        player2ticket = UnityEngine.Random.Range(1000, 5000);

        // You are master so already get your ticket
        myTicket = player1ticket;

        // Then simply push the other ticket(s) into the room properties
        Hashtable properties = PhotonNetwork.room.customProperties; 
        properties["ticket2"] =  player2ticket);  

        // This will store the value in the current room
        // and triggers OnRoomPropertiesUpdate on all already connected clients
        PhotonNetwork.room.SetCustomProperties(properties);
    }
}

// This will be called
// - as soon as you entered a room
// - whenever someone writes into the room properties
public override void OnRoomPropertiesUpdate(Hashtable propertiesThatChanged)
{
    // Not sure about the exact API right now, it's not in their docs
    // it's either ContainsKey, HasKey or something similar ..maybe even TryGetValue works
    if(!propertiesThatChanged.ContainsKey("ticket2")) return;

    if(!PhotonNetwork.IsMasterClient)
    {
        myTicket = (int) PhotonNetwork.room.customProperties["ticket2"];
    }
}

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

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