superloading settings.lua

Moderator: Moderator

Post Reply
Message
Author
Charidan
Higher
Posts: 64
Joined: Wed Jul 22, 2015 8:12 pm

superloading settings.lua

#1 Post by Charidan »

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:

Code: Select all

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:

Code: Select all

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?
Currently developing the Player AI addon. You can get it from the T-Engine Addon Hub or Steam
You can also view the source code.

HousePet
Perspiring Physicist
Posts: 6215
Joined: Sun Sep 09, 2012 7:43 am

Re: superloading settings.lua

#2 Post by HousePet »

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.
My feedback meter decays into coding. Give me feedback and I make mods.

Charidan
Higher
Posts: 64
Joined: Wed Jul 22, 2015 8:12 pm

Re: superloading settings.lua

#3 Post by Charidan »

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.
Currently developing the Player AI addon. You can get it from the T-Engine Addon Hub or Steam
You can also view the source code.

Post Reply