Advertisement Create a free account

API one-click useroptions

1461520923
Stephen L.
Pro
Marketplace Creator
Sheet Author
API Scripter
If useroptions are specified for a script in its script.json file, how do you access the values of these from within the API? I've searched the wiki for documentation about this, but couldn't find any. The _Example script specifies useroptions in its script.json file, but it doesn't demonstrate how to access or use them.
1461521653
The Aaron
Forum Champion
API Scripter
There is a new global object named globalconfig. It has properties for each script which contain an object with each of the useroptions. 
1461521826

Edited 1461521853
Stephen L.
Pro
Marketplace Creator
Sheet Author
API Scripter
ok. I just tried printing that with log() and got a mapping of script names (lowercase and with whitespace removed) to objects with 'version' and 'lastsaved' properties. I'm guessing that any useroptions key-value pairs will also be in here?
1461528712
The Aaron
Forum Champion
API Scripter
That is correct.  I'll try to write up a wiki page about it at some point.  The name of the script property is something arbitrarily assigned by Steve, but generally is the lowercase, spaceless version of the script name.
1461529454
Stephen L.
Pro
Marketplace Creator
Sheet Author
API Scripter
Cool, thanks!
1461598697

Edited 1461598932
Brian
Sheet Author
API Scripter
Here's an example in use: var bshields = bshields || {}; bshields.Collision = (function() { 'use strict'; var version = 2.2, behaviors = { DONT_MOVE: 1, WARN_PLAYER: 2, STOP_AT_WALL: 4 }, config = {}, configDefaults = { pathColor: '#ff00ff', layer: 'walls', behavior: behaviors.STOP_AT_WALL | behaviors.WARN_PLAYER }; Object.defineProperties(config, { pathColor: { get: function() { var stPathColor = state.bshields.Collision.config.pathColor; if (/* stPathColor has an invalid value */) return configDefaults.pathColor; /* ensure stPathColor is in correct format */ return stPathColor; } }, layer: { get: function() { var stLayer = state.bshields.Collision.config.layer; if (/* stLayer has an invalid value */) return configDefaults.layer; return stLayer; } }, behavior: { get: function() { var stBehavior = state.bshields.Collision.config.behavior; if (!stBehavior) return configDefaults.behavior; /* map string value stBehavior to integer return value */ } } }); // script contents // config.pathColor, config.layer, and config.behavior are referenced function checkInstall() { if (!state.bshields || !state.bshields.Collision || !state.bshields.Collision.version || state.bshields.Collision.version !== version) { state.bshields = state.bshields || {}; state.bshields.Collision = { version: version, gcUpdated: 0, config: {} }; } checkGlobalConfig(); } function checkGlobalConfig() { var gc = globalconfig && globalconfig.collisiondetection, st = state.bshields.Collision; if (gc && gc.lastsaved && gc.lastsaved > st.gcUpdated) { st.gcUpdated = gc.lastsaved; st.config.pathColor = gc['Path Color']; st.config.layer = gc.Layer; st.config.behavior = gc.Behavior; } } return { checkInstall: checkInstall, registerEventHandlers: registerEventHandlers }; }()); on('ready', function() { 'use strict'; bshields.Collision.checkInstall(); bshields.Collision.registerEventHandlers(); }); "useroptions": [ { "name": "Path Color", "type": "text", "default": "#ff00ff", "description": ... }, { "name": "Layer", "type": "select", "options": ["all", "walls", "gmlayer", "objects", "map"], "default": "walls", "description": ... }, { "name": "Behavior", "type": "select", "options": ["Don't Move", "Warn Player", "Stop at Wall", "Don't Move & Warn Player", "Warn Player & Stop at Wall"], "default": "Warn Player & Stop at Wall", "description": ... }], The above script doesn't do it, but the gc.lastsaved check also lets you modify values in-play without overwriting them whenever you reload, while still overwriting them if you change the user options.
1461606336
Stephen L.
Pro
Marketplace Creator
Sheet Author
API Scripter
Thanks, Brian! That's a good example.