Script claims global variable defined in another script is "Not defined"

This works:

// NumberInfo.js
var Fraction = {
    JsonSchema : [{
        name: 'numerator',
        type: 'number',
        default: 1
    }, {
        name: 'denominator',
        type: 'number',
        default: 1
    }]
    
};

var NumberInfo = pc.createScript('numberInfo');
NumberInfo.attributes.add('fraction', {
    type: 'json',
    schema: Fraction.JsonSchema
});

This does NOT work:

// Fraction.js
var Fraction = {
    JsonSchema : [{
        name: 'numerator',
        type: 'number',
        default: 1
    }, {
        name: 'denominator',
        type: 'number',
        default: 1
    }]
    
};

// NumberInfo.js
var NumberInfo = pc.createScript('numberInfo');
NumberInfo.attributes.add('fraction', {
    type: 'json',
    schema: Fraction.JsonSchema
});

Fraction is ahead of NumberInfo in “Script Execution Order”, and Fraction is declared as a global variable, but NumberInfo cannot access this variable during attributes definition unless Fraction is defined above it in the SAME script.

Is there any way around this?

thanks!

Hi @Charlie_Van_Norman ,

For the script editor parser to work all attributes/script types definitions should be in the same file. Right now splitting them like that won’t be accepted by the parser.

Thanks, that’s what I was afraid of.

I do feel like this is something that we should change :thinking:

Can you make a feature request on the Editor repo please? GitHub - playcanvas/editor: Issue tracker for the PlayCanvas Editor

1 Like

Added feature request: Parse attributes to be able to use JSON schemas from other files · Issue #444 · playcanvas/editor · GitHub

1 Like