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 Execute HelloWorld from CC

Discussion in 'Cloud Code' started by pKallv, Apr 9, 2024.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,198
    I have been able to upload my HelloWorld c# code to CC but struggle to execute from my code. I have been testing a lot but I am not enough experienced to get this to work.

    Screenshot 2024-04-09 at 07.32.13.png

    Code (CSharp):
    1. // This file was generated by Cloud Code Bindings Generator. Modifications will be lost upon regeneration.
    2. using System.Collections.Generic;
    3. using System.Threading.Tasks;
    4.  
    5. namespace Unity.Services.CloudCode.GeneratedBindings
    6. {
    7.     public class HelloWorldBindings
    8.     {
    9.         readonly ICloudCodeService k_Service;
    10.         public HelloWorldBindings(ICloudCodeService service)
    11.         {
    12.             k_Service = service;
    13.         }
    14.  
    15.         public async Task<string> SayHello(string name)
    16.         {
    17.             return await k_Service.CallModuleEndpointAsync<string>(
    18.                 "HelloWorld",
    19.                 "SayHello",
    20.                 new Dictionary<string, object>()
    21.                 {
    22.                     {"name", name},
    23.                 });
    24.         }
    25.     }
    26. }
    27.  
    Execute from:

    Code (CSharp):
    1. async Task CallCC()
    2.         {
    3.          
    4.             txt_Log.text += ">>>> CallCC <<<<\n";
    5.             //var arguments = new Dictionary<string, object> { { "name", "Peter Källviks" } };
    6.             //var response = await CloudCodeService.Instance.CallEndpointAsync<HelloWorldBindings>("SayHello", arguments);
    7.  
    8.             try
    9.             {
    10.                 // Call out to the RollDice endpoint in the HelloWorld module in Cloud Code
    11.                 var response = await CloudCodeService.Instance.CallModuleEndpointAsync<CloudCodeResponse>("HelloWorld",
    12.                     "SayHello", new Dictionary<string, object>() { { "name", "Peter" } });
    13.  
    14.                 txt_Log.text += response + "\n";
    15.             }
    16.             catch (CloudCodeException ex)
    17.             {
    18.                 txt_Log.text += "CloudCodeException: " + ex + "\n";
    19.             }
    20. }
    Code (CSharp):
    1. DeserializationException: Error converting value "Hello, Peter!" to type 'HWTest+CloudCodeResponse'. Path '', line 1, position 15.
    2. Unity.Services.CloudCode.Internal.Http.JsonObject.GetAs[T] (Unity.Services.CloudCode.Internal.Http.DeserializationSettings deserializationSettings) (at ./Library/PackageCache/com.unity.services.cloudcode@2.6.1/Runtime/com.unity.services.cloudcode.internal/Http/JsonObject.cs:94)
    3. Unity.Services.CloudCode.CloudCodeInternal.DeserializeOutput[TResult] (Unity.Services.CloudCode.Internal.Response`1[T] result) (at ./Library/PackageCache/com.unity.services.cloudcode@2.6.1/Runtime/CloudCode.cs:98)
    4. Unity.Services.CloudCode.CloudCodeInternal.CallModuleEndpointAsync[TResult] (System.String module, System.String function, System.Collections.Generic.Dictionary`2[TKey,TValue] args) (at ./Library/PackageCache/com.unity.services.cloudcode@2.6.1/Runtime/CloudCode.cs:62)
    5. HWTest.CallCC () (at Assets/Scripts/HWTest.cs:93)
    6. HWTest.Awake () (at Assets/Scripts/HWTest.cs:43)
    7. System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) (at <58ad0307846e4e2aa7b395ea5a7758f5>:0)
    8. UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at /Users/bokken/build/output/unity/unity/Runtime/Export/Scripting/UnitySynchronizationContext.cs:153)
    9. UnityEngine.UnitySynchronizationContext.Exec () (at /Users/bokken/build/output/unity/unity/Runtime/Export/Scripting/UnitySynchronizationContext.cs:83)
    10. UnityEngine.UnitySynchronizationContext.ExecuteTasks () (at /Users/bokken/build/output/unity/unity/Runtime/Export/Scripting/UnitySynchronizationContext.cs:107)
    11.  
     
    Last edited: Apr 9, 2024
  2. samg-unity

    samg-unity

    Unity Technologies

    Joined:
    Mar 23, 2021
    Posts:
    45
    Hi @pKallv

    Its hard to tell whats gone wrong with what you've shared already, so I'll share some of what I have and maybe that helps to resolve the issue.

    My C# module simply looks like this:

    Code (CSharp):
    1. using Unity.Services.CloudCode.Core;
    2.  
    3. namespace Sample;
    4.  
    5. public class HelloWorld
    6. {
    7.     [CloudCodeFunction("SayHello")]
    8.     public string SayHello(string name)
    9.     {
    10.         return $"Hello {name}!";
    11.     }
    12. }
    After generating the bindings which look the same as yours, I've utilised the generated bindings like so:

    Code (CSharp):
    1. using Unity.Services.Authentication;
    2. using Unity.Services.CloudCode;
    3. using Unity.Services.CloudCode.GeneratedBindings;
    4. using Unity.Services.Core;
    5. using UnityEngine;
    6.  
    7. public class TestModule : MonoBehaviour
    8. {
    9.     private async void Start()
    10.     {
    11.         // Initialize the Unity Services Core SDK
    12.         await UnityServices.InitializeAsync();
    13.  
    14.         // Authenticate by logging into an anonymous account
    15.         await AuthenticationService.Instance.SignInAnonymouslyAsync();
    16.  
    17.         try
    18.         {
    19.             var txt = ">>>> Say Hello <<<<\n";
    20.  
    21.             // Call the function within the module and provide the parameters we defined in there
    22.             var module = new HelloWorldBindings(CloudCodeService.Instance);
    23.             var result = await module.SayHello("Peter");
    24.  
    25.             //Debug.Log(result);
    26.  
    27.             txt += result;
    28.  
    29.             Debug.Log(txt);
    30.         }
    31.         catch (CloudCodeException exception)
    32.         {
    33.             Debug.LogException(exception);
    34.         }
    35.     }
    36. }
    After deploying the HelloWorld C# Module and running the above script, I get the expected output
    upload_2024-4-10_10-37-59.png
     
    Marvin_Wack and GabKBelmonte like this.
  3. GabKBelmonte

    GabKBelmonte

    Unity Technologies

    Joined:
    Dec 14, 2021
    Posts:
    149
    Marvin_Wack and samg-unity like this.
  4. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,198
    I think I get it now and I believe I mixed the bindings file with the example.cs file you posted above.

    Given your example I did get it working earlier but for some reason I thought the logic should be in the bindings file and call that.

    Just for the record whatever I put in the "Example.cs", the file above, it will not compile into the runtime, right?

    A BIG thank you.
     
    samg-unity likes this.
  5. samg-unity

    samg-unity

    Unity Technologies

    Joined:
    Mar 23, 2021
    Posts:
    45
    > Just for the record whatever I put in the "Example.cs", the file above, it will not compile into the runtime, right?

    Using the Cloud Code package "Generate Solution" and "Generate Bindings" options, it should be automatically set up to separate the C# module project from the Unity project.

    For example I have something following this project structure:

    Code (Boo):
    1. > Assets
    2.   > Scenes
    3.   > CloudCode
    4.      > GeneratedModuleBindings
    5.          > HelloWorld
    6.             HelloWorldBindings.cs
    7.      HelloWorld.ccmr
    8.      TestModule.cs
    9.  
    10. > HelloWorld
    11.    > Project
    12.       Example.cs
    13.       HelloWorld.csproj
    14.    HelloWorld.sln
    Which should mean that when deploying the HelloWorld C# Module project nothing within the Assets folder will be deployed to Cloud Code.

    Does that help answer your concern?
     
    Marvin_Wack and pKallv like this.