How to handle return values RPC-style?

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

How to handle return values RPC-style?

toreau
2014-04-27 03:48:23

I'm in the process of converting a proof of concept from singleplayer to multiplayer, using Photon Networking. I'm a bit puzzled on how to deal with RPCs and return values, or their lack of it, though.

Consider this singleplayer code:

[code2=csharp]if ( Input.GetKeyDown(KeyCode.H) && objectBeingLookedAt.isGatherable ) { RockResourceController resourceController = objectBeingLookedAt.transform.GetComponent<RockResourceController>(); inventoryController.AddItems ( resourceController.Hatchet () ); }

// Inside RockResourceController public Dictionary<Item, int> Hatchet () { Dictionary<Item, int> hatchedItems = new Dictionary<Item, int>();

foreach &#40; KeyValuePair&lt;Item, int&gt; pair in inventoryController.itemsDictionary &#41; {
    int nrOfItems = Mathf.Clamp&#40; Random.Range&#40;minHatchet, maxHatchet&#41;, 1, pair.Value &#41;;

    hatchedItems.Add &#40; pair.Key, nrOfItems &#41;;
    inventoryController.RemoveItem&#40; pair.Key, nrOfItems &#41;;
}

if &#40; inventoryController.isEmpty &#41; {
    Destroy&#40; this.gameObject &#41;;
}

return hatchedItems;

}[/code2] How can/should I convert this logic into "multiplayer mode"? I've tried setting the Hatchet method to be an RPC, and I can of course call it, but how do I get its return value(s) back?

Thanks in advance!

UPDATE:

I came up with a solution. Well, "solution". :) It's something that works, but from my point of view it's less than beautiful;

[code2=csharp]if ( Input.GetKeyDown(KeyCode.H) && objectBeingLookedAt.isGatherable ) { RockResourceController resourceController = objectBeingLookedAt.transform.GetComponent<RockResourceController>(); resourceController.GetComponent<PhotonView>().RPC ( "Hatchet", PhotonTargets.All, inventoryController.photonView.viewID ); }

// Inside RockResourceController [RPC] public void Hatchet ( int playerInventoryPhotonViewId ) { Dictionary<Item, int> hatchedItems = new Dictionary<Item, int>();

foreach &#40; KeyValuePair&lt;Item, int&gt; pair in inventoryController.itemsDictionary &#41; {
    int nrOfItems = Mathf.Clamp&#40; Random.Range&#40;minHatchet, maxHatchet&#41;, 1, pair.Value &#41;;
	
    hatchedItems.Add &#40; pair.Key, nrOfItems &#41;;
    inventoryController.RemoveItem&#40; pair.Key, nrOfItems &#41;;
}

PhotonView playerInventoryPhotonView = PhotonView.Find&#40; playerInventoryPhotonViewId &#41;;
InventoryController playerInventoryController = playerInventoryPhotonView.GetComponent&lt;InventoryController&gt;&#40;&#41;;

playerInventoryController.AddItems&#40; hatchedItems &#41;;

if &#40; inventoryController.isEmpty &#41; {
    PhotonNetwork.Destroy&#40; this.gameObject &#41;;
}

}[/code2] Any suggestions on how to improve this workflow, if possible?

Thanks!

Comments

Tobias
2014-04-29 12:57:52

RPCs don't have a return value, because they could actually be running on any number of remote clients - including none at all. Which return code would you want? If you need some reaction, you will have to send another RPC to the origin of some RPC that needs a reply.

At the moment, I can't analyze your code. It's more effort to analyze what you DO versus what you MIGHT want to do. If you can describe what you need to do without posting code, it might help get a more directed reply.

Back to top