ToME: the Tales of Maj'Eyal

superloading settings.lua
Page 1 of 1

Author:  Charidan [ Fri Mar 08, 2019 12:56 am ]
Post subject:  superloading settings.lua

tl;dr: What is the correct way to add settings to the menu in an addon?

I'm trying to add a new tab to the Game Options menu (in retrospect, it should probably be a new entry in the main menu but I'll deal with that later), but there seems to be an issue preventing my settings from being initialized.

My superload of settings.lua is very simple:
config.settings.playerai = config.settings.playerai or {}

-- set defaults for the player ai
if type(config.settings.playerai.health_threshold) == 'nil' then config.settings.playerai.health_threshold_stop = 0.25 end
if type(config.settings.playerai.health_threshold) == 'nil' then config.settings.playerai.health_threshold_flee = 0.50 end

But I get the error that "config.settings.playerai" is a nil value.

My theory is that the line at the top of settings.lua:
if config.settings.tome_init_settings_ran then return end
config.settings.tome_init_settings_ran = true

Is being run when the main menu is loaded, and the addon is not loaded (addons being loaded upon New Game), and therefore my superload is never called.

Should I be hanging this init code off of a different file/class? What's the best way to ensure this code is properly run? Maybe call it from bindHook("ToME:run") or bindHook("ToME:load") perhaps?

EDIT: Are there other details to consider during this, such as explicit saving of the new settings? Should I instead be storing things in config.settings.tome.playerai so that things like save/load are handled more automatically?

Author:  HousePet [ Fri Mar 08, 2019 3:51 am ]
Post subject:  Re: superloading settings.lua

Not by Superloading. Bad!

Grab one of my addons, such as Arcanum or Verdant, pull it apart and look near the bottom of the load.lua file.

Author:  Charidan [ Fri Mar 08, 2019 7:50 pm ]
Post subject:  Re: superloading settings.lua

The hook onto GameOptions:generateList (or GameOptions:tabs in my case) works great for putting the menu items into the options menu, and I've had that working fine.

The question was how you get those settings into the config object, and how you ensure that these settings are saved.

It turns out I'm doing it basically the same way as you were (except I'm calling other files from load.lua instead of putting it all in directly because I expect it to get messy and large), with one key exception: In the game:saveSettings() function, I spelled the names of my variables wrong. Doh!

Thanks for giving me a working model frustratingly similar to what I had so I could finally make myself read closely enough.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group