Page 1 of 1

Class import doesn't recreate shared vars when imported

Posted: Sep 20th, '17, 20:08
by SimpleSi
which of course, may be a design feature?

Obviously its better to not use global/shared vars in class intended to be imported into other projects but sometimes needs must

Once a shared var has been set - it then appears in the variable lists but it would be nicer if the import routine parsed the class and re-created any shared vars used

(At least it showed up a design fault in my MQTT class - I was never intialising one of my variables so it errored out on me when I tried to read it!

But great stuff on fixing the exporting bu in v75 :-)

Re: Class import doesn't recreate shared vars when imported

Posted: Sep 20th, '17, 23:30
by JohnM
Importing should probably ensure that any shared variables used by the scripts exist.

Will put this on the to-do list...

Re: Class import doesn't recreate shared vars when imported

Posted: Sep 20th, '17, 23:43
by JohnM
If you are creating a library of functions that are meant to be shared, it's better to use GP's extension mechanism, which was designed to do exactly that.

Here's a quick outline of the process:

1. Create your library. It can contain multiple class, shared variables, and shared blocks. They will all be part of the "extension" but will be isolated from from the project that uses the extension. That is, it's fine if user of the module has classes, shared variables, or shared functions with the same names as the ones in the module. The features becomes increasingly important as people create modules with lots of classes, variables, and functions and then created projects using multiple modules.

2. Export one more more shared blocks by right-clicking on the block in the palette and selecting "export to palette" from the menu. Only the blocks you export will be visible when the extension is imported.

3. Save the project as usual.

4. Create a new project that will use your extension. Right-click on the Stage and select "load extension...". Select your extension.

5. The blocks you exported to the palette in your extension will appear in the palette and can be used by the new project.

Extensions are the primary way of sharing libraries/modules/packages in GP. The class import/export mechanism is only needed when you need to combine code from different projects as if it had all been created in that project.

I'm guessing that your MQTT class should probably be packaged as an extension to make it easy for others to use it.

Re: Class import doesn't recreate shared vars when imported

Posted: Sep 21st, '17, 08:16
by SimpleSi
Wow :)

I'd seen reference to extensions but thought that was something for down the road

I'll be having a play now :)

Re: Class import doesn't recreate shared vars when imported

Posted: Sep 21st, '17, 09:48
by SimpleSi
If you are creating a library of functions that are meant to be shared, it's better to use GP's extension mechanism, which was designed to do exactly that.
:-) :-) :-)

ABSOLUTELY FANTASTIC FEATURE


:-)