Project Zomboid

Project Zomboid

B42 Native ModOptions Example
Notloc 28. dec. 2024 kl. 12:14
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)
Sidst redigeret af Notloc; 28. dec. 2024 kl. 16:20
< >
Viser 1-2 af 2 kommentarer
dhert  [udvikler] 28. dec. 2024 kl. 15:11 
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.
Sidst redigeret af dhert; 28. dec. 2024 kl. 15:25
Notloc 28. dec. 2024 kl. 16:13 
Ah good point, events are a better recommendation then directly decorating the load function.
< >
Viser 1-2 af 2 kommentarer
Per side: 1530 50