question

Josh Hughes avatar image
Josh Hughes asked

Cloudscript: Access property within SetObjects[]

I'm trying to access data that I will eventually be passing into the objects.SetObjects() function to update my Entity Objects for a player.

However, whenever I'm trying to access 'Currency' or any proeprty in the object it returns null or an error.

{
                            "DataObject": {
                                "Currency": 9545673828.105536,
                                "WorldLastUpdateTime": 637548279843703300,
                                "PerSecondIncome": 1279134.8219332695,
                                "Generator_0": {
                                    "GeneratorLevel": 49,
                                    "IsManual": false,
                                    "OwningWorld": "PlayerData_World0",
                                    "Automate": 1,
                                    "Increase_Speed": 50,
                                    "Increase_Yield": 25
                                },
                                "Generator_1": {
                                    "GeneratorLevel": 329,
                                    "IsManual": false,
                                    "OwningWorld": "PlayerData_World0",
                                    "Automate": 1,
                                    "Increase_Speed": 20,
                                    "Increase_Yield": 25
                                }
                            },
                            "DeleteObject": null,
                            "EscapedDataObject": null,
                            "ObjectName": "PlayerData_World0"
                        }

Above is an example of the data I'm passing in, and what it looks like when I print the entire object. I'm attempting to access the 'Currency' property but I can't get it to work.

I've tried;

worldData.Currency

worldData["Currency"]

worldData["0"] (which just returns above)

worldData.DataObject

worldData.Objects

worldData["Objects"]

worldData["DataObject"]

I've also tried JSON.parse(worldData)

and JSON.parse(worldData.DataObject)

and JSON.parse(JSON.parse(worldData.DataObject))

and none of it seems to work. I feel like its a simple solution, but I'm not as familiar with js myself yet and couldn't find anything online.Easiest solution would be for me to pass the value I want in along with the args from Unity but I want to avoid that if possible since its essentially duplicating data.

CloudScript
10 |1200

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

1 Answer

·
Citrus Yan avatar image
Citrus Yan answered

Simply use "worldData.DataObject.Currency" should be able to access the "Currency" property. Here is code sample:

var worldData = {
    "DataObject": {
        "Currency": 9545673828.105536,
        "WorldLastUpdateTime": 637548279843703300,
        "PerSecondIncome": 1279134.8219332695,
        "Generator_0": {
            "GeneratorLevel": 49,
            "IsManual": false,
            "OwningWorld": "PlayerData_World0",
            "Automate": 1,
            "Increase_Speed": 50,
            "Increase_Yield": 25
        },
        "Generator_1": {
            "GeneratorLevel": 329,
            "IsManual": false,
            "OwningWorld": "PlayerData_World0",
            "Automate": 1,
            "Increase_Speed": 20,
            "Increase_Yield": 25
        }
    },
    "DeleteObject": null,
    "EscapedDataObject": null,
    "ObjectName": "PlayerData_World0"
}
console.log(worldData.DataObject.Currency)
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.