How to use Modulator.exists?
-
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. -
@d-healey So you have a reference to something (lets say
modulator1
) and when you callmodulator1.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 tomodulator1
in the first place? -
@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 :)
-
@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?
-
@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()
. -
@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 fromclass ConstScriptingObject
(inScriptingBaseObjects.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 callingcheckValidObject()
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 ascheckValidObject()
. 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 callcheckValidObject()
making it basically useless to callexits()
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 }
- AudioSampleProcessor (
-
@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.
-
@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?