PDA

View Full Version : How to listen for option changes (5E)?



jkeller
November 16th, 2025, 16:05
In the 5E ruleset, how do I get my extension to listen for option change events?

I've registered my simple on/off option using OptionsManager.registerOption2.
I see it, and I can check the value using OptionsManager.getOption.

I tried calling OptionsManager.registerCallback(LABEL, myOnOptionChanged),
where LABEL is the key used in the call to registerOption2
but my function never gets called,

I'm not using OptionsManager.registerOptionData, should I?

Thanks!

Trenloe
November 16th, 2025, 20:59
OptionsManager.registerCallback should work. There's plenty of examples in the 5E ruleset. What scope does the code have - i.e. where are you calling OptionsManager.registerCallback?

jkeller
November 17th, 2025, 02:02
I'm not familiar enough with LUA to answer the scope question. I have about 20 LUA files in this extension. The code is in my "main" file (not sure if that's even a thing, but it's my file with the onInit function).

onInit calls registerOptions, which is where OptionsManager.registerOption2 and OptionsManager.registerCallback are called. The callback function is in that file too. None are tagged as local.

I noticed some of the other code uses the "self" prefix for the callback function:

OptionsManager.registerCallback("HREN", self.onHRENOptionChanged);

And some use the name tag:

OptionsManager.registerCallback("HRAS", CharAttunementManager.onOptionChanged);

I tried all 3 ways, and none seems to work.

I must be doing something wrong, but I'm not sure what heh. Thanks for the help!

Trenloe
November 17th, 2025, 02:15
Post your code so we can take a look.

jkeller
November 17th, 2025, 14:58
I pulled out just the relevant code (which still has the same behavior).

The option is available, and the value is shown in onInit. But changing it doesn't seem to trigger the callback.




LABEL = "Extension Test";

function onInit()
if Session.IsHost then
registerOptions();
print("Extension Test Option: " .. tostring(OptionsManager.getOption(LABEL)));
end
end

function registerOptions()
OptionsManager.registerOption2(LABEL,
false, -- locked
"option_header_houserule",
"option_label_TEST",
"option_entry_cycler",
{
labels = "option_val_off|option_val_on",
values = "off|on",
baselabel = "option_val_off",
baseval = "off",
default = "on"
});
OptionsManager.registerCallback(LABEL, onOptionChanged);
end

function onOptionChanged(sOptionName, sNewValue)
print(LABEL .. " option updated: " .. tostring(sOptionName) .. " => " .. tostring(sNewValue));
end

Trenloe
November 17th, 2025, 16:18
Thanks for including the code. Don't have a space in the option key - keys in LUA need to be handled differently when they have spaces and the FG code isn't coded for that. I'd recommend referring to existing FG code as examples, and trying to keep to a similar syntax that's used in the FG code.

jkeller
November 17th, 2025, 16:37
That was the problem, thanks Trenloe!