RPC To Spawn Enemies

Options
Hello. I'm trying to make it so that when I spawn enemies, a number goes up. When that number reaches a certain amount to stop spawning enemies. That works great. Just the problem is, when another player joins the game, that number is 0 for them. So lets say, one player joins and makes 2 enemies spawn. Another player joins and makes another 2 enemies spawn, so both players have 4 enemies. I don't want that. I want to make it stay at 2 enemies for everyone that will continue to join. Do I need to use an RPC? In my code, I use an RPC to spawn the enemies, hoping that it would have worked, but it didn't. Where can I put the code that spawns the enemies so it will work like I want it to? Thanks in advance!
[code2=csharp]using UnityEngine;
using System.Collections;

public class MonsterSpawn : Photon.MonoBehaviour {

private string[] monsters = new string[3];
public int randomnumber;
public int spawnMonster;
public Vector3 vector2 = new Vector3 (129, 2, 111);

//public Vector3 vector3 = new Vector3(103, 7, 114);

// Use this for initialization
void Start () {
Application.runInBackground = true;
monsters[0] = "Fire1";
monsters[1] = "Grass2";
monsters[2] = "Water1";
}

// Update is called once per frame
void Update ()
{
this.photonView.RPC("SpawnMonster", PhotonTargets.AllBuffered);
}

[RPC]
void SpawnMonster()
{
if(spawnMonster < 2)
{
PhotonNetwork.Instantiate(monsters[Random.Range(0,monsters.Length)], vector2, Quaternion.identity, 0);
spawnMonster++;

}
}
}[/code2]

Comments

  • Tobias
    Options
    RPCs can be buffered. That means joining players will get them, even when they were executed earlier by everyone else.
    If you use this mechanic, you will end up with a lot of RPCs "from the past", however. They won't be replaced with newer calls.

    Better: Use a custom property in the room. This is available to everyone (even when joining late) and can be updated. Pick any short string name for it and set the number of current players in PhotonNetwork.room.SetCustomProperties(). You can set them when you create the room and when you are in the room. Don't set them while you are joining...
  • I now have this code:
    [code2=csharp]string[] roomPropsInLobby =
    {
    "m"
    };
    customRoomProperties.Add ("m", 0);
    PhotonNetwork.CreateRoom("Some room name", true, true, 18, customRoomProperties, roomPropsInLobby);[/code2]
    Now, how do I check if the customRoomProperty's "m" is less than 0, than update it to 1 if it is less than 0?
    Thanks!
  • Tobias
    Options
    When in the room, you can use PhotonNetwork.room to access it. It has a field customProperties (or similar) to access them. To update it, use PhotonNetwork.room.SetCustomProperties method.
  • wana7262
    Options
    i got the same issue...!!! working on zombies game there is a zombie spawner with photon view when a player joins it enables the spawner and then spawner instantiate zombies over network but when another player joins in it again enables the spawner which again spawns zombies in the scene..!!! spawner spawns 8 zombies in each round but when another player joins in there are 16 zombies...!!! how can i solve it..!!! please guide me to some easy solution..!!!
    Thanks