question

Daniel Otto avatar image
Daniel Otto asked

Using Object - Set Objects via Cloudscript - numbers are always converted to booleans

Hi,

Somehow all numbers in objects passed into entity.SetObjects are converted to booleans - when i return the state of the objects as payload they all have the desired structure with integers - but when checking the object content on developer.playfab.com all numbers are replaced with booleans.

I don't have a Javascript background so my first thoughts was it must have to do something with it's auto conversion and i tried to force the numbers with new Number() - but as the payload object is perfectly fine my assumption is that the object is converted internally by SetObjects.

var groupObjects = entity.GetObjects({Entity: groupEntityKey});
var groupStats = {};
if(groupObjects.Objects.hasOwnProperty("Stats")){
    if(groupObjects.Objects.Cartels.hasOwnProperty("DataObject")){
        groupStats = cityObjects.Objects.Stats.DataObject;
        groupStats.push({Stat: args.Stat, Slot: groupStats.length});
    }
    else{
        groupStats= [{Stat: args.Stat, Slot: new Number(0)}];
    }
}
else{
    groupStats= [{Stat: args.Stat, Slot: new Number(0)}];
}
var setObjectsResponse = entity.SetObjects({Entity: groupEntityKey, Objects: [{DataObject: groupStats, DeleteObject: false, ObjectName: "Stats"}]});


return { result: 1, errorCode: -1, payload: groupStats};

Is there a way to deal with this behaviour?

apisCloudScriptentities
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

·
Sarah Zhang avatar image
Sarah Zhang answered

JavaScript is a weakly typed language. Different from strongly typed languages, this means that JavaScript will figure out what type of data you have and make the necessary adjustments so that you don’t have to redefine your different types of data. Please refer to the corresponding websites for more about the knowledge and syntax of Js.

The Number() function is designed to convert the values of other types to numbers. The function new Number(0) would build a new Number object. The statement new Number(0) won’t return a number, it’s by design. In your case, you can use the number – 0 as the field value directly.

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.