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?
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;
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;
},
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.