Referencing asset(text file) directly

Hi everyone,

I’m trying to access the contents of a text file located in the assets, but I’m trying everything to reference/access it and it is not working… I’ve already tried:

 context.assets.getAssetById();

 var file = {
                          filename: "teste.txt",
                          url: "teste.txt",
                        }

 var asset = context.assets.find("teste", pc.asset.ASSET_TEXT);

When debugging, all these variables are ‘undefined’, what am I doing wrong?

Best Regards,

Arthur;

Hi Arthur,

Sorry, this part of the code is really non-obvious. We’re working on a better way to fix this, and I’ll update the docs asap to cover this use-case.

We load game assets upfront to prevent pop in and make sure everything is loaded when your code is run. But this means we need to know which assets to include. Currently we only include assets which are referenced in your pack. e.g. used in a model component or in an audiosource component.

So, how does this work for something like a text file, or a texture which you use at runtime? To make sure these assets are included they must be referenced in a Script Attribute. Basically, in your script add this as the first line:

pc.script.attribute('textAsset', 'asset', []);

then in the Designer, use the Entity menu: Entity -> Refresh Script Attributes. If you now select the entity with this script attached you’ll see a asset picker property below the script filename. Add your text file to the asset picker.

Then in your code you can do this:

var asset = context.assets.getAssetById(this.textAsset);
var contents = asset.resource;

Hope that helps.

2 Likes

Thank you very much!!! It looks like it works, but my playcanvas is crashing. I’m sorry for bothering you.

The error is:

WEBGL11004: INVALID_OPERATION: vertexAttribPointer
File: playcanvas-stable.js Line: 4001 Column: 7

WEBGL11094: INVALID_OPERATION: vertexAttribPointer: the enum UNSIGNED_BYTE is not supported.
File: playcanvas-stable.js Line:4001 Column: 4
That only started happening after I applied your changes, but this only happens in Internet Explorer, maybe since I use activeX to access the file’s content.
It seems like my function breaks something :c.

My function is:

  readFileContent: function(fileLocation) {
            debugger;
            if(this.isInternetExplorer()) {
                var FileOpener = new ActiveXObject('Scripting.FileSystemObject');
                var FilePointer = FileOpener.OpenTextFile(fileLocation, 1, true);
                FileContents = FilePointer.ReadAll(); // we can use FilePointer.ReadAll() to read all the lines
                FilePointer.Close();
                return FileContents;
            }
            else 
                return null;
        },

Hmm, I don’t know anything about ActiveX I’m afraid.

But I don’t think you need to use ActiveX anywhere. The engine handles the file loading, you will just get the contents of the file as a string.

Sorry, you are right. It was my mistake, ActiveX is not needed since ‘.resource’ ro read the file already returns it content. Thank you for the support.