question

Ozan Yilmaz avatar image
Ozan Yilmaz asked

How to read this custom data in Cloudscript?

Hello,

I'm trying to read "AmmoPrice" custom data in Cloudscript. This is how it is declared.

This is how I try to read it, however, the variable "price" is null in this code.

    // GetItemByID calls GetCatalogItems function and returns the matched item. It doesn't return null in this case
    var item = GetItemByID(args.ItemID);
    var itemCustomData = JSON.parse(item.CustomData);
    var price = parseInt(itemCustomData["AmmoPrice"]["Money"]);

It's interesting though. When I try to read "Price" custom data, I can read it without a problem, but when the custom data is JSON, I cannot read it.

Also, I can read "AmmoPrice" in Unity as JSON, and separate it into "Money" and "Coins"

01.jpg (17.7 KiB)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Rick Chen avatar image
Rick Chen answered

In your case, the CustomData was parsed to JSON on the outer layer, which would be:

Object {Price: 5, AmmoPrice: ”{\”Money\”:2,\”Coin\”:1}”}

Notice that the itemCustomData["AmmoPrice"] is a string rather than an object. I think you need to parse itemCustomData["AmmoPrice"] as well before accessing the “Money” key, otherwise it will return undefined.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Ozan Yilmaz avatar image
Ozan Yilmaz answered

@Junjin Chen

EDIT: My bad, parsing the value again works

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.