Skip to content

Commit b5582da

Browse files
committed
Added ability to get a list of project settings changed.
1 parent 6e4e807 commit b5582da

File tree

4 files changed

+319
-2
lines changed

4 files changed

+319
-2
lines changed

core/config/project_settings.cpp

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ String ProjectSettings::globalize_path(const String &p_path) const {
277277
bool ProjectSettings::_set(const StringName &p_name, const Variant &p_value) {
278278
_THREAD_SAFE_METHOD_
279279

280+
// Capture old value before making changes
281+
Variant old_value;
282+
if (props.has(p_name)) {
283+
old_value = props[p_name].variant;
284+
}
285+
280286
if (p_value.get_type() == Variant::NIL) {
281287
props.erase(p_name);
282288
if (p_name.operator String().begins_with("autoload/")) {
@@ -298,7 +304,10 @@ bool ProjectSettings::_set(const StringName &p_name, const Variant &p_value) {
298304
}
299305

300306
_version++;
301-
_queue_changed();
307+
308+
if (old_value != p_value) {
309+
_queue_changed(p_name);
310+
}
302311
return true;
303312
}
304313

@@ -344,7 +353,10 @@ bool ProjectSettings::_set(const StringName &p_name, const Variant &p_value) {
344353
}
345354

346355
_version++;
347-
_queue_changed();
356+
357+
if (old_value != p_value) {
358+
_queue_changed(p_name);
359+
}
348360
return true;
349361
}
350362

@@ -528,12 +540,32 @@ void ProjectSettings::_queue_changed() {
528540
callable_mp(this, &ProjectSettings::_emit_changed).call_deferred();
529541
}
530542

543+
void ProjectSettings::_queue_changed(const StringName &p_name) {
544+
if (!MessageQueue::get_singleton() || MessageQueue::get_singleton()->get_max_buffer_usage() == 0) {
545+
return;
546+
}
547+
548+
// Track this setting as changed
549+
changed_settings.insert(p_name);
550+
551+
// Only queue the deferred call once per frame
552+
if (!is_changed) {
553+
is_changed = true;
554+
callable_mp(this, &ProjectSettings::_emit_changed).call_deferred();
555+
}
556+
}
557+
531558
void ProjectSettings::_emit_changed() {
532559
if (!is_changed) {
533560
return;
534561
}
535562
is_changed = false;
563+
564+
// Emit the general settings_changed signal to indicate changes are complete.
536565
emit_signal("settings_changed");
566+
567+
// Clear the changed settings after emitting the signal
568+
clear_changed_settings();
537569
}
538570

539571
bool ProjectSettings::load_resource_pack(const String &p_pack, bool p_replace_files, int p_offset) {
@@ -1313,6 +1345,31 @@ Variant ProjectSettings::get_setting(const String &p_setting, const Variant &p_d
13131345
}
13141346
}
13151347

1348+
PackedStringArray ProjectSettings::get_changed_settings() const {
1349+
PackedStringArray arr;
1350+
for (const String &setting : changed_settings) {
1351+
arr.push_back(setting);
1352+
}
1353+
return arr;
1354+
}
1355+
1356+
bool ProjectSettings::check_changed_settings_in_group(const String &p_setting_prefix) const {
1357+
for (const String &setting : changed_settings) {
1358+
if (setting.begins_with(p_setting_prefix)) {
1359+
return true;
1360+
}
1361+
}
1362+
return false;
1363+
}
1364+
1365+
void ProjectSettings::mark_setting_changed(const String &p_setting) {
1366+
changed_settings.insert(p_setting);
1367+
}
1368+
1369+
void ProjectSettings::clear_changed_settings() {
1370+
changed_settings.clear();
1371+
}
1372+
13161373
void ProjectSettings::refresh_global_class_list() {
13171374
// This is called after mounting a new PCK file to pick up class changes.
13181375
is_global_class_list_loaded = false; // Make sure we read from the freshly mounted PCK.
@@ -1509,6 +1566,12 @@ void ProjectSettings::_bind_methods() {
15091566

15101567
ClassDB::bind_method(D_METHOD("save_custom", "file"), &ProjectSettings::_save_custom_bnd);
15111568

1569+
// Change tracking methods
1570+
ClassDB::bind_method(D_METHOD("get_changed_settings"), &ProjectSettings::get_changed_settings);
1571+
ClassDB::bind_method(D_METHOD("check_changed_settings_in_group", "setting_prefix"), &ProjectSettings::check_changed_settings_in_group);
1572+
ClassDB::bind_method(D_METHOD("mark_setting_changed", "setting"), &ProjectSettings::mark_setting_changed);
1573+
ClassDB::bind_method(D_METHOD("clear_changed_settings"), &ProjectSettings::clear_changed_settings);
1574+
15121575
ADD_SIGNAL(MethodInfo("settings_changed"));
15131576
}
15141577

core/config/project_settings.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class ProjectSettings : public Object {
4646
// and will always detect the initial project settings as a "change".
4747
uint32_t _version = 1;
4848

49+
// Track changed settings for get_changed_settings functionality
50+
HashSet<String> changed_settings;
51+
4952
public:
5053
typedef HashMap<String, Variant> CustomMap;
5154
static inline const String PROJECT_DATA_DIR_NAME_SUFFIX = "godot";
@@ -119,6 +122,7 @@ class ProjectSettings : public Object {
119122
bool _property_get_revert(const StringName &p_name, Variant &r_property) const;
120123

121124
void _queue_changed();
125+
void _queue_changed(const StringName &p_name);
122126
void _emit_changed();
123127

124128
static inline ProjectSettings *singleton = nullptr;
@@ -209,6 +213,12 @@ class ProjectSettings : public Object {
209213

210214
bool has_custom_feature(const String &p_feature) const;
211215

216+
// Change tracking methods
217+
PackedStringArray get_changed_settings() const;
218+
bool check_changed_settings_in_group(const String &p_setting_prefix) const;
219+
void mark_setting_changed(const String &p_setting);
220+
void clear_changed_settings();
221+
212222
const HashMap<StringName, AutoloadInfo> &get_autoload_list() const;
213223
void add_autoload(const AutoloadInfo &p_autoload);
214224
void remove_autoload(const StringName &p_autoload);

doc/classes/ProjectSettings.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,32 @@
5454
[b]Note:[/b] Setting [code]"usage"[/code] for the property is not supported. Use [method set_as_basic], [method set_restart_if_changed], and [method set_as_internal] to modify usage flags.
5555
</description>
5656
</method>
57+
<method name="check_changed_settings_in_group" qualifiers="const">
58+
<return type="bool" />
59+
<param index="0" name="setting_prefix" type="String" />
60+
<description>
61+
Checks if any settings with the prefix [param setting_prefix] exist in the set of changed settings. See also [method get_changed_settings].
62+
</description>
63+
</method>
5764
<method name="clear">
5865
<return type="void" />
5966
<param index="0" name="name" type="String" />
6067
<description>
6168
Clears the whole configuration (not recommended, may break things).
6269
</description>
6370
</method>
71+
<method name="clear_changed_settings">
72+
<return type="void" />
73+
<description>
74+
Clears the list of changed settings. Note that internally [code]changed_settings[/code] is cleared after a successful save, so generally the most appropriate place to use this method is when processing [signal settings_changed].
75+
</description>
76+
</method>
77+
<method name="get_changed_settings" qualifiers="const">
78+
<return type="PackedStringArray" />
79+
<description>
80+
Gets an array of the settings which have been changed since the last save. Note that internally [code]changed_settings[/code] is cleared after a successful save, so generally the most appropriate place to use this method is when processing [signal settings_changed].
81+
</description>
82+
</method>
6483
<method name="get_global_class_list">
6584
<return type="Dictionary[]" />
6685
<description>
@@ -172,6 +191,13 @@
172191
Returns the localized path (starting with [code]res://[/code]) corresponding to the absolute, native OS [param path]. See also [method globalize_path].
173192
</description>
174193
</method>
194+
<method name="mark_setting_changed">
195+
<return type="void" />
196+
<param index="0" name="setting" type="String" />
197+
<description>
198+
Marks the passed project setting as being changed, see [method get_changed_settings]. Only settings which exist (see [method has_setting]) will be accepted.
199+
</description>
200+
</method>
175201
<method name="save">
176202
<return type="int" enum="Error" />
177203
<description>

0 commit comments

Comments
 (0)