How to save extension preferences -- question about write_default & read_default

Firstly, please learn how to properly post code in the forum …

Secondly, (and yes I realize you were just testing,) but the preferences JSON files are a shared filespace.

This means that everyone cannot be using plain descriptive names for their settings keys. Ie “HelloWorld” is too generic and could clash if someone else uses the same identifier.

Just as extensions need to have uniquely named registrar files and folders, they also need to use the same unique names for settings keys and attribute dictionary names. (All 3: files, dictionaries and settings are in shared spaces.)

So Tom, let us say that you choose to use “DrawFun” as your unique namespace identifier. (This is equivalent to an author or company name. Ie, a “fictitious name” if you will.)

So, your “HelloWorld” extension registrar file in the “Plugins” folder would be named "DrawFun_HelloWorld.rb". The extension subfolder must use the same name (but without the .rb file type.) Ie, "Plugins/DrawFun_HelloWorld" would be the extension subfolder.

Then you must use the same identifier ("DrawFun_HelloWorld") for this extension’s preferences key and any attribute dictionaries that your extension attaches to model objects.

All of the code in all of your extension’s files must be module wrapped using the same identifiers like so …

module DrawFun
  module HelloWorld

    # This extension's code ...

  end # extension submodule
end # top level namespace module

The fully qualified identifer to your extension is: DrawFun::HelloWorld.
(Basically the underscore is replaced with Ruby’s :: scope operator.)

Thirdly … lets get back to the preamble of your topic’ title … “How to save extension preferences” …

There are quite a few topics covering this more generally including what is the best way.

I feel that using “write_default” and “read_default” has not really been “the recommended wayby developers. Let us say that it is an old way that has long been available by the API.

But it has quirks… read_default evaluates the stored values in Ruby which can lead to weirdness.

Many developers have instead switched to using their own JSON files that are separate from SketchUp’s. I posted example of methods only 3 weeks ago …

1 Like