question

Flarvain avatar image
Flarvain asked

Struggling to extraploate specific values out of a key from catalog customdata

Hi All,

This is definitely a newbie question, but i'm currently doing a adding building function in my game. I have a catalogue called buildings that i am hoping to use to work out how long a building should take to build and the relative cost at each level of the building.

I have most of the other pieces of this function working but i am basically wanting to compare the inventory item's current level with the catalogue items build time and cost for that level.

I'll be storing the start build time in internal data read only and the finish time there as well inside of a key marked with the instanceID of the building im making.

The data being stored in my Catalog item looks like this:

In order to get finish time, i'm getting my catalog item:

then getting the custom data for that catalog item:

For now just to see what this data turns out as i've been pushing this to internal read only data to see what the result is.

I can see that if i push my catalogItemCustomData straight up like this:

Then i get a result like this:

That's great, so I thought okay, i now want to access point 0 in that array, so i changed my function to be:

Now it only returns the first character in the array which is "{"

How do i need to cast this to return the first point in the array? Ideally i'll be making that [0] the [currentlevel-1] from my instanceID's custom data.

Thanks,

Flarvain

1 comment
10 |1200

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

Flarvain avatar image Flarvain commented ·

None of my images came through....

    var catalog = server.GetCatalogItems({ CatalogVersion: "Buildings" });
    var catalogItem = null;
    for (var c = 0; c < catalog.Catalog.length; c++) {
        if (args.ItemID === catalog.Catalog[c].ItemId)
            catalogItem = catalog.Catalog[c];
    }


        var stringCombo = "StartBuildTime" + myNewInstanceID
        var serverStartBuildingTime = {};
        serverStartBuildingTime[stringCombo] = now.getTime();
        var StartBuildingTime = server.UpdateUserReadOnlyData({PlayFabId: currentPlayerId, Data: serverStartBuildingTime})
        
        var catalogItemCustomData = JSON.parse(catalogItem.CustomData) // custom data returns a string that contains all of your custom data as one really long string, 

        
        var BuildTimeCustomData =  catalogItemCustomData["BuildTime"]// you can now access the key value pair.... result of this is {30,60,90,120} etc
        var CustomDataCompileKey = {BuildTime: BuildTimeCustomData} 
        var TestData = server.UpdateUserReadOnlyData({PlayFabId: currentPlayerId, Data: CustomDataCompileKey})
0 Likes 0 ·
Flarvain avatar image
Flarvain answered

Amazing - Thank you so much @Citrus Yan,

I replaced:

var BuildTimeCustomData =  catalogItemCustomData["BuildTime"][0]

With:

var BuildTimeCustomData =  JSON.parse(catalogItemCustomData["BuildTime"])[0]

I had tried this prior but it was when i still had brackets like {}. Thank you!

1 comment
10 |1200

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

Citrus Yan avatar image Citrus Yan commented ·

Glad it helped, happy coding!

0 Likes 0 ·
Citrus Yan avatar image
Citrus Yan answered

Hi, could you please add the missing images so that we can better understand your question? To do that, you can use the "upload image" button in the editor:


4 comments
10 |1200

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

Flarvain avatar image Flarvain commented ·

Hmmm it looks like i can't edit the original question so i'll add them here with the sentences from above:

The data being stored in my Catalog item looks like this:

In order to get finish time, i'm getting my catalog item:

    var catalog = server.GetCatalogItems({ CatalogVersion: "Buildings" });
    var catalogItem = null;
    for (var c = 0; c < catalog.Catalog.length; c++) {
        if (args.ItemID === catalog.Catalog[c].ItemId)
            catalogItem = catalog.Catalog[c];

then getting the custom data for that catalog item:

var catalogItemCustomData = JSON.parse(catalogItem.CustomData)

I can see that if i push my catalogItemCustomData straight up like this:

        var BuildTimeCustomData =  catalogItemCustomData["BuildTime"]
        var CustomDataCompileKey = {BuildTime: BuildTimeCustomData} 
        var TestData = server.UpdateUserReadOnlyData({PlayFabId: currentPlayerId, Data: CustomDataCompileKey})

I get a result like this:

trying to access point 0 in the array:

        var BuildTimeCustomData =  catalogItemCustomData["BuildTime"][0]

Return in internal data:

{ (i can't add another image apparently but it just returns the first character)

0 Likes 0 ·
Citrus Yan avatar image Citrus Yan Flarvain commented ·

Actually, {30,60,120,240,...} is not a correct representation of an array, instead, [30,60,120,240,...] is the correct format of an array. Maybe that's where your issue coming from.

0 Likes 0 ·
Flarvain avatar image Flarvain Citrus Yan commented ·

Hi Citrus Yan,

Thanks for the reply. I had tried that in the past thinking the same thing but with the code:

var catalogItemCustomData = JSON.parse(catalogItem.CustomData) 

        
var BuildTimeCustomData =  catalogItemCustomData["BuildTime"][0]
var CustomDataCompileKey = {BuildTime: BuildTimeCustomData}
var TestData = server.UpdateUserReadOnlyData({PlayFabId: currentPlayerId, Data: CustomDataCompileKey})

And the catalogue setup like:

I get this result:

There's a conversion that im missing because it's reading this only a string, but im not sure how to do this.

Thanks for your help

0 Likes 0 ·
Show more comments

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.