Can I pass refrences with PhotonView.RCP or update values on clients side?

Options
lets say I create a NPC and it has 100 gold.
how can update it's gold for when people buy something from it?

public Class NPCClass
{
    public NPCClass(int startCash){
        public int money = startCash ;
    }
}

//Network class
NPCClass npcFrank = new NPCClass(100)
BuyFromNPC(npcFrank, 10);

[PunRPC]
void BuyFromNPC(NPCClass npc, int money){
    ScenePhotonView.RPC("InteractWithNPC", PhotonTargets.All, Object[] {npc, money});
}

void InteractWithNPC(NPCClass npc, int money){
    npc.money = money;
}
How can I refrence the money the NPC has? I am having a huge problem trying to directly refrence values to change them over all clients. I don't want to create a:

    [PunRPC]
    void InteractWithNPCFrank(int money)
    {
        //blah
    }
I need to create dynamic methods that can call with refrence changes in class values.
Is there a way to do this or is there a way to update a list of objects on a clients computer ever event that will change everyone elses values?

Comments

  • vadim
    Options
    You have public 'money' property available on every client. You need only to update it synchronously. SetMoney RPC or better ChangeMoney RPC (to avoid racing between sets from different clients) should work in your case.
  • i73
    i73
    edited February 2016
    Options
    vadim said:

    You have public 'money' property available on every client. You need only to update it synchronously. SetMoney RPC or better ChangeMoney RPC (to avoid racing between sets from different clients) should work in your case.

    So I believe I understand what you are saying, and yes my NPC has a public var money but when I want to change that specific NPC money value how can I do that? If I can't actually reference the class how can I reference the properties?

    Even if I go with this:
       
        ChangeMoney(20);
    
        [punRPC]
        void ChangeMoney(int n)
        {
            SomeRandomNPC.money = n;
            //How can I update 'Frank' or 'James' NPC money without passing the class?
        }    
    ^ just passes 20 without the actual refrence to the npc class I can't add to it.

    Normally I would
       
        ChangeMoney(20, ref FrankNpcClass);
    
        void ChangeMoney(int n, ref NPCClass npc)
        {
            npc.money = n;
            //referencing the actual npc frank.money
        }    
  • vadim
    Options
    RPC is always called on PhotonView object. I guess in this particular case you call RPC on NPC object. So you can access property by 'this.money' if handling RPC in component keeping this property.
    Otherwise, you can find required component via GetComponent and access its property.
  • i73
    Options
    vadim said:

    RPC is always called on PhotonView object. I guess in this particular case you call RPC on NPC object. So you can access property by 'this.money' if handling RPC in component keeping this property.
    Otherwise, you can find required component via GetComponent and access its property.

    I feel stupid for not thinking of this... Thank you

    ChangeNPCMoney(float amount, NPCClass npc)
    {
    npc.ChangeMoney( amoun)t;
    }

    NPCClass{
    public float money;
    [punRPC]
    ChangeMoney(float amount)
    {
    this.money = amount;
    }
    }

    I think I did not fully understand what photon view did until I looked at the code, I realize now it just calls the methods on all clients.
    Thanks for your help!