是否有可能只加载光子对象?

发布于 2025-01-18 22:31:57 字数 1680 浏览 4 评论 0原文

示例场景:舞台上有 4 个座位,我有一个名为 x 的 int 值。玩家每次坐在椅子上,x 值都会增加 1。当你从座位上站起来时,x值减少1。我通过rpc将这个值发送给所有玩家。当游戏中的所有玩家都就座时(当游戏中的玩家数量等于 x 时)我将光子对象加载到场景中。最后坐在座位上的人将此对象上传到舞台上。因为当最后一个玩家坐在座位上时,x的值就等于游戏中玩家的数量。在游戏的一部分中,我必须完全摧毁这个物体。只有座位上的最后一个玩家才能摧毁它,因为该物体的所有者就是他自己。我想要的是这样的:即使masterclient是第一个入座的玩家,它也会等待其他玩家入座,并且当x值等于游戏中玩家的数量时(当最后一个玩家入座时) ),主客户端加载该项目。因此该对象的所有者成为游戏中的主客户端。而masterclient可以随意销毁该对象。我该怎么办呢。 我使用的代码文件:

public class control : MonoBehaviourPunCallbacks{
private int x;
private GameObject nesne;

public void sit()
{

    x = PlayerPrefs.GetInt("kaçoldu");

    photonView.RPC("KacOldu", RpcTarget.OthersBuffered,x);
    KacOldu(x);


    if (PhotonNetwork.CurrentRoom.PlayerCount == x)
    {
         nesne = PhotonNetwork.Instantiate("nesne", Vector3.zero, Quaternion.identity, 0, null);
    }

}
public void up()
{
    x = PlayerPrefs.GetInt("kaçoldu");

    photonView.RPC("KacOldueksi", RpcTarget.OthersBuffered, x);
    KacOldueksi(x);

    PhotonNetwork.Destroy(nesne);

}

[PunRPC]
private void KacOldu(int x)
{
    x = PlayerPrefs.GetInt("kaçoldu");
    x++;
    Debug.Log("kaç?"+ x);
    PlayerPrefs.SetInt("kaçoldu", x);
}
[PunRPC]
private void KacOldueksi(int x)
{
    x = PlayerPrefs.GetInt("kaçoldu");
    x--;
    Debug.Log("kaç?" + x);
    PlayerPrefs.SetInt("kaçoldu", x);
}

}

游戏中的masterclient无法销毁名为“nesne”的对象,因为“nesne”的所有者是另一个玩家。如果我尝试这样做: if (PhotonNetwork.CurrentRoom.PlayerCount == x && PhotonNetwork.IsMasterClient) { nesne = PhotonNetwork.Instantiate("nesne", Vector3.zero, Quaternion.identity, 0, null); } 我仍然无法成功,因为如果最后坐着的玩家不是主客户端,“nesne”的所有者仍然无法成为主客户端。我希望主客户端在游戏中的玩家数量等于 x 时加载“nesne”。你能帮助我吗?

Example scenario : there are 4 seats on the stage, and I have an int value named x. The x value increases by 1 each time the player sits in the chair. When you get up from the seat, the x value decreases by 1. I send this value to all players with the help of rpc. When all the players in the game are seated (when the number of players in the game is equal to x) I load a photon object into the scene. The last person sitting on the seat uploads this object to the stage. Because when the last player sits on the seat, the value of x becomes equal to the number of players in the game. In part of the game I have to completely destroy this object. Only the last player in the seat can destroy it because the owner of the object is himself. What I want is this: Even if the masterclient is the first player to take the seat, it waits for the other players to take the seat, and when the x value equals the number of players in the game (when the last player is seated), the masterclient loads the item. So the owner of the object becomes the masterclient in the game. And masterclient can destroy the object at will. How can I do that. Code file I use:

public class control : MonoBehaviourPunCallbacks{
private int x;
private GameObject nesne;

public void sit()
{

    x = PlayerPrefs.GetInt("kaçoldu");

    photonView.RPC("KacOldu", RpcTarget.OthersBuffered,x);
    KacOldu(x);


    if (PhotonNetwork.CurrentRoom.PlayerCount == x)
    {
         nesne = PhotonNetwork.Instantiate("nesne", Vector3.zero, Quaternion.identity, 0, null);
    }

}
public void up()
{
    x = PlayerPrefs.GetInt("kaçoldu");

    photonView.RPC("KacOldueksi", RpcTarget.OthersBuffered, x);
    KacOldueksi(x);

    PhotonNetwork.Destroy(nesne);

}

[PunRPC]
private void KacOldu(int x)
{
    x = PlayerPrefs.GetInt("kaçoldu");
    x++;
    Debug.Log("kaç?"+ x);
    PlayerPrefs.SetInt("kaçoldu", x);
}
[PunRPC]
private void KacOldueksi(int x)
{
    x = PlayerPrefs.GetInt("kaçoldu");
    x--;
    Debug.Log("kaç?" + x);
    PlayerPrefs.SetInt("kaçoldu", x);
}

}

