Expansion - Access AdditionalSourcecode file
-
@d-healey said in Expansion - Access AdditionalSourcecode file:
Don't do this:
if (cx.loadDataFile("NewNumber.json") == undefined)
It's inefficient because you'll have to read from disk more than once. Instead load it once and store it in a local variable, or if you are going to be using the data in other parts of your script you could use a reg variable in your on init callback or at the top of your namespace.
Thanks :)
But if the file doesn't exist then I get the error which prevents the rest of the script from running. That's the wall that I'm hitting. Unless I misunderstand?
To put this into context I've supplied an Expansion which doesn't have this file inside and I'd rather not update all the users if there's a way to get around it!
I'm using this elsewhere which works fine (no errors) - was hoping the same would apply in my expansion callback:
if(Engine.loadFromJSON("..//AnaData.js") == undefined)
-
@DanH as I said:
var myObj = Engine.loadFromJSON("somefile"); if (myObj) { Console.print(myObj.NumberData); }else{ myObj = {NumberData : 444}; }
-
@Lindon Thanks, but that will look in the appData folder for the file, whereas I'm in an active expansion.... Or am I getting confused?
-
@DanH You're not confused, but you could just take Lindon's code and adjust it to load from an expansion (and don't use
var
) :)local e = expHandler.getCurrentExpansion(); local data = e.loadDAtaFile("NewNumber.json"); if (isDefined(data)) { // Do the thing } else { // Do the other thing }
-
@DanH my takeaway from this is that Engine.loadFromJSON can handle a file not being there but currentexpansion.loadDataFile cannot, and produces an error...
-
@d-healey yup, but I can't get past .loadDataFile if there's no data file
HISE doesn't seem to like it (unlike Engine.loadFromJSON)
-
@DanH Which line gives the error and what is the error?
-
@d-healey
Error:Interface:! Cant find data file {EXP::TEST}NewNumber.json
Line:
local data = e.loadDAtaFile("NewNumber.json");
-
@DanH I see the problem. I've got to go out now but I'll take a look in the HISE source when I get back in a few hours and see if there is an easy fix.
-
@d-healey ok thanks!
-
This is how
Engine.loadFromJSON
handles a missing file.And this is how
loadDataFile
handles it:So it reports an error but does it actually stop the rest of your script running? In other words, if you put
Console.print("HELLO WORLD");
on the next line does anything print to the console? -
@d-healey yeah it stops it - i.e no "Hello World"
-
@DanH Okay, I think we should make it so it behaves the same as
loadFromJSON
and leave it up to the scripting to validate the file. I'll make a pull request with this later. -
@DanH Yeah removing those lines makes it work like the other function which I think is much better behaviour. I've made a pull request and the change is also in my fork.
-
@d-healey great, so just remove the reportScriptError line?
-
@DanH Just do what I did - https://github.com/christophhart/HISE/pull/286/commits/3ebdf557839a53c2d94c123e72391b62919aa725 (the red stuff is the parts I deleted)