Calling project specific code from an Ignition module

Hello!

I am trying to make an Ignition module for MQTT where the projects are in charge of subscribing to the topics they need and set a callback on message reception. Im looking for a way to call those callbacks from the module.

I think im in the right direction using this line but I might be wrong (It seems its still attempting to use my global scripts but I want to use any project script) :
gatewayContext.getProjectManager().getProjectScriptManager("ProjectNameHere").runCode();

Has anyone attempted to do this? What worked for you?

Thanks!

It is extremely dangerous to allow callers to supply a callback function that is going to be retained for more than one single, short-term, use. Any project edit/save will restart gateway scripts and make the function obsolete. Your module holding the function object will block garbage collection and you will leak gobs of memory.

Instead, have the user supply a function name, and look that name up in a script manager's locals(). (Split on dots and use .findattr() on following parts.) You can cache that lookup in between project restarts (using a project listener to signal the need to discard). Use the script manager's .runFunction() method to execute function objects.

Thanks for the quick reply!

The concerns for project save were indeed something I had in mind.

Script managers only have a .createLocalsMap() function available. Is that what you are refering to? So i have to lookup the name of the function I want to use in that map, retrieve the PyFunction object and call it with runFunction()?

Yes.