Project Zomboid

Project Zomboid

B42 Native ModOptions Example
Notloc 28 Dec, 2024 @ 12:14pm
Apply event example
Because the format in the comments section was hard to read.

Apply Event
Triggered whenever the user applies settings in the mod option menu.
local modOptions = PZAPI.ModOptions:create(modId, modUiName) modOptions.apply = function (self) -- Your code. end

The only issue with this (depending on your use case) is that it does not trigger during the initial load.
But you can effectively get that functionality using events, as per dhert's feedback:

Events.OnMainMenuEnter.Add(function() modOptions:apply() end)
Last edited by Notloc; 28 Dec, 2024 @ 4:20pm
< >
Showing 1-2 of 2 comments
dhert  [developer] 28 Dec, 2024 @ 3:11pm 
Nice find! I saw the empty "apply()" function for the ModOptions, and just figured it was something that wasn't implemented yet. I didn't notice that it was already being called by the MainOptions, so it makes sense that it would just be a blank function there. I'll update the example to include this information too here in a bit.

As for initially loading, I think it's best practice to just grab any settings you need during the "OnMainMenuEnter" Event. The options are available during that event, and it's better to hook into an event than have every other mod decorate the `load` function with its own thing. I'll include an example for this step as well.

Some example code:
local myOption = 0 options.apply = function() myOption = options:getOption("2"):getValue() print("Applied Option: " .. myOption) end Events.OnMainMenuEnter.Add(function() options.apply() print("Saved Option: " .. myOption) end)
You can actually just manually call the `apply()` function if you set the event during the initial phase, and thus have all your options cached locally.
Last edited by dhert; 28 Dec, 2024 @ 3:25pm
Notloc 28 Dec, 2024 @ 4:13pm 
Ah good point, events are a better recommendation then directly decorating the load function.
< >
Showing 1-2 of 2 comments
Per page: 1530 50