FAQs - Cloud Code
Working with Cloud Code scripts?
Can I send requests from Cloud Code scripts?
Yes. You can use SparkRequests. For details on how to do this, see the Using SparkRequests API tutorial.
Can I send requests on behalf of other players in Cloud Code?
Yes. You can use use SparkRequests + ExecuteAs(). For details on how to do this, see the Using SparkRequests API tutorial.
Can I send HTTP requests to a 3rd party endpoint from Cloud Code?
Yes. You can do this with SparkHttp. The supported ports are - 80, 8080, and 443.
How do I set or overwrite a specific sub-field in a player's scriptData/privateData?
You can use dot notation for this. For example:
Is there a way I can use Cloud Code for building geo-location based game mechanics?
Yes. You can use Cloud Code to store geo-location data in Redis for rapid retrieval. For an example that shows you how to do this, see this Tips & Tricks article.
I understand that some Cloud Code scripts have player context and some don't. What is the significance of this?
Scripts that have player context can make use of the SparkPlayer API, which is very useful for some typical use cases. For details of which scripts have player context and which don't, together with some examples of how to use scripts with player context, please see this Tips & Tricks article.
Scripts Timing Out?
Can I extend the timeout threshold on the Cloud Code scripts in my game?
No. There is a hard limit of 10 seconds imposed on all Cloud Code scripts for games in the Development or Standard Pricing Tiers. If you feel you require a longer timeout threshold for Cloud Code scripts in your game, please contact us to discuss the Enterprise Pricing Tier.
Why is there a timeout limit imposed on Cloud Code scripts?
API Requests which exceed the timeout limit indicate a non-scalable solution. If you adhere to best practices when creating your Cloud Code scripts, the current limit is far more than you should ever need.
Why is my Cloud Code script timing out?
This is typically caused by having too many instructions or operations being executed within the context of a single script.
Problematic scripts are those that are likely to compromise the performance of your game. Here are some common questions relevant to refactoring these scripts to optimize them.
Are there any tools I can use to check and monitor my Cloud Code scripts to find scripts that are compromising my game's performance?
Yes. There are several tools built into GameSparks designed to help you manage and improve the performance of the Cloud Code scripts in your game. For a survey of these tools and advice on how to best exploit them, see this Tips & Tricks article.
How do I ensure database queries embedded within my Cloud Code scripts are optimized?
Queries should be executed on indexed fields to ensure timely and efficient interactions with the database. The database is intended for transactional use, and it's good practice to keep this in mind at all times when creating your Cloud Code scripts.
Should I avoid performing complex numerical calculations in my Cloud Code scripts?
Yes. Complex numerical calculations can be costly in terms of performance. If you use these frequently within your scripts, you can think about offloading some of this calculation work to the client in an effort to distribute the load. The same applies to the use of algorithmic design patterns or complex string manipulations within your Cloud Code scripts.
Are there any performance risks when nesting Require modules in my Cloud Code scripts?
Yes. If you include Require modules in a request, or nest them within another module, keep in mind the following risks. The Require statement inlines Cloud Code into the current context and this takes time, because parsing has to be performed and so on. We recommend that you exercise caution and refrain from nesting modules or including any modules that are not needed, because this will add to the overall execution time of your Cloud Code script. Note also that you can use requireOnce to prevent the duplicate inlining of Cloud Code.
Can using the SparkRequests constructor compromise my Cloud Code scripts?
Yes. You can construct Requests and send them from within Cloud Code and any logic associated with the Requests will be executed also. The execution time for these constructed Requests is added to the execution time needed for the Request they were sent from. This can cause the parent Request to exceed the 30 second limit and timeout.
Is using SparkHTTP requests to 3rd party APIs within my Cloud Code scripts a good idea?
We recommend caution when doing this. SparkHttp Requests to 3rd party API's are not reliable and can often be the reason why Cloud Code scripts timeout. This is because SparkHttp is blocking, therefore contributing to increased execution time.
More Information? Please review the Cloud Code Best Practices page for more information.