HISE Logo Forum
    • Categories
    • Register
    • Login

    Namespace != object

    Scheduled Pinned Locked Moved General Questions
    25 Posts 3 Posters 993 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • LindonL
      Lindon @d.healey
      last edited by

      @d-healey said in Namespace != object:

      HISE Script != OOP

      How to fake it - https://forum.hise.audio/topic/177/javascript-version/5

      Well your code from there:

      namespace myClass
      {
          reg _privateVariable; //Use underscore for variables that should be private
          reg publicVariable;
      
          inline function createInstanceOfClass(id, someParameter)
          {
              local obj = {};
              
              obj.id = id;
              obj.someParameter = someParameter;
              return obj;
          }
      
          inline function changeParameter(obj, parameter, value) //Pass in the instance of the object
          {
              obj[parameter] = value;
          }
      }
      

      Is pretty similar to where I got to but Im not using name spaces-- but its still creating objects {}

      But now I need to serailise these objects and thats worrying me - as well as the no garbage collection.

      So the assertion :
      HISE Script != OOP

      will hold for the general rules of OO, but when you can do this:

      var myObj = {};

      then you are on the path to OO at least...

      HISE Development for hire.
      www.channelrobot.com

      1 Reply Last reply Reply Quote 0
      • d.healeyD
        d.healey
        last edited by

        Can the task not be accomplished procedurally instead?

        Libre Wave - Freedom respecting instruments and effects
        My Patreon - HISE tutorials
        YouTube Channel - Public HISE tutorials

        LindonL 1 Reply Last reply Reply Quote 0
        • LindonL
          Lindon @d.healey
          last edited by

          @d-healey said in Namespace != object:

          Can the task not be accomplished procedurally instead?

          "Procedurally"? -- it will be very very very very very ugly...heres what a small sample of the table looks like (it actually has 10 values for each cell - here you are seeing only 3):

          38221087-a23a-4551-aee5-cf9728dd373b-image.png

          -- no ones idea of fun -and filled with potential fail.

          HISE Development for hire.
          www.channelrobot.com

          1 Reply Last reply Reply Quote 0
          • d.healeyD
            d.healey
            last edited by

            What's it for?

            Libre Wave - Freedom respecting instruments and effects
            My Patreon - HISE tutorials
            YouTube Channel - Public HISE tutorials

            LindonL 1 Reply Last reply Reply Quote 0
            • LindonL
              Lindon @d.healey
              last edited by Lindon

              @d-healey -- not really in a position to say publicly...but generally its about cross fading sounds together to get a required outcome..

              HISE Development for hire.
              www.channelrobot.com

              d.healeyD 1 Reply Last reply Reply Quote 0
              • d.healeyD
                d.healey @Lindon
                last edited by

                @Lindon Sounds fun! Do you need to use namespaces if you have it working with objects?

                Libre Wave - Freedom respecting instruments and effects
                My Patreon - HISE tutorials
                YouTube Channel - Public HISE tutorials

                LindonL 1 Reply Last reply Reply Quote 0
                • LindonL
                  Lindon @d.healey
                  last edited by

                  @d-healey said in Namespace != object:

                  @Lindon Sounds fun! Do you need to use namespaces if you have it working with objects?

                  No I dont , but I'm a bit worried about performance, garbage collection (tho its all a std tree structure for the objects in here), and serialisation.

                  HISE Development for hire.
                  www.channelrobot.com

                  LindonL 1 Reply Last reply Reply Quote 0
                  • LindonL
                    Lindon @Lindon
                    last edited by Lindon

                    -- yep and as I suspect:

                    Engine.dumpAsJSON(myObj1, "my.json");
                    ..
                    ..
                    ..
                    var myObj2 = Engine.loadFromJSON("my.json");
                    

                    leaves myObj2 = undefined

                    even tho my.json looks like this:

                    {
                      "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
                        }
                      ],
                      "targetPlan": function (target){
                              for (idx= 0;idx<this.planSet.length;idx++)
                              {
                                  if (this.planSet[idx].soundName == target)
                                        return this.planSet[idx];
                              };
                            }
                        
                    }
                    

                    HISE Development for hire.
                    www.channelrobot.com

                    LindonL 1 Reply Last reply Reply Quote 0
                    • LindonL
                      Lindon @Lindon
                      last edited by

                      @Lindon - yep loadFromJSON wont load object definitions...so serialisation will be a problem...

                      HISE Development for hire.
                      www.channelrobot.com

                      d.healeyD 1 Reply Last reply Reply Quote 0
                      • d.healeyD
                        d.healey @Lindon
                        last edited by d.healey

                        @Lindon Does it work if you save the object definition as a string and use the exec command?

                        Libre Wave - Freedom respecting instruments and effects
                        My Patreon - HISE tutorials
                        YouTube Channel - Public HISE tutorials

                        LindonL 1 Reply Last reply Reply Quote 0
                        • LindonL
                          Lindon @d.healey
                          last edited by Lindon

                          @d-healey dunno - I will try --- tomorrow...
                          err.. looking up exec gives me this:

                          Link Preview Image
                          W3Schools.com

                          Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.

                          favicon

                          (www.w3schools.com)

                          --which is seraching in strings

                          HISE Development for hire.
                          www.channelrobot.com

                          d.healeyD 1 Reply Last reply Reply Quote 0
                          • d.healeyD
                            d.healey @Lindon
                            last edited by

                            @Lindon I have a video about the exec command waiting to be edited, basically it executes a string as if it was a line of code, but it's a bit finicky and I haven't tried it with functions.

                            So you could do something like this.

                            var myCommand = "Console.print('HELLO WORLD');";
                            exec(myCommand);
                            

                            Libre Wave - Freedom respecting instruments and effects
                            My Patreon - HISE tutorials
                            YouTube Channel - Public HISE tutorials

                            LindonL 1 Reply Last reply Reply Quote 0
                            • Christoph HartC
                              Christoph Hart
                              last edited by

                              @Lindon said in Namespace != object:

                              garbage collection

                              Don't worry about garbage collection. This only gets a problem if you have cyclic references, which are pretty easy to avoid:

                              var x = {};
                              var y = {};
                              
                              x.child = y;
                              y.child = x; // <= Baam, leak
                              

                              A tree normally doesn't have references from the child back to the parent, so unless you specifically want to shoot yourself in the foot, it doesn't matter.

                              BTW, there's a cyclic reference checker built into HISE, that would detect the cycle in the (trivial) example above and spit out an error.

                              8d0c7162-fb77-4242-8d0c-b8e0154a6208-image.png

                              1 Reply Last reply Reply Quote 0
                              • Christoph HartC
                                Christoph Hart
                                last edited by

                                Oh and to your original question:

                                Back in the C days when there was no OOP built into the language, people were using POD structs (= plain old data) along with functions that take a reference to that object and manipulate it. This is pretty similar to what I would suggest trying out in HiseScript.

                                So instead of a "real" OOP approach where you create objects that contain methods that operate on themselves you'll write "global" functions (or tuck them into a namespace for increased code clarity), then pass any object to it as parameter:

                                namespace MyObjectFunctions
                                {
                                    inline function initObject(obj)
                                    {
                                        obj.myVariable = 2;
                                    }
                                
                                    inline function printVariable(obj)
                                    {
                                        Console.print(obj.myVariable);
                                    }
                                }
                                
                                const var objList = [];
                                
                                for(i = 0; i < 1200; i++)
                                {
                                    var obj = {};
                                    MyObjectFunctions.initObject(obj);
                                    objList.push(obj);
                                }
                                
                                for(o in objList)
                                    MyObjectFunctions.printVariable(o);
                                

                                This is equivalent to the "correct" OOP way (I'm using C++ here, but you'll get the point)

                                class MyFunctionObject
                                {
                                    void init()
                                    {
                                        myVariable = 2;
                                    }
                                
                                    void printVariable()
                                    {
                                        Console.print(myVariable);
                                    }
                                
                                    int myVariable;
                                };
                                
                                MyFunctionObject objList[1200];
                                
                                for(int i = 0; i < 1200; i++)
                                    objList[i].init();
                                
                                for(auto& o: objList)
                                    o.printVariable();
                                

                                As you can see, there is not much overhead compared to Javascript, but this separation between data and logic keeps things tidy - you can serialize the data using JSON etc.

                                LindonL 1 Reply Last reply Reply Quote 0
                                • LindonL
                                  Lindon @Christoph Hart
                                  last edited by

                                  @Christoph-Hart yeah this is sort of what I feared - now I have a lot of additional code to write to serialise and unseralise these arrays....when (as you say) I'm using simple pretty strict tree structures for my object collection.... so no garbage collection leaks -- it just seems a shame the JSON dump and load commands wont dump AND load objects - pretty ironic given the JSON acronym...

                                  HISE Development for hire.
                                  www.channelrobot.com

                                  1 Reply Last reply Reply Quote 0
                                  • LindonL
                                    Lindon @d.healey
                                    last edited by Lindon

                                    @d-healey said in Namespace != object:

                                    @Lindon I have a video about the exec command waiting to be edited, basically it executes a string as if it was a line of code, but it's a bit finicky and I haven't tried it with functions.

                                    So you could do something like this.

                                    var myCommand = "Console.print('HELLO WORLD');";
                                    exec(myCommand);
                                    

                                    Sadly:

                                    myTester = exec(Engine.loadFromJSON("test2.js"));
                                    
                                    var myCommand = "var mTestyNew = {'SoundName':'B','group':2};";
                                    exec(myCommand);
                                    

                                    where test2.js contains:

                                    var myTestFile2 = {
                                      "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
                                        }
                                      ],
                                      "targetPlan": function (target){
                                              for (idx= 0;idx<this.planSet.length;idx++)
                                              {
                                                  if (this.planSet[idx].soundName == target)
                                                        return this.planSet[idx];
                                              };
                                            }
                                        
                                    };
                                    

                                    Results in an object called mTestyNew (so that works) and myTester = undefined,so the LoadFromJSON fails...

                                    Even if it worked I have no idea how I would save an object in a way that would "exec"

                                    It looks like what Im asking for is

                                    JSON.stringify() <---which seems to be present, so I can turn an object into a string

                                    and

                                    JSON.parse() <-- which is missing so I cant turn it back into an object
                                    however this works:

                                    var TestOutString = JSON.stringify(myObject);
                                    var TestNewObj = eval(TestOutString);
                                    

                                    So all I need now is a way to save this string.....

                                    HISE Development for hire.
                                    www.channelrobot.com

                                    1 Reply Last reply Reply Quote 0
                                    • Christoph HartC
                                      Christoph Hart
                                      last edited by

                                      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...

                                      1 Reply Last reply Reply Quote 0
                                      • Christoph HartC
                                        Christoph Hart
                                        last edited by

                                        Oh, and this:

                                        var myTestFile2 = {
                                          "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
                                            }
                                          ],
                                          "targetPlan": function (target){
                                                  for (idx= 0;idx<this.planSet.length;idx++)
                                                  {
                                                      if (this.planSet[idx].soundName == target)
                                                            return this.planSet[idx];
                                                  };
                                                }
                                            
                                        };
                                        

                                        is Javascript, not JSON. You need to remove the var myTestFile2 = at the beginning so it's just the object declaration...

                                        LindonL 1 Reply Last reply Reply Quote 0
                                        • LindonL
                                          Lindon @Christoph Hart
                                          last edited by Lindon

                                          @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...

                                          HISE Development for hire.
                                          www.channelrobot.com

                                          1 Reply Last reply Reply Quote 0
                                          • Christoph HartC
                                            Christoph Hart
                                            last edited by

                                            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.

                                            LindonL 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post

                                            46

                                            Online

                                            1.7k

                                            Users

                                            11.7k

                                            Topics

                                            101.9k

                                            Posts