[edit: found a better solution below: set a flag in process()]
I’ve figured out a trick to determine when the module dataFromJson is being called from the context menu when loading a preset. The trick is to make a custom menu item (a label or separator, for example) that resets a flag in its destructor.
Then in the module’s dataFromJson check the fromContextMenu flag to know that the call came from the context menu.
Unfortunately this doesn’t help in the Save preset scenario because rack calls dataToJson every 15 seconds in a global settings change, even while the context menu is open. If it’s just doing the periodic save, you’ll be fooled into behaving like a preset was being saved. Depending on what you’re doing special this may not matter because the next periodic save will be a normal one, or it may cause your module to misbehave, so caution is in order.
If your goal is to run a function when a preset is loaded, you could set a bool variable in void paramsFromJson(json_t *rootJ) override {} and and then just check it in process? (This is what I do for my cowbell module to auto reset it when a preset is loaded to prevent some feedback issues)
As far as I can tell from skimming Rack source, paramsFromJson is called in the preset loading case also, so it doesn’t serve to distinguish loading a module preset from patch load/module init cases,
or, are you saying that any calls to the fromJson overrides after the first are always preset loads?
erm no, what I am saying is that paramsFromJson is called for preset loads, dataFromJson is for auto save. At least for my cowbell module, paramsFromJson works to run code only on preset loads, i havent seen any unwanted behaviour from using it…
Both are called for all deserialization when the corresponding params or data object is in the json: opening a patch, restoring from autosave, duplicating a module, loading a preset.
There are modules with no data, and modules with no params. In these cases the corresponding override isn’t called.
I’m happen to be working through a case where the module has no params (unusual, I know).
But the principle is the same, whichever one you’re overriding.
I tried setting a flag on the first deserialize but this fails for first preset load after the module is newly added.
So, I’ve rethought it, and have a much better solution that seems to be working:
Any dataFromJson/paramsFromJson that occurs after the module is running is from a preset load. So it’s just a simple matter of setting a flag in process(). I only run the special code when the flag is set. ez-pz