playcanvas / engine

JavaScript game engine built on WebGL, WebGPU, WebXR and glTF
https://playcanvas.com
MIT License
9.55k stars 1.34k forks source link

For an array of JSON type script attributes, allow to set default values #4306

Closed yaustar closed 10 months ago

yaustar commented 2 years ago

Given:

Test.attributes.add('testObjects', {
    type: 'json',
    array: true,  
    schema: [{
        name: 'foo',
        type: 'entity'
    }, {
        name: 'bar',
        type: 'string',
    } ],
    default: [{foo: null, bar: ""}]
});

It would be great if we can autofill the default values of a script type with these types rather than just have an array of 0

kpal81xd commented 11 months ago

You set the default property for each element in the schema not on the surrounding object as you have done. For your example, you would set it as

Test.attributes.add('testObjects', {
    type: 'json',
    array: true,  
    schema: [{
        name: 'foo',
        type: 'entity',
        default: null,
    }, {
        name: 'bar',
        type: 'string',
        default: '',
    } ],
});
willeastcott commented 10 months ago

@kpal81xd Ah yeah, makes sense.

@yaustar I'm going to close this since it appears you specified the JSON attribute incorrectly. However, if you believe there's still a problem here, please let me know.

Maksims commented 10 months ago

I guess there is still no way to specify default value for a whole JSON array? I guess this applies for any array-like attribute.

yaustar commented 10 months ago

The idea (which wasn't communicated well in hindsight) is to be able to set a default array of values so when first added, it could look like this

image

kpal81xd's example sets the default values for each new added array element

kpal81xd commented 10 months ago

Ahh yes, I understand now. Doing some testing using the main branch and local engine flag in the editor indicates that it's working as outlined above.