The masterclient in the game cannot destroy the object named "nesne", because the owner of the "nesne" is another player. If I try this: if (PhotonNetwork.CurrentRoom.PlayerCount == x && PhotonNetwork.IsMasterClient) { nesne = PhotonNetwork.Instantiate("nesne", Vector3.zero, Quaternion.identity, 0, null); }
I still can't succeed because if the last sitting player is not the masterclient, the owner of the "nesne" still cannot become the masterclient. I want the masterclient to load the "nesne" when the number of players in the game is equal to x. Can you help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

π浅易 2025-01-25 22:31:57

老实说,我真的不理解您在这些playerprefs背后的逻辑,以及什么x完全应该是因为每个玩家似乎都能完全覆盖它...

但是,对于您的要求,我将

  • a)简单地使用rpctarget.allbuffered,以便您也将自己接听为b
  • )b)b)检查金额int int rpc而

不是eg

public void sit()
{
    x = PlayerPrefs.GetInt("kaçoldu");

    photonView.RPC(nameof(KacOldu), RpcTarget.AllBuffered, x);
}

public void up()
{
    x = PlayerPrefs.GetInt("kaçoldu");

    photonView.RPC(nameof(KacOldueksi), RpcTarget.AllBuffered, x);
}

[PunRPC]
private void KacOldu(int x)
{
    x = PlayerPrefs.GetInt("kaçoldu");
    x++;
    Debug.Log("kaç?"+ x);
    PlayerPrefs.SetInt("kaçoldu", x);

    // This happens now regardless of whether you or anyone else is the last sitting client
    if (PhotonNetwork.IsMasterClient && PhotonNetwork.CurrentRoom.PlayerCount == x)
    {
         // I would actually use this instead in case the Masterclient role is shifted to someone else
         nesne = PhotonNetwork.InstantiateRoomObject("nesne", Vector3.zero, Quaternion.identity, 0, null);
    }
}

[PunRPC]
private void KacOldueksi(int x)
{
    x = PlayerPrefs.GetInt("kaçoldu");
    x--;
    Debug.Log("kaç?" + x);
    PlayerPrefs.SetInt("kaçoldu", x);

    // This happens for any standing up client including the masterclient himself
    if (PhotonNetwork.IsMasterClient && nesne)
    {
         PhotonNetwork.Destroy(nesne);
    }
}

To be honest I don't really understand your logic behind those PlayerPrefs and what x is supposed to be exactly since every player seems to be able to overrule it completely ...

But for what you ask for I would

  • a) simply use RpcTarget.AllBuffered instead so that you also get the call yourself as
  • b) check the amount int the RPC instead

so something like e.g.

public void sit()
{
    x = PlayerPrefs.GetInt("kaçoldu");

    photonView.RPC(nameof(KacOldu), RpcTarget.AllBuffered, x);
}

public void up()
{
    x = PlayerPrefs.GetInt("kaçoldu");

    photonView.RPC(nameof(KacOldueksi), RpcTarget.AllBuffered, x);
}

[PunRPC]
private void KacOldu(int x)
{
    x = PlayerPrefs.GetInt("kaçoldu");
    x++;
    Debug.Log("kaç?"+ x);
    PlayerPrefs.SetInt("kaçoldu", x);

    // This happens now regardless of whether you or anyone else is the last sitting client
    if (PhotonNetwork.IsMasterClient && PhotonNetwork.CurrentRoom.PlayerCount == x)
    {
         // I would actually use this instead in case the Masterclient role is shifted to someone else
         nesne = PhotonNetwork.InstantiateRoomObject("nesne", Vector3.zero, Quaternion.identity, 0, null);
    }
}

[PunRPC]
private void KacOldueksi(int x)
{
    x = PlayerPrefs.GetInt("kaçoldu");
    x--;
    Debug.Log("kaç?" + x);
    PlayerPrefs.SetInt("kaçoldu", x);

    // This happens for any standing up client including the masterclient himself
    if (PhotonNetwork.IsMasterClient && nesne)
    {
         PhotonNetwork.Destroy(nesne);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文