Test running the script

The Unity Dashboard allows for test-running scripts in the environment in which they are saved. Unity uses the same environment when communicating with other Unity services through Cloud Code scripts. The images in this topic demonstrate a test run of the sample script that simulates rolling a six-sided die.

See Limits for more information on script execution limitations.

Generating a test Player ID

An authenticated game client calls Cloud Code scripts; the implication being that a player took an action that would trigger the game client to execute a specific script. To simulate this, use the Cloud Code editor to generate an anonymous Player ID and access token that is then exposed to the script at runtime. Player IDs are generated for the currently selected environment and do not exist in any of the other environments for the project.

These anonymous player access tokens are valid for one hour before they expire, and you can generate a new token by selecting Generate in the Run Code tab’s Player ID section.

These anonymous Player IDs are useful because you can trace the changes made in other services during a test run, for example, when using the Cloud Save or Economy SDKs in a Cloud Code script.

Anonymous Player IDs persist throughout browser sessions and are prepopulated for each project. This allows for a single Player ID to be used between multiple scripts, which can simulate the usage from a game client.

Test runs are not possible without a valid Player ID. If the Player ID has expired, select Run to view a dialog that describes the error.

Input parameters

The input fields of the parameters match the types of the input parameters specified in the Details tab. This means, for instance, that a Boolean parameter can only be true or false, and a numeric parameter can only be a number.

You must always supply the required input parameters for a test run. If you don’t, the dashboard indicates the missing field with an asterisk next to the parameter name, and a red error label if you interacted with the parameter input but didn’t complete it.

Testing previous versions

Selecting a version from the list of versions updates the code and the parameters in the Run Code tab. This makes it possible to run previously published versions of a script, which you can use for debugging, for example, if a newer version has introduced a bug that did not exist in older versions.

Test run response

Cloud Code populates the Run Code tab’s Response/Logs/Request section when you execute a test run. Execute a test run by selecting Run at the top of the Parameters section.

  • The Response tab shows the amount of time it took to execute the script, including network latency between the browser and the Cloud Code service. It also shows what the script returned during its execution.
  • The Logs tab shows any logged information captured during the execution of the function.
  • The Request tab shows the input parameters the script was called with.

Debugging, handling, and throwing errors

The simplest way to debug a script that is not behaving as expected is to add log lines to the script to identify where the script is going wrong.

The Response tab and the script editor help indicate where errors have occurred, although those can only go so far in helping to debug problems with the script.

The Response tab shows the line and column of an error. The debugger uses this information and adds an annotation to visually indicate where the error occurred in the Script Code section.

The error annotation stays visible until you make edits to the script or when you run the script again.

The following is an example of how you can handle and throw an error.

Copy
JavaScript
const { CurrenciesApi } = require('@unity-services/economy-2.3');

module.exports = async ({ params, context, logger }) => {
  const { playerId, projectId } = context;
  const currencies = new CurrenciesApi(context);
  const { currencyId } = params;
  try {
    const setBalance = await currencies.setPlayerCurrencyBalance({
      projectId,
      playerId,
      currencyId,
      currencyBalanceRequest: {
        currencyId,
        balance: 100,
      }
    });
  } catch (err) {
    if (err.response) {
      logger.error(
        JSON.stringify({
          response: err.response.data,
          status: err.response.status,
          headers: err.response.headers,
        })
      );
    } else {
      logger.error(JSON.stringify(err.message));
    }
    throw Error('oops');
  }
};

Error handling

Use the error code or error type instead of the error message returned from the Cloud Code API error response for your error handling logic. Error messages might change in the future, but error codes and type will remain the same.