question

admin-12 avatar image
admin-12 asked

How can I generate and provide a random number through Cloud Script?

The gameplay of my game is dependent upon randomness. I've created deterministic random logic that will function based around a single seed value. This allows each client to technically generate the same randomness, which allows everything to be in sync. What I'm looking to do is generate a random int value from within a Cloud Script that can be given to each player in the game. My setup utilizes Photon as the multiplayer backend, so all of those handlers are available. The number that's generated and given to all the players in a room needs to be the same number. Each match should have a different random number. Is it possible to create this sort of flow?

CloudScript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
brendan avatar image
brendan answered

Is this a number you only need to generate once for the game session? If so, you could use a Webhook call (https://doc.photonengine.com/en-us/realtime/current/reference/playfab) to trigger generation of the number, write it to Shared Group Data, and have the players read it from there (use the Room ID to come up with the Shared Group Data ID). Then, the players would request that data from the Shared Group. And you could use the RoomClosed webhook to then clean up that Shared Group Data.

Now, a caution - this is not a way to drive anything realtime for games. If you need to do this multiple times during the game session, this wouldn't be the way to go. Cloud Script is designed to provide a way for a client to request some small server-side logic to run occasionally. If a higher rate of data exchange is needed for this, a dedicated server (custom game server) or Photon Enterprise would be the way to go.

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

admin-12 avatar image admin-12 commented ·

Your suggestion is exactly what I was looking for! The random value is something that would only need to be generated a single time at the very beginning of each match. From there local game logic will do the rest.

Thanks a bunch for the help!

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.