RPC call problem

Heya,

I must be doing something really wrong with the RPC calls from client to client via the cloud server solution. Hoping someone here can help me with it. I was basing it off of the chat scripts.

OK, I have created a sample "attack test script" using RPC's, I believe modeled exactly after how PM's are sent in the Worker Demo, and it is not behaving as expected:
************************* 
** BEGIN SCRIPT 
*************************

using UnityEngine; 
using System.Collections;

public class TestShoot : Photon.MonoBehaviour 
{ 
public static TestShoot TS; 
private int maxHealth = 10; 
public int HitCount = 10; 
public float fireRate = 2.0F; 
private float nextFire = 0.0F; 
public Transform AimBegin;	// Empty GO in player prefab to provide an "aim point" from which to shoot ray into scene to check for "attack collision" (since we don't have weapons and attack animations to check for collisions with yet)

void Awake() 
{ 
TS = this; 
}

// Use this for initialization 
void Start () 
{ 
HitCount = maxHealth; 
} 

// Update is called once per frame 
void Update () 
{ 
if (photonView.isMine) 
{ 
// Are we dead? 
if (HitCount <= 0) 
{ 
Debug.LogWarning("YOU ARE DEAD!!!"); 
return; 
} 

if (Input.GetButton("Fire1") && Time.time > nextFire) 
{ 
nextFire = Time.time + fireRate;

// Cast a ray into the scene to see if we hit anyone directly in front of us 
RaycastHit hit = new RaycastHit(); 
Physics.Raycast (AimBegin.position, AimBegin.forward, out hit, 2.0f); 

if (hit.collider && hit.collider.tag == "Player") // Did we hit anything? and if so, was it a player? 
{ 
Debug.Log("Player Hit: " + hit.collider.gameObject.name);

// Get the PhotonView belonging to the player that we hit, so that we can send an RPC 
// call to it's owner (only way I could see to get a reference to the "PhotonPlayer" of the 
// person we hit… is this correct way to do this? 
PhotonView pView = hit.collider.gameObject.GetComponentInChildren<PhotonView>(); 
if (pView != null) 
{ 
// Call local method to make the RPC call and tell it which PhotonPlayer to send the RPC to 
SendHit(pView.owner); 
} 
else 
{ 
Debug.LogError("Missing photonview"); 
} 
} 
} 
} 
} 

void OnGUI() 
{	
// Update players current health onscreen - "HitCount" var 
GUI.Label(new Rect(10, 50, 150, 30), "Health: " + HitCount.ToString()); 
} 

[RPC] 
void DoHit(PhotonMessageInfo info) 
{ 
// I believe that at this point, we are receiving the message from the attacker and this method should be being 
// fired on the "victim" by the RPC call that came in from the attacker? Please advice if this is incorrect. 
DoDMG(2, info); 
}

void SendHit(PhotonPlayer target) 
{ 
// Tell our photonview to make an RPC call to the person we hit to run the "DoHit" method on the other player. 
photonView.RPC("DoHit", target); 
}

public static void DoDMG(int amount, PhotonMessageInfo info) 
{ 
Debug.LogWarning("I was hit by: " + info.sender + " - at - " + info.timestamp.ToString() ); // This line gets called correctly (or appears to, and the correct debug statement is printed to the log, telling us who we were attacked by… everything about this line seems to work, ie the correct attacker was named etc.

TS.HitCount -= amount;	// This line however is behaving weird (or appears to be), the HitCount var that actually gets decremented, is that of the attacker, not the victim as would be expected. At this point, the victims GUI also updates the "Health" label, by seemingly printing a 2nd Health label on top of the first with the correct HitCount after the damage was applied. This is very weird and hard to explain, it now shows 2 health labels on top of each other, the first still shows the default 10, while the 2nd shows the correct number after the attack, Note that in the inspector in the Unity editor at this point, the attacker is the one who actually received the damage and now has a reduced HitCount, while the victim, still registers as having full health.

// Also please note that in this scenario, the "attacker" is a client on a separate machine, while the "victim" is my local copy running in the unity editor so that i can watch the debug statements and see if i actually registered the hit RPC call from the attacker. 
} 
}

************************* 
** END SCRIPT 
*************************
Please any help you may be able to provide to help me figure out why the scenario is not working and why I am getting multiple GUI updates with different values on the "victims" screen, and why in the editor it is showing that the attacker actually took the damage etc, would be GREATLY appreciated!.

Thank you.

Comments

  • Has anyone gotten this to work on the photon cloud? One client hitting (ie attacking) another and both registering it correctly?
  • I think I found something: Your TS is static. If you change values of a player through this variable, all players will share just one instance if TS and thus get the same hits (inside of TS).

    If you remove the "static" in front of TS, how does it do?
Sign In or Register to comment.