Transferring a particular "state" - between multiple players

Hi Guys,

Hoping someone can help me figure out the best way to do this as I've been trying a number of different methods without luck. Below is what I'm trying to do followed by what I've tried and what is going wrong.

- I have up to 4 players instantiated on a scene. One of them has a "boss" state - currently that player is tagged as "boss". - When the "boss" touches any other player that player becomes the "boss" and the player that was boss isn't anymore.
- There can only be one "boss".

Seems like there is probably an easy solution to this but everything I've tried doesn't seem to work. The latest I tried was to set up 2 inenumerator functions to:
a) make a player "notboss" and wait three seconds - this is called on collision if player was "boss" and collided with "notboss"
b)make a player boss and wait a second - this is called on collision id player was "notboss" and collided with "boss".

I've also tried rpc's with no luck but maybe I was doing something wrong so any advice on how to set this up if this would be the best way to do this would be greatly appreciated

I’m wondering, what is a reliable way to achieve this? I’ve tried a few methods but none seem to work and I can’t find anything online specifically dealing with this scenario.

If you can be bothered reading my current code and logs I’ve put it below split into 3 parts. Collision function, ienumerators, and below that I’ve put in the output from my debugger and under that some comments on what is going wrong:

///////function for handling collisions

private void OnCollisionEnter2D(Collision2D collision)
{

Debug.Log("Collision entered.");


if (myPhotonView.IsMine)
{
Debug.Log("Photonview is mine");

if (gameObject.tag == "NotBoss" && collision.gameObject.tag == "Boss")
{
Debug.Log("gameobject tag = " + gameObject.tag + ". collision game object tag = " + collision.gameObject.tag + " . call reamainBossforseconds");

StartCoroutine(RemainBossForSeconds());
Debug.Log("Returning from remainBossforseconds");

}

if (gameObject.tag == "Boss" && collision.gameObject.tag == "NotBoss")
{
Debug.Log("gameobject tag = " + gameObject.tag + ". collision game object tag = " + collision.gameObject.tag + " . call reamainNOTBossforseconds");
StartCoroutine(RemainNotBossForSeconds());
Debug.Log("Returning from remainNOTBossforseconds");
}

}



}



/////// Ienums called from collision functions:


//this one is called when player becomes boss
IEnumerator RemainBossForSeconds()
{
Debug.Log("BOSS - Setting isBoss to true");
IsBoss = true;
Debug.Log("BOSS - setting tag to invincible");
gameObject.tag = "Invincible";
Debug.Log("BOSS - waiting for 1 second");
yield return new WaitingForSeconds(1);
Debug.Log("BOSS - setting tag to BOSS");
gameObject.tag = "Boss";


}

//this one is called when player becomes NotBoss
IEnumerator RemainNotBossForSeconds()
{
Debug.Log("NOTBOSS - Setting isBoss to false");
IsBoss = false;
Debug.Log("NOTBOSS - setting tag to invincible");
gameObject.tag = "Invincible";
Debug.Log("NOTBOSS - waiting for 3 second2");
yield return new WaitingForSeconds(3);
Debug.Log("NOTBOSS - setting tag to NotBOSS");
gameObject.tag = "NotBoss";

}


////// debug output from a collision:

Collision entered.
UnBossyEngine.Debug:Log (object)
AstroMovement:OnCollisionEnter2D (UnBossyEngine.Collision2D) (at Assets/AstroMovement.cs:347)

Collision entered.
UnBossyEngine.Debug:Log (object)
AstroMovement:OnCollisionEnter2D (UnBossyEngine.Collision2D) (at Assets/AstroMovement.cs:347)

Photonview is mine
UnBossyEngine.Debug:Log (object)
AstroMovement:OnCollisionEnter2D (UnBossyEngine.Collision2D) (at Assets/AstroMovement.cs:352)

gameobject tag = Boss. collision game object tag = NotBoss . call reamainNOTBossforseconds
UnBossyEngine.Debug:Log (object)
AstroMovement:OnCollisionEnter2D (UnBossyEngine.Collision2D) (at Assets/AstroMovement.cs:368)

NOTBOSS - Setting isBoss to false
UnBossyEngine.Debug:Log (object)
AstroMovement/<RemainNotBossForSeconds>d__25:MoveNext () (at Assets/AstroMovement.cs:441)
UnBossyEngine.MonoBehaviour:StartCoroutine (System.Collections.IEnumerator)
AstroMovement:OnCollisionEnter2D (UnBossyEngine.Collision2D) (at Assets/AstroMovement.cs:369)

NOTBOSS - setting tag to invincible
UnBossyEngine.Debug:Log (object)
AstroMovement/<RemainNotBossForSeconds>d__25:MoveNext () (at Assets/AstroMovement.cs:443)
UnBossyEngine.MonoBehaviour:StartCoroutine (System.Collections.IEnumerator)
AstroMovement:OnCollisionEnter2D (UnBossyEngine.Collision2D) (at Assets/AstroMovement.cs:369)

NOTBOSS - waiting for 3 second2
UnBossyEngine.Debug:Log (object)
AstroMovement/<RemainNotBossForSeconds>d__25:MoveNext () (at Assets/AstroMovement.cs:446)
UnBossyEngine.MonoBehaviour:StartCoroutine (System.Collections.IEnumerator)
AstroMovement:OnCollisionEnter2D (UnBossyEngine.Collision2D) (at Assets/AstroMovement.cs:369)

Returning from remainNOTBossforseconds
UnBossyEngine.Debug:Log (object)
AstroMovement:OnCollisionEnter2D (UnBossyEngine.Collision2D) (at Assets/AstroMovement.cs:370)

ExBoss Collision
UnBossyEngine.Debug:Log (object)
AstroMovement:OnCollisionExBoss2D (UnBossyEngine.Collision2D) (at Assets/AstroMovement.cs:387)

ExBoss Collision
UnBossyEngine.Debug:Log (object)
AstroMovement:OnCollisionExBoss2D (UnBossyEngine.Collision2D) (at Assets/AstroMovement.cs:387)

NOTBOSS - setting tag to NotBOSS
UnBossyEngine.Debug:Log (object)
AstroMovement/<RemainNotBossForSeconds>d__25:MoveNext () (at Assets/AstroMovement.cs:449)
UnBossyEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)


//////// from testing a lot, the behavior is very inconsistent. Sometimes Boss passes the Boss variable, sometimes Boss doesn’t. sometimes both players become not boss, sometimes both become boss. Can anyone suggest a reliable way to achieve what I’m after or fix the code above?

Many thanks.

Comments

  • Update: I was able to figure this outusing RPC's, view Id's, ienum functions, and a lot of added logic. the 'state' still isn't transferring on 100% of collisions but i think that's more a physics/synch issue which I'm addressing separately.