Namespace != object
-
@Christoph-Hart yes - both these post pretty much sum up the problem:
var myTester = eval(Engine.loadFromJSON("test2.js"));
where test2.js =
{ "soundName": "S", "group": 17, "planSet": [ { "soundName": "B", "group": 2, "preWait": { "minVal": 4, "currentVal": 10, "maxVal": 25 }, "postWait": { "minVal": 1, "currentVal": 1, "maxVal": 1 }, "aCurve": 0.5, "aTime": 0, "aLevel": 0, "hTime": 100, "dCurve": 0.43, "dTime": 50, "sLevel": 100, "rTime": 0 } ] };
results in myTester =
{ "dump": Method, "clone": Method }
So we see LoadFromJSON will not load any kind of object......unless I'm doing something silly(quite possible)
and
"But isn't the only problem the fact that you put a function into the object? JSON is a data format so you can't put logic in it..."So yes JSON definitely ISNT working properly, and even if it was then it wouldnt do whats needed here. However there is a way to "unserialise" a string into an object WITH its functions, as well as a way to serialise an object WITH its functions
Just theres no way to save a string in HISE....
Hold on I WAS doing something silly...that eval is not required....stiull I'd like a way to serialise and unseralise WITH functions and we seem to be most of the way there... 'cept for save and load string...
-
I can't speak about
eval
because this is an easter egg that David found by accident :)But the normal JSON dump method works as it should:
var x = { "obj1": [12, 15, 16], "p2": ["alpha"] }; Engine.dumpAsJSON(x, "myTest"); var y = Engine.loadFromJSON("myTest");
Look at
y
in the object viewer (right click on the row in the scriptwatch table). You'll see that it perfectly clones the x object via the external file. -
@Christoph-Hart yeah _ I think I got there before you - me being silly - it works for objects as long as they are just data, so not really objects just structs really... so I guess I'm back to having some external function for every method I want on an object I would like to "save" - its messy. Back to looking at namespaces again...
-
But why is that messy? You most definitely don't want logic code in a user readable JSON dump? And if you rewrite the function
inline function saveTargetPlan(obj, target) { for (p in obj.planSet) // changed it to a range-based loop for you ... { if (p.soundName == target) return p; } }
and put it in a separate file with proper namespacing, I don't see how it can be more messy than the other solution...
-
@Christoph-Hart well you're going to have to excuse me I'm an old SmallTalk programmer - encapsulation is nearly everything...