]> Untitled Git - wolf-seeking-sheep.git/blob - addons/dialogic/Modules/Settings/subsystem_settings.gd
Updated export config options
[wolf-seeking-sheep.git] / addons / dialogic / Modules / Settings / subsystem_settings.gd
1 extends DialogicSubsystem
2 ## Subsystem that allows setting and getting settings that are automatically saved slot independent.
3 ##
4 ## All settings that are stored in the project settings dialogic/settings section are supported.
5 ## For example the text_speed setting is stored there.
6 ## How to access this subsystem via code:
7 ##    ```gd
8 ##        Dialogic.Settings.text_speed = 0.05
9 ##    ```
10 ##
11 ## Settings stored there can also be changed with the Settings event.
12
13
14 var settings := {}
15 var _connections := {}
16
17
18 #region MAIN METHODS
19 ####################################################################################################
20
21 ## Built-in, called by DialogicGameHandler.
22 func clear_game_state(_clear_flag:=DialogicGameHandler.ClearFlags.FULL_CLEAR):
23         _reload_settings()
24
25
26 func _reload_settings() -> void:
27         settings = {}
28         for prop in ProjectSettings.get_property_list():
29                 if prop.name.begins_with('dialogic/settings'):
30                         settings[prop.name.trim_prefix('dialogic/settings/')] = ProjectSettings.get_setting(prop.name)
31
32         if dialogic.has_subsystem('Save'):
33                 for i in settings:
34                         settings[i] = dialogic.Save.get_global_info(i, settings[i])
35
36
37 func _set(property:StringName, value:Variant) -> bool:
38         if not settings.has(property) or settings[property] != value:
39                 _setting_changed(property, value)
40         settings[property] = value
41         if dialogic.has_subsystem('Save'):
42                 dialogic.Save.set_global_info(property, value)
43         return true
44
45
46 func _get(property:StringName) -> Variant:
47         if property in settings:
48                 return settings[property]
49         return null
50
51
52 func _setting_changed(property:StringName, value:Variant) -> void:
53         if !property in _connections:
54                 return
55
56         for i in _connections[property]:
57                 if not is_instance_valid(i.get_object()):
58                         var remove := func(): _connections[property].erase(i)
59                         remove.call_deferred()
60                         continue
61                 i.call(value)
62
63 #endregion
64
65
66 #region HANDY METHODS
67 ####################################################################################################
68
69 ## Get a setting named `property`, if it does not exist, falls back to `default`.
70 func get_setting(property: StringName, default: Variant) -> Variant:
71         return _get(property) if _get(property) != null else default
72
73 ## Whether a setting has been set/stored before.
74 func has_setting(property: StringName) -> bool:
75         return property in settings
76
77
78 func reset_all() -> void:
79         for setting in settings:
80                 reset_setting(setting)
81
82
83 func reset_setting(property: StringName) -> void:
84         if ProjectSettings.has_setting('dialogic/settings/'+property):
85                 settings[property] = ProjectSettings.get_setting('dialogic/settings/'+property)
86                 _setting_changed(property, settings[property])
87         else:
88                 settings.erase(property)
89                 _setting_changed(property, null)
90
91
92 ## If a setting named `property` changes its value, this will emit `Callable`.
93 func connect_to_change(property: StringName, callable: Callable) -> void:
94         if not property in _connections:
95                 _connections[property] = []
96         _connections[property].append(callable)
97
98 #endregion