RPC in bullet without photonView

In my game the bullets were photonnetwork.instantiated but as many people say, it's better to instantiate them via RPC. So I instantiate them via RPC, then I attach them a script OnCollisionEnter to damage, and everything ok it deals damage, but now I'm trying to storage in the damage done by the player, but doesn't work because the bullet itself doesn't have a Photon View.
In short words, I want that if A hits B with a bullet and deals 10.0 damage, then A hits C with a bullet and deals 5.0 damage, there's a script that add all that damage and say, damagedone=15.0.

*****SCRIPT: BULLET INSTANTIATE and DAMAGEDONE attached to player*****
[PunRPC]
public void BulletIns()
{
GameObject myb= Instantiate(bullet, bulletemitter.transform.position, bulletemitter.transform.rotation) as GameObject;
Rigidbody temp_myb;
temp_myb = myb.GetComponent();
temp_myb.AddForce(transform.forward * 200.0f);
}

[PunRPC]
public void DamageDealt(float amg)
{
ddealt += amg;
}

*****SCRIPT: OnCollisionEnter() attached to the bullet*****
void OnCollisionEnter(Collision other)
{
if (other.gameObject.name == "Character(Clone)")
{
PhotonView pv = GetComponent();
pv.RPC("DamageDealt", PhotonTargets.AllBuffered, 5.0f);
//THIS IS WHERE I GET THE ERROR, AS THE BULLET DOESN'T HAVE A PHOTONVIEW. //IF I IGNORE THIS EVERYTHING WORKS FINE, BUT I NEED TO STORAGE THE DAMAGE DONE
GameObject target = other.collider.gameObject;
PhotonView pj = target.GetComponent();
pj.RPC("ApplyDamage", PhotonTargets.AllBuffered, 5.0f);
if (pj != null && pj.instantiationId != 0)
{
PhotonNetwork.Destroy(gameObject);
}
else
{
Destroy(gameObject);
}
}
}

Comments

  • Hey there,

    you could use Playerpropertys to synchronize damage done. You should keep a reference of the Bullets "owner" or "origin" on the bullet itself. Then you can tell the player you hit to add X Damage Points to the owners dmg count.
    Be careful though to Accumulate some points over some time before you set the damage points. Each Point setting will be a Network message! Also make sure only one player set the points! Either the owner or the player owner that you hit or something. If you are not carefull here you can easily double your network message count :)

    Hope this helps.
  • AndresPL96
    edited December 2018

    Then you can tell the player you hit to add X Damage Points to the owners dmg count.

    Thanks for answer!

    I've tried to do this but nothing i've tried has worked. I gave the bullet my player ID so when it collides it gives that int to the other player, and then, the other player sends a RPC targeting only to that ID with the method "DamageDealt", but doesn't work, I don't increase my damage dealt.

    ....if (other.gameObject.name == "Character(Clone)")
    {
    target = other.collider.gameObject;
    PhotonView pv = target.GetComponent();......

    Is there a way to make this but instead of other.collider using owner? Or a way to just give an object it's owner photonview?


  • Hey there,
    did you try setting playerproperties directly? This way you can skip your rpc problems.
    Lets say A was hit by B and C. Then you should have a dictionary on A (!) that simply maps the player name to the damage that was dealt by this player. So for example Dict\ damagevalues = {B : 15; C : 9001}.
    Then each second have each player call a function that sets the values that you have accumulated in this damage dictionary to the playerproperties. Then you add the values to the playerproperties of each player and clear the dictionary.
    You can also set the values directly for each damage applied but this will only create unneccesary traffic.
    So each bullet only needs a reference to its origins photonplayer.
    Hope this helps. If something is not clear i'll try to provide some code snippets next time when i am at home.
  • Hey there,
    did you try setting playerproperties directly? This way you can skip your rpc problems.
    Lets say A was hit by B and C. Then you should have a dictionary on A (!) that simply maps the player name to the damage that was dealt by this player. So for example Dict\ damagevalues = {B : 15; C : 9001}.
    Then each second have each player call a function that sets the values that you have accumulated in this damage dictionary to the playerproperties. Then you add the values to the playerproperties of each player and clear the dictionary.
    You can also set the values directly for each damage applied but this will only create unneccesary traffic.
    So each bullet only needs a reference to its origins photonplayer.
    Hope this helps. If something is not clear i'll try to provide some code snippets next time when i am at home.

    Thanks, I get what you mean but I've never done this, so if you can post those code snippets here it would help me a lot. :):)
  • Well here we go:

    playerPropertyBuffer = new Dictionary<Photon.Realtime.Player,int> ();

    This should be the map you buffer the data in. Then when you damage a player simply add the origin player with the given damage value to this map.
    Then each second or something call a function that executes this:

    foreach (KeyValuePair<Photon.Realtime.Player,int> kvp in playerPropertyBuffer) { ExitGames.Client.Photon.Hashtable props = new ExitGames.Client.Photon.Hashtable (){ }; lastValue = (int)(kvp.Key.CustomProperties ["points"]); props.Add ("points", lastValue + kvp.Value); kvp.Key.SetCustomProperties (props, null, Photon.Realtime.WebFlags.Default); }

    Sorry for the messy formatting.
    This way you can set/update properties for a certain player.
    Hope this helps you on the right way. If you can't get it to work let me know.
  • Thanks for answer again, but I don't understand the code a lot.

    Well here we go:

    playerPropertyBuffer = new Dictionary<Photon.Realtime.Player,int> ();

    Here you create the dictionary that will store the damage for each player that hits player "A", but "Photon.Realtime.Player" gives me an error.

    Then,

    foreach (KeyValuePair<Photon.Realtime.Player,int> kvp in playerPropertyBuffer) { ExitGames.Client.Photon.Hashtable props = new ExitGames.Client.Photon.Hashtable (){ }; lastValue = (int)(kvp.Key.CustomProperties ["points"]); props.Add ("points", lastValue + kvp.Value); kvp.Key.SetCustomProperties (props, null, Photon.Realtime.WebFlags.Default); }

    Again I got error and I don't get almost anything you trying to do there :( ...

    I did it this way, but still stuck:

    public Dictionary<int, float> playerPropertyBuffer;

    I could get the player ID that hit player A and its DMG, so I added them to the dictionary:

    if (playerPropertyBuffer.ContainsKey(ID))
    {
    playerPropertyBuffer[ID] = playerPropertyBuffer[ID] + DMG;
    }
    else
    {
    playerPropertyBuffer.Add(ID, DMG);
    }; But then I don't know how to do that stuff you did. How to give each player the damage they did?