HISE Logo Forum
    • Categories
    • Register
    • Login

    How to use Modulator.exists?

    Scheduled Pinned Locked Moved Scripting
    8 Posts 2 Posters 124 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.
    • d.healeyD
      d.healey
      last edited by

      I want to check if a modulator exists in my project (so I can reuse the same code in other projects that might not have that modulator), but it seems the exists function can only be called on a reference to a modulator, but trying to get a reference to a non-existent modulator results in an error.

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

      hujackusH 1 Reply Last reply Reply Quote 0
      • hujackusH
        hujackus @d.healey
        last edited by

        @d-healey So you have a reference to something (lets say modulator1) and when you call modulator1.exists(); it throws an error?

        Does modulator1.getGlobalModulatorId(); throw an error in the same way? That could be a workaround.

        Does checking modulator1==undefined ahead of time make sense in your situation? How do you get the reference to modulator1 in the first place?

        Check out the music of Conway's Game of Life - https://www.youtube.com/@hujackus
        ConwayMatrix - http://hujackus.altervista.org/conwaymatrix/

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

          @hujackus said in How to use Modulator.exists?:

          So you have a reference to something (lets say modulator1) and when you call modulator1.exists(); it throws an error?

          Nope, I don't have a reference because there is no modulator :)

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

          hujackusH 1 Reply Last reply Reply Quote 0
          • hujackusH
            hujackus @d.healey
            last edited by

            @d-healey Good, I was thinking you had a reference somehow and calling exists() wasn't working properly.

            So what we are looking for is a simple way to traverse the Synth tree, grab all the modulators and check to see if one of those is the one you have a script for?

            Check out the music of Conway's Game of Life - https://www.youtube.com/@hujackus
            ConwayMatrix - http://hujackus.altervista.org/conwaymatrix/

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

              @hujackus said in How to use Modulator.exists?:

              So what we are looking for is a simple way to traverse the Synth tree, grab all the modulators and check to see if one of those is the one you have a script for?

              Nope, I can do that already (I solved it shortly after the initial post). What I want is to know how to use Modulator.exists().

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

              hujackusH 1 Reply Last reply Reply Quote 0
              • hujackusH
                hujackus @d.healey
                last edited by

                @d-healey said in How to use Modulator.exists?:

                What I want is to know how to use Modulator.exists().

                So far I've been looking into this file: hi_scripting/scripting/api/ScriptingApiObjects.h

                • AudioSampleProcessor (class ScriptingAudioSampleProcessor)
                • ChildSynth (class ScriptingSynth)
                • Effect (class ScriptingEffect)
                • MidiProcessor (class ScriptingMidiProcessor)
                • Modulator (class ScriptingModulator)
                • TableProcessor (class ScriptingTableProcessor)
                • SlotFX (class ScriptingSlotFX)

                All the above script objects have the exists() function because they inherit it from class ConstScriptingObject (in ScriptingBaseObjects.h) In addition to shared functions, each of these classes have an internal reference ( WeakReference<Processor>) to the actual processor. Because this reference can be null or be deleted at anytime, the script object constantly has to verify that the reference is still there by calling checkValidObject() every time a method is called. (This function never returns false*, it only returns true or throws an error.)

                bool ConstScriptingObject::checkValidObject() const
                {
                	if (!objectExists())
                	{
                		reportScriptError(getObjectName().toString() + " " + getInstanceName() + " does not exist.");
                		RETURN_IF_NO_THROW(false)
                	}
                
                	if (objectDeleted())
                	{
                		reportScriptError(getObjectName().toString() + " " + getInstanceName() + " was deleted");
                		RETURN_IF_NO_THROW(false)
                	}
                
                	return true;
                }
                

                Basically exists() is defined to do exactly the same thing as checkValidObject(). It is literally defined that way.

                /** Checks if the Object exists and prints a error message on the console if not. */
                bool exists() { return checkValidObject(); };
                

                This leads me to believe that Modulator.exists() should only be used in a fail-fast check before calling Modulator API functions. However, all of the standard functions already call checkValidObject() making it basically useless to call exits() at all. Nonetheless, an example of how to use the function is...

                const modulator = Synth.getModulator("myDodgyModulator");
                // I suspect modulator's state is out of sync because reasons
                if(modulator.exists()){
                    //call a few functions on modulator that might not already call checkValidObject
                }
                

                Check out the music of Conway's Game of Life - https://www.youtube.com/@hujackus
                ConwayMatrix - http://hujackus.altervista.org/conwaymatrix/

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

                  @hujackus said in How to use Modulator.exists?:

                  Nonetheless, an example of how to use the function is...

                  Did you try it? I get an error.

                  The workaround is to use getAllModulators and then check if it returns anything - not quite the same thing but worked in my current project. But I'd still like to solve this mystery.

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

                  hujackusH 1 Reply Last reply Reply Quote 0
                  • hujackusH
                    hujackus @d.healey
                    last edited by

                    @d-healey said in How to use Modulator.exists?:

                    Did you try it? I get an error.

                    That's very interesting. I spent so much time in the C++ i failed to consider if it even compiles in HISE. I assumed it did, but it doesn't in the onInit() callback. It will compile if you call exists() in any of the other callbacks, but will throw an error at runtime.

                    Perhaps exists() is not supposed to be visible in the API and was accidentally added into the documentation?

                    Check out the music of Conway's Game of Life - https://www.youtube.com/@hujackus
                    ConwayMatrix - http://hujackus.altervista.org/conwaymatrix/

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

                    20

                    Online

                    1.7k

                    Users

                    11.8k

                    Topics

                    102.6k

                    Posts