Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Resolved Error Response from trigger action question

Discussion in 'Cloud Code' started by samhain323, May 15, 2024.

  1. samhain323

    samhain323

    Joined:
    Aug 4, 2020
    Posts:
    37
    So I'm using the com.unity.services.cloud-save.key-saved.v1 trigger via cloud code module.

    Basically, when playerDataA is changed, the trigger calls a function that then updates playerDataB with some of playerDataA's properties. It then saves the updated data.

    The update works (playerDataB is changed), and I get a successful response from trigger action, but then I get an error response from trigger action.

    I'm guessing it has something to do with the update of playerDataB also causing the trigger, but I'm not sure what I can do about it. Is it a bug or a feature? Below is the trigger method. The only thing the HandlePlayerDataUpdate() method does is update and save the data. If the trigger is called a second time, that method will not be called because they have different key Ids.

    Code (CSharp):
    1.        
    2. [CloudCodeFunction(ServerFunction.HandleKeySavedEvent)]
    3.         public async Task HandleKeySavedEvent(IExecutionContext context, string id, string idType, string key, bool valueIncluded, string value, string accessClass, string modifiedDate)
    4.         {
    5. _logger.LogDebug($"HandleKeySavedEvent(1) id: {id} idType: {idType} accessClass: {accessClass} modifiedDate: {modifiedDate} key: {key} valueIncluded: {valueIncluded} value: {value}");
    6.  
    7.             switch (key)
    8.             {
    9.                 case ServerKeys.PlayerData:
    10.                     await HandlePlayerDataUpdate(context, id, value);
    11.                     break;
    12.                 default:
    13.                     break;
    14.             }
    15.             _logger.LogDebug($"HandleKeySavedEvent(2)");
    16.         }
     
  2. samg-unity

    samg-unity

    Unity Technologies

    Joined:
    Mar 23, 2021
    Posts:
    47
    Hi @samhain323

    > I'm guessing it has something to do with the update of playerDataB also causing the trigger, but I'm not sure what I can do about it.

    Are you already using a filter on the trigger you created? https://docs.unity.com/ugs/en-us/manual/cloud-code/manual/triggers/concepts/filters. If not this could prevent the re-trigger of the script on the playerDataB update as you could move your logic on checking the key to the trigger.

    Code (JavaScript):
    1. data['key'] == 'playerDataA'

    Please note
    that unfortunately you can only apply the filter on creation directly via the REST API at this time.

    > I get a successful response from trigger action, but then I get an error response from trigger action.

    If you could provide any further information on the error response you received, that could help clarify the issue if you're already using filters.

    Thanks!
     
    samhain323 likes this.
  3. samhain323

    samhain323

    Joined:
    Aug 4, 2020
    Posts:
    37
    I am not using a filter, I'll give that a shot, thanks.

    As to the error detail, it did not seem to be very informative, other than a severity number (I changed the code with a workaround, so I'm not getting the error any longer).
     
  4. samhain323

    samhain323

    Joined:
    Aug 4, 2020
    Posts:
    37
    Meaning I cannot add it to the triggers-config.tr file?
     
  5. samg-unity

    samg-unity

    Unity Technologies

    Joined:
    Mar 23, 2021
    Posts:
    47
  6. samhain323

    samhain323

    Joined:
    Aug 4, 2020
    Posts:
    37
    Last edited: May 20, 2024
  7. samhain323

    samhain323

    Joined:
    Aug 4, 2020
    Posts:
    37
  8. samg-unity

    samg-unity

    Unity Technologies

    Joined:
    Mar 23, 2021
    Posts:
    47
    I put together the following js script to create an example trigger with a filter.

    You'll need to create a service account key with a `Trigger Configuration Editor` project role - see Dashboard > Admininistration > Service Accounts.

    Code (JavaScript):
    1. const axios = require("axios-1.6");
    2.  
    3. module.exports = async ({ params, context, logger }) => {
    4.   let result;
    5.   try {
    6.     // Create a service account and base64 encode the <KEY_ID>:<SECRET_KEY> values (with the colon character)
    7.     const credentials = `REPLACE_WITH_YOUR_BASE64_ENCODED_VALUE`;
    8.     const url = `https://services.api.unity.com/triggers/v1/projects/${context.projectId}/environments/${context.environmentId}/configs`;
    9.     const data = {
    10.       "name": "example-cloud-code-trigger",
    11.       "eventType": "com.unity.services.cloud-save.key-save.v1",
    12.       "actionType": "cloud-code",
    13.       "actionUrn": "urn:ugs:cloud-code:MyTestModule/methodName",
    14.       "filter": "data[\"parameter\"] == \"value\""
    15.     }
    16.     const headers = {
    17.       'Content-Type': 'application/json',
    18.       'Authorization': `Basic ${credentials}`
    19.     };
    20.  
    21.     result = await axios.post(url, data, {headers: headers});
    22.  
    23.     return result.data;
    24.   } catch (err) {
    25.     logger.error("Triggers API request failed", {"error.message": err.message});
    26.     throw err;
    27.   }
    28. };
     
    samhain323 likes this.
  9. samhain323

    samhain323

    Joined:
    Aug 4, 2020
    Posts:
    37
    great, thank you!