diff --git a/public/scripts/variables.js b/public/scripts/variables.js
index 2db475abf..768c747a5 100644
--- a/public/scripts/variables.js
+++ b/public/scripts/variables.js
@@ -2,12 +2,20 @@ import { chat_metadata, getCurrentChatId, saveSettingsDebounced, sendSystemMessa
import { extension_settings, saveMetadataDebounced } from './extensions.js';
import { executeSlashCommands, registerSlashCommand } from './slash-commands.js';
-function getLocalVariable(name) {
+function getLocalVariable(name, args={}) {
if (!chat_metadata.variables) {
chat_metadata.variables = {};
}
- const localVariable = chat_metadata?.variables[name];
+ let localVariable = chat_metadata?.variables[name];
+ if (args.index !== undefined) {
+ try {
+ localVariable = JSON.parse(localVariable);
+ localVariable = localVariable[Number(args.index)];
+ } catch {
+ // that didn't work
+ }
+ }
return (localVariable === '' || isNaN(Number(localVariable))) ? (localVariable || '') : Number(localVariable);
}
@@ -22,8 +30,16 @@ function setLocalVariable(name, value) {
return value;
}
-function getGlobalVariable(name) {
- const globalVariable = extension_settings.variables.global[name];
+function getGlobalVariable(name, args={}) {
+ let globalVariable = extension_settings.variables.global[name];
+ if (args.index !== undefined) {
+ try {
+ globalVariable = JSON.parse(globalVariable);
+ globalVariable = globalVariable[Number(args.index)];
+ } catch {
+ // that didn't work
+ }
+ }
return (globalVariable === '' || isNaN(Number(globalVariable))) ? (globalVariable || '') : Number(globalVariable);
}
@@ -536,10 +552,10 @@ function sqrtValuesCallback(value) {
export function registerVariableCommands() {
registerSlashCommand('listvar', listVariablesCallback, [], ' – list registered chat variables', true, true);
registerSlashCommand('setvar', (args, value) => setLocalVariable(args.key || args.name, value), [], 'key=varname (value) – set a local variable value and pass it down the pipe, e.g. /setvar key=color green', true, true);
- registerSlashCommand('getvar', (_, value) => getLocalVariable(value), [], '(key) – get a local variable value and pass it down the pipe, e.g. /getvar height', true, true);
+ registerSlashCommand('getvar', (args, value) => getLocalVariable(value, args), [], 'index=listIndex (key) – get a local variable value and pass it down the pipe, index is optional, e.g. /getvar height or /getvar index=3 costumes', true, true);
registerSlashCommand('addvar', (args, value) => addLocalVariable(args.key || args.name, value), [], 'key=varname (increment) – add a value to a local variable and pass the result down the pipe, e.g. /addvar score 10', true, true);
registerSlashCommand('setglobalvar', (args, value) => setGlobalVariable(args.key || args.name, value), [], 'key=varname (value) – set a global variable value and pass it down the pipe, e.g. /setglobalvar key=color green', true, true);
- registerSlashCommand('getglobalvar', (_, value) => getGlobalVariable(value), [], '(key) – get a global variable value and pass it down the pipe, e.g. /getglobalvar height', true, true);
+ registerSlashCommand('getglobalvar', (args, value) => getGlobalVariable(value, args), [], 'index=listIndex (key) – get a global variable value and pass it down the pipe, index is optional, e.g. /getglobalvar height or /getglobalvar index=3 costumes', true, true);
registerSlashCommand('addglobalvar', (args, value) => addGlobalVariable(args.key || args.name, value), [], 'key=varname (increment) – add a value to a global variable and pass the result down the pipe, e.g. /addglobalvar score 10', true, true);
registerSlashCommand('incvar', (_, value) => incrementLocalVariable(value), [], '(key) – increment a local variable by 1 and pass the result down the pipe, e.g. /incvar score', true, true);
registerSlashCommand('decvar', (_, value) => decrementLocalVariable(value), [], '(key) – decrement a local variable by 1 and pass the result down the pipe, e.g. /decvar score', true, true);