<-- back to the index of Rosetta examples

script state persistency (load/unload/reload)

Counts how many times the script was loaded, in a persistent on-disk counter.
Example implementations
awk | lua
Explanation, step by step
Create a function preunload that will be registered as an action. This function will be called automatically right before the script is unloaded. The return value of this function, automatically converted to string, will be saved on disk (by pcb-rnd).

On init, scriptpersistency("read") reads back the last saved string. If it returns -1, that means there was no saved data, reset the counter to 0.

When scriptpersistency("remove") is called, it removes the file from disk. This matters if pcb-rnd exits abruptly (e.g. crashes), not calling script unload to overwrite it or if multiple pcb-rnd processes would be running in parallel and only one should load the data into the script. Keeping it on disk means the script would still load the last known-to-be-good data after a pcb-rnd crash.

Note #1: the script can save and reload multiple items, as data is an opaque string to pcb-rnd. However, serialization (packing and unpacking of the items into a single string) needs to be done by the script.

Note #2: persistency data is stored by script ID. Script ID is assigned run-time by the user. That means the ID can be reused by a different script next time and persistency data saved by one script may need to be passed to another. This simplified example does not deal with that. A real script should either do sanity checks or even better store a signature in the data.

Note #3: however, this also means compatible scripts can share data written in a common format. The example scripts in this directory can be all loaded under the same ID and will continue the counting. In a case like this, a safer approach is to store a format-specific signature.

Note #4: ReloadScript() really does an unload and a load, but the reason passed to preunload() will be "reload". The script may decide to save persistency differently knowing that the reload will be immediate.