question

Joshua Strunk avatar image
Joshua Strunk asked

JS static properties and Cloud Script

What is the scope of static properties on an object in Cloud Script?

From my experimentation it seems to be just whatever server the Cloud Script happens to be getting called on. Is this assumption correct?

Is it okay to use static properties on an object in Cloud Script, as long as I reset the properties on every call to Cloud Script?

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.

brendan avatar image
brendan answered

On a small scale, with a few players, that is correct. However, at large scale, it's really non-deterministic.

Think of it this way - the Cloud Script engine is running for your clients as a web service. If the number of your players is large, we'll need to have multiple servers running, to ensure that we have sufficient capacity to manage all your players. And a player will be sent to a server based upon availability - not past history. So while static data which is meant to be the same across all players will work to some extent in Cloud Script, that data may have to be re-loaded when a new engine is created to handle load. And, of course, data which is unique per-player should always be re-loaded.

Does that help to answer your questions concerning data usage? If not, please feel free to detail out the scenarios you have in mind, and we'll provide feedback on the expected results.

10 |1200

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

Joshua Strunk avatar image
Joshua Strunk answered

Thank you. I it sounds like it is working how I expected. Just to be sure I am using this to cache data in a static space across single Cloud Script calls not to do some crazy cross Cloud script call operation. The idea being I reset that static data at the top of all of my handlers functions. Really simplified example below

handlers.exampleCSCall(args) {

   Data();

   Data.staticProp = args.CharacterId;

   someOperationOnCharacter();

}

function Data(){

   Data.staticProp = null;

}

function someOperationOnCharacter(){

  var charId = Data.staticProp;

}

10 |1200

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

brendan avatar image
brendan answered

Sure, within the context of a single request - that is to say, when you call a handler via ExecuteCloudScript - and even if that handler calls other functions (as in your example), your data would be consistent. Each engine can run multiple scripts at the same time, but each executing script has its own memory space during its lifespan.

10 |1200

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

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.