Skip to content

Commit b78a4cd

Browse files
authored
Merge pull request #59 from Justin99x/master
Load default values for save options
2 parents bd225e0 + 98ef2b0 commit b78a4cd

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
### [Mods Base v1.10](https://github.com/bl-sdk/mods_base/blob/master/Readme.md#v19)
1212
> - Linting fixups.
1313
14+
### Save Options v1.2
15+
- Fixed issue where loading a character with no save option data inherited option values from
16+
previous character.
17+
1418
### UI Utils v1.3
1519
- Linting fixes.
1620

src/save_options/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"register_save_options",
2626
)
2727

28-
__version_info__: tuple[int, int] = (1, 1)
28+
__version_info__: tuple[int, int] = (1, 2)
2929
__version__: str = f"{__version_info__[0]}.{__version_info__[1]}"
3030
__author__: str = "bl-sdk"
3131

src/save_options/hooks.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import save_options.options
1010
from mods_base import JSON, get_pc, hook
11-
from save_options.options import trigger_save
11+
from save_options.options import set_option_to_default, trigger_save
1212
from save_options.registration import (
1313
ModSaveOptions,
1414
load_callbacks,
@@ -126,6 +126,13 @@ def end_load_game(_1: UObject, _2: WrappedStruct, ret: Any, _4: BoundFunction) -
126126
# loading character in main menu also. No callback here because the timing of when this is
127127
# called doesn't make much sense to do anything with it. See hook on LoadPlayerSaveGame.
128128

129+
# Often we'll load a save from a character with no save data. We'll set all save options
130+
# to default first to cover for any missing data.
131+
132+
for mod_save_options in registered_save_options.values():
133+
for save_option in mod_save_options.values():
134+
set_option_to_default(save_option)
135+
129136
# This function returns the new save game object, so use a post hook and grab it from `ret`
130137
save_game = ret
131138
if not save_game:

src/save_options/options.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass
2-
from typing import Any
2+
from typing import Any, cast
33

44
from unrealsdk import make_struct
55
from unrealsdk.hooks import Type, prevent_hooking_direct_calls
@@ -15,11 +15,28 @@
1515
get_pc,
1616
hook,
1717
)
18-
from mods_base.options import DropdownOption, KeybindOption
18+
from mods_base.options import BaseOption, DropdownOption, GroupedOption, KeybindOption, NestedOption
1919

2020
any_option_changed: bool = False
2121

2222

23+
def set_option_to_default(save_option: BaseOption) -> None:
24+
"""
25+
Sets an option's value to its default.
26+
27+
For GroupedOption and NestedOption, recursively sets all children to their default values.
28+
"""
29+
match save_option:
30+
case ValueOption():
31+
val_opt = cast(ValueOption[Any], save_option)
32+
val_opt.value = val_opt.default_value
33+
case GroupedOption() | NestedOption():
34+
for child in save_option.children:
35+
set_option_to_default(child)
36+
case _:
37+
pass
38+
39+
2340
def can_save() -> bool:
2441
"""Checks if a character is loaded that we can save to."""
2542
pc = get_pc()

0 commit comments

Comments
 (0)