Hello!
I have a project which uses Unity Authentication, Economy and Cloud Code. My aim is to make a “stockmarket simulator”, with the CC storing a dictionary, referring each Economy currency to a value (iron currently costs 5 credits, steel costs 15 credits, etc.). My current CC JS code is:
// Unity Cloud Code Script: Handle Dictionary with Action Parameter
module.exports = async (params, context) => {
try {
// Check if the 'action' parameter exists
if (!params.action) {
throw new Error("Missing 'action' parameter.");
}
// Process based on the 'action' parameter
if (params.action === "getDictionary") {
const myDictionary = {
"CREDITS": 1,
"IRON": 5
};
return myDictionary; // Return only the dictionary
} else {
throw new Error(`Unknown action: ${params.action}`);
}
} catch (error) {
// Log and return error to the client
return {
success: false,
message: error.message
};
}
};
Now I have a few questions.
a). Do I need to set a parameter for the JS script? If so, which one (string, int, etc.)
b). How do I retrieve the dictionary from a Unity C# script? What I currently have is this, but it is currently not working very well (throws an error):
// Define parameters for Cloud Code call
var parameters = new Dictionary<string, object>
{
{ "action", "getDictionary" }
};
// Call the Cloud Code script and expect a dictionary response
Dictionary<string, int> result = await CloudCodeService.Instance.CallEndpointAsync<Dictionary<string, int>>("ValueManager", parameters);
Debug.Log("Dictionary fetched successfully!");
c). Do I have the CallEndpointAsync function called wrongly? Am I mixing up the parameters?
If anybody can answer any of those questions, I would be very grateful.
Thanks,
Arcane Creator
Bump.
I’d appreciate a response even if it is only for one of the questions.
Also, if you have an alternate solution, that is welcome too
Bump once again.
Sorry for the bumps, but I would like a response soon from anyone.
Thanks
Hi. I can see you’re using Cloud Code JS and trying to pass in an “action” parameter. Did you create that as a string parameter in the dasboard?
I would recommend you check out Cloud Code C# Modules. It offers a “binding generation” which makes interacting with your cloud code way easier.
However, if you want to use Cloud Code JS and receive the response in the form of a dictionary you can do this:
var arguments = new Dictionary<string, object> { { "action", "getDictionary" } };
var response = await CloudCodeService.Instance.CallEndpointAsync<Dictionary<string, object>>("ValueManager", arguments);
int credits = System.Convert.ToInt32(response["CREDITS"]);
int iron = System.Convert.ToInt32(response["IRON"]);
Hope this helps!
1 Like
To build on what Marius is saying, if you’re creating it from the dashboard you need to add parameters
or through the editor :
or in the script through the editor export params:
Or as Marius said, you could use C# modules which are overall more robust.
Gab
Hi there.
Thanks for your answer, it has helped, I have moved on from one error message to another 
I have updated my script to this:
// Define parameters for Cloud Code call
var arguments = new Dictionary<string, object> { { "action", "getDictionary" } };
var response = await CloudCodeService.Instance.CallEndpointAsync<Dictionary<string, object>>("ValueManager", arguments);
Dictionary<string, int> result = new Dictionary<string, int>();
foreach (var keyValuePair in response)
{
Debug.Log($"{keyValuePair.Key}, {keyValuePair.Value}"); //result.Add(keyValuePair.Key, System.Convert.ToInt32(keyValuePair.Value));
}
Now, I am getting three messages.
1). “message, Missing ‘action’ parameter.”
2). “success, False” (This was caused by the first Debug.Log)
3). “Dictionary fetched successfully!” (This was caused by the second Debug.Log)
What I understand is that I am somehow passing the action parameter incorrectly, but I do not understand how. I have added a string parameter in the dashboard called “action”, and my arguments dictionary has the correct (according to you) key and value.
How do I correct this? To be clear, I am trying to get a Dictionary<string, int> out of this.
Thanks
Update from extensive debugging.
The JS script is getting an error and catches it. It sends a dictionary with {(success, False), (message, missing “action” parameter)}.
So somehow the JS script is not getting my getDictionary action parameter. Do you know why and how I can fix this?
Thanks
Hi @Arcane_Creator
So somehow the JS script is not getting my getDictionary action parameter. Do you know why and how I can fix this?
Looking at your shared JS code, I think the issue is with how the arguments are being passed through to your function:
module.exports = async (params, context) => {
...
}
should be
module.exports = async ({ params, context }) => {
...
}
Wow, thanks.
I am a complete beginner to cloud code (and Javascript in general) so I don’t think I would have ever spotted it 
Thanks again
1 Like
Hi @Arcane_Creator,
Did you get your initial script from a template? Im wondering if there’s some doc that needs to be fixed.
Cheers!
Hi
No, I did not use a template, your docs are all fine (as far as I know).
I pieced it together from several different places, and there was quite a bit of copy-pasteing involved (which explains my problem).
Thanks for asking!
1 Like