Assets array with script 2.0

Hello, not sure if i have coded it right since never used 2.0 scripts before, i have made this line

Gui.attributes.add('iconGui', { type: 'asset', assetType: 'texture', array: true });

and loaded 4 images in editor as iconGui
then i have this line
this.icbag=this.iconGui[0]…and also tried to add .resource…but don’t seems right
in previous scripts i had this

this.icbag=app.assets.get(this.iconGui[0]).getFileUrl();

What do i do wrong?

If you are trying to get the file URL, taking this example:

var Test = pc.createScript('test');

Test.attributes.add('foo', {type: 'asset', assetType: 'texture', title: 'Foo', array: true, description:''});

this.foo is an array of assets, this.foo[0] will return an pc.Asset object so all you have to do is call getFileUrl()

console.log(this.foo[0].getFileUrl());

Hi @steven if this should work it doesn’t with raphael.min.js maybe is raphael fault?
Here is the project https://playcanvas.com/editor/scene/492980
gui.js is scripts object

Your app is crashing on this line:

this.invIcoPaper = Raphael(10,85, this.icoSheetWidth, this.icoSheetHeight);

I see that, but can’t understand why, the same script works fine in Kallen Supremacy, so i guess Raphael has some issue with new scripts

Yes it’s totally a raphael issue with new scripts, i will restart the project switching back to old scripts since seems there is no documentation about raphael with playcanvas.

I’ve forked and fixed the project: https://playcanvas.com/project/458598/overview/next-millennium-from-forums

As you have just started using Scripts 2.0, you’ve left some code outside of the initialization function in GUI.js

var Gui = pc.createScript('gui');
Gui.attributes.add('iconGui', { type: 'asset',assetType: 'texture', title: 'iconGui', array: true });
//this.entity = entity;
this.defaultOpacity = 0.6;
this.icoSheetWidth = 60;
this.icoSheetHeight = 60;
this.isOpen = false;

// initialize code called once per entity
Gui.prototype.initialize = function() {
    //this.player=app.root.findByName("Player").script.player;
    this.icbag=this.iconGui[0].getFileUrl();
    this.icchar=this.iconGui[1].getFileUrl();
    this.icque=this.iconGui[2];
    this.createIcons();
};

Specifically icoSheetWidth and icoSheetHeight and therefore they are not part of the script instance so when you do this in createIcons:

this.invIcoPaper = Raphael(10,85, this.icoSheetWidth, this.icoSheetHeight);

this.icoSheetWidth and this.icoSheetHeight are undefined.

So to fix it, you just move that code into the initialize function where I believe you intended to be:

var Gui = pc.createScript('gui');
Gui.attributes.add('iconGui', { type: 'asset',assetType: 'texture', title: 'iconGui', array: true });

// initialize code called once per entity
Gui.prototype.initialize = function() {
    //this.player=app.root.findByName("Player").script.player;
    this.icbag=this.iconGui[0].getFileUrl();
    this.icchar=this.iconGui[1].getFileUrl();
    this.icque=this.iconGui[2].getFileUrl();
    
    this.defaultOpacity = 0.6;
    this.icoSheetWidth = 60;
    this.icoSheetHeight = 60;
    this.isOpen = false;
    
    this.createIcons();
};

I stress that you should use the debugger in the browser more to help figure what is going on when you hit problems like this. Using callstack and breakpoints helped me to identify that you were passing in undefined variables to the Raphael function in GUI.js.

Uhm i see…in the previous version of script it worked fine putting it before the initialize function, so no i need to put that inside the initialize function…i see…since debugger didn’t show me the problem but just pointed out raphael script i thought it was a problem of raphael…now i see is just my lacking of knowledge. Thanks a lot @steven

Was the previous version of the script in the old script system?

The crash was in Raphael, but if you use the un-minified version of script, you can check over the code and have a quick look at the data that is being used at that time.

So on the line before it crashes in Raphael, you can see two arguments being undefined:

Next step is to go back through the callstack to where you are calling the code:

Then you can see that the params being passed are undefined:

Oh yes i see…didn’t thought of that…as i told i didn’t think to move the this,icoSheetWidth definition inside initialize…so didn’t come in my mind that the problem could be this…newbie mistake with the new scripts.[quote=“steven, post:9, topic:3343”]
Was the previous version of the script in the old script system?
[/quote]

Yes i meant that

So with the old scripting system:

pc.script.create('Level', function (app) {
    // Creates a new Level instance
    var Level = function (entity) {
        this.entity = entity;
        
        this.map = {};
        this.freeCells = [];
        //this.depth = 0;
        this.walls = {};
        this.visible = {};
        this.traps = {};
    };

    Level.prototype = {
        // Called once after all resources are loaded and before the first update
        initialize: function () {
            this.game = app.root.findByName('Root').script.Game;
            this.player=app.root.findByName('Player').script.player;
            this.output= app.root.findByName('Root').script.Output;
            //this.calc=app.root.findByName('calc').script.maths;

            this.createNewLevel();
        },

The variables such as this.map, this.walls etc, they are within scope of the script instance of Level whereas the code you had in the new scripting system was in global scope and not the script instance.

In a nutshell, put all your script instance variables in the initialize function for the new scripting system.

Ok thanks :slight_smile: Also if i don’t plan to convert Kallen in the new script system for now…i will convert the sequel probably when will go multiplayer.