Documentation

Did you find this helpful?

Creating a Scheduled Task

This tutorial walks you through how to create a task that runs on a schedule. There are many game operation routines that can be automated using a scheduled task, such as modifying prices in a store according to the time of the day, updating Title Data to reflect changes for a current event, injecting virtual currencies into the game economy daily, etc. In this example, we show you how to modify a game variable "rareDropRate" in the Title Data at 12:00 UTC, and only on weekend days.

Step 1 - Prepare the Cloud Script

Go to Servers -> Cloud Script. Add a Cloud Script function called "adjustRareDropRate" with a simple SetTitleData API as shown (for the sharp-eyed, don’t worry - there’s a bug in there on purpose). Make sure you deploy the new revision, so that it is live in your game. You can learn more about using Cloud Script in our Cloud Script tutorial and documentation.

handlers.adjustRareDropRate = function(args) {
    // Tutorial demo Cloud Script
    serverAPI.SetTitleData({
        "Key": "rareDropRate",
        "Value": args.dropRate
    });
}

Step 2 - Create a scheduled task

Now go to Servers -> Tasks, and click NEW TASK on top right corner to bring up the create task view. In the “Type of task” field, choose “Run a Cloud Script function”. Below that, you’ll be able to pick a function from the currently deployed revision of Cloud Script and specify arguments to pass in. Choose the adjustRareDropRate handler you just wrote, above.

To set the schedule for this task, choose "On a schedule (UTC)" under the SCHEDULE header. A simple schedule builder here lets you choose when the task should run (every hour, day, week, etc). Since we would like an advanced schedule where we can specify which days of the week to run in this case, click on "CRON EXPRESSION". The highly customizable cron expression allows you to build a very complex schedule, though it’s important to note that we currently only allow schedules whose occurrences happen on 5-minute marks of the hour. For example, you may specify a task to run on 5th, 10th, 25th,or 50th minute of the hour, but you may not specify a task to run on the 3rd, 11th, or 46th minute of the hour. If you'd like to learn more about Cron expression, crontab.guru provides rich information and an interactive expression builder. In this case, we want the task to run at 12:00 UTC on Saturdays and Sundays, which would be “00 12 * * 0,6” (zero minutes past twelve o’clock, every Sunday and Saturday).

Finally, make sure you save the new task before moving on to the next step.

Step 3 - Test the task

On the Tasks view (Servers -> Tasks), we can see that the next run of the newly created task is on the following Saturday - as expected. So, if it’s currently Tuesday, then the next scheduled runtime would be 4 days away. To test the task now, select the "Adjust rare drop rate" task and click "RUN TASKS".

Step 4 - Check the results of the test run

Unfortunately, the task run has failed. Click into the task instance to see what went wrong.

4_RunDropRateTaskFailed.PNG#asset:549

The Task Instance Detail view provides diagnostic information on why the task failed. In this case, it’s pointing out that the call to "serverAPI.SetTitleData" is incorrect. It should really be "server.SetTitleData" (have a look at the “Calling the Server APIs” section of the Cloud Script tutorial). There’s also other important information on this view such as the start and end times, the function that was called, any arguments passed in, the full Cloud Script execution result, etc.

5_RunDropRateTaskFailureDetail.PNG#asset:550

Step 5 - Test again (successfully)

Go ahead and fix the error we found on step 4 here is the correct code snippet:

handlers.adjustRareDropRate = function(args) {
    // Tutorial demo Cloud Script
    server.SetTitleData({
        "Key": "rareDropRate",
        "Value": args.dropRate
    });
}

And now, repeating Step 3, your final test run is successful.

6_RunDropRateTaskSucceeded.PNG#asset:551

We should go to Content -> Title Data to verify that the Title Data entry actually get set.

7_RunDropRateTaskTitleDataResult.PNG#asset:552



Did you find this helpful?