Skip to content

Commit 07b8725

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

File tree

4 files changed

+145
-2
lines changed

4 files changed

+145
-2
lines changed

core/config/project_settings.cpp

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,13 @@ 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+
// Early return if value hasn't changed (unless it's being deleted)
281+
if (p_value.get_type() != Variant::NIL) {
282+
if (props.has(p_name) && props[p_name].variant == p_value) {
283+
return true;
284+
}
285+
}
286+
280287
if (p_value.get_type() == Variant::NIL) {
281288
props.erase(p_name);
282289
if (p_name.operator String().begins_with("autoload/")) {
@@ -298,7 +305,7 @@ bool ProjectSettings::_set(const StringName &p_name, const Variant &p_value) {
298305
}
299306

300307
_version++;
301-
_queue_changed();
308+
_queue_changed(p_name);
302309
return true;
303310
}
304311

@@ -344,7 +351,7 @@ bool ProjectSettings::_set(const StringName &p_name, const Variant &p_value) {
344351
}
345352

346353
_version++;
347-
_queue_changed();
354+
_queue_changed(p_name);
348355
return true;
349356
}
350357

@@ -528,12 +535,31 @@ void ProjectSettings::_queue_changed() {
528535
callable_mp(this, &ProjectSettings::_emit_changed).call_deferred();
529536
}
530537

538+
void ProjectSettings::_queue_changed(const StringName &p_name) {
539+
changed_settings.insert(p_name);
540+
541+
if (!MessageQueue::get_singleton() || MessageQueue::get_singleton()->get_max_buffer_usage() == 0) {
542+
return;
543+
}
544+
545+
// Only queue the deferred call once per frame.
546+
if (!is_changed) {
547+
is_changed = true;
548+
callable_mp(this, &ProjectSettings::_emit_changed).call_deferred();
549+
}
550+
}
551+
531552
void ProjectSettings::_emit_changed() {
532553
if (!is_changed) {
533554
return;
534555
}
535556
is_changed = false;
557+
558+
// Emit the general settings_changed signal to indicate changes are complete.
536559
emit_signal("settings_changed");
560+
561+
// Clear the changed settings after emitting the signal
562+
changed_settings.clear();
537563
}
538564

539565
bool ProjectSettings::load_resource_pack(const String &p_pack, bool p_replace_files, int p_offset) {
@@ -1313,6 +1339,23 @@ Variant ProjectSettings::get_setting(const String &p_setting, const Variant &p_d
13131339
}
13141340
}
13151341

1342+
PackedStringArray ProjectSettings::get_changed_settings() const {
1343+
PackedStringArray arr;
1344+
for (const String &setting : changed_settings) {
1345+
arr.push_back(setting);
1346+
}
1347+
return arr;
1348+
}
1349+
1350+
bool ProjectSettings::check_changed_settings_in_group(const String &p_setting_prefix) const {
1351+
for (const String &setting : changed_settings) {
1352+
if (setting.begins_with(p_setting_prefix)) {
1353+
return true;
1354+
}
1355+
}
1356+
return false;
1357+
}
1358+
13161359
void ProjectSettings::refresh_global_class_list() {
13171360
// This is called after mounting a new PCK file to pick up class changes.
13181361
is_global_class_list_loaded = false; // Make sure we read from the freshly mounted PCK.
@@ -1509,6 +1552,9 @@ void ProjectSettings::_bind_methods() {
15091552

15101553
ClassDB::bind_method(D_METHOD("save_custom", "file"), &ProjectSettings::_save_custom_bnd);
15111554

1555+
// Change tracking methods
1556+
ClassDB::bind_method(D_METHOD("get_changed_settings"), &ProjectSettings::get_changed_settings);
1557+
ClassDB::bind_method(D_METHOD("check_changed_settings_in_group", "setting_prefix"), &ProjectSettings::check_changed_settings_in_group);
15121558
ADD_SIGNAL(MethodInfo("settings_changed"));
15131559
}
15141560

core/config/project_settings.h

Lines changed: 8 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,10 @@ 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+
212220
const HashMap<StringName, AutoloadInfo> &get_autoload_list() const;
213221
void add_autoload(const AutoloadInfo &p_autoload);
214222
void remove_autoload(const StringName &p_autoload);

doc/classes/ProjectSettings.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,26 @@
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="get_changed_settings" qualifiers="const">
72+
<return type="PackedStringArray" />
73+
<description>
74+
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].
75+
</description>
76+
</method>
6477
<method name="get_global_class_list">
6578
<return type="Dictionary[]" />
6679
<description>

tests/core/config/test_project_settings.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,80 @@ TEST_CASE("[ProjectSettings] localize_path") {
155155
TestProjectSettingsInternalsAccessor::resource_path() = old_resource_path;
156156
}
157157

158+
TEST_CASE("[SceneTree][ProjectSettings] settings_changed signal") {
159+
SIGNAL_WATCH(ProjectSettings::get_singleton(), SNAME("settings_changed"));
160+
161+
ProjectSettings::get_singleton()->set_setting("test_signal_setting", "test_value");
162+
MessageQueue::get_singleton()->flush();
163+
164+
SIGNAL_CHECK("settings_changed", { {} });
165+
166+
SIGNAL_UNWATCH(ProjectSettings::get_singleton(), SNAME("settings_changed"));
167+
}
168+
169+
TEST_CASE("[ProjectSettings] get_changed_settings basic functionality") {
170+
String setting_name = "test_changed_setting";
171+
ProjectSettings::get_singleton()->set_setting(setting_name, "test_value");
172+
173+
PackedStringArray changes = ProjectSettings::get_singleton()->get_changed_settings();
174+
CHECK(changes.has(setting_name));
175+
}
176+
177+
TEST_CASE("[ProjectSettings] get_changed_settings multiple settings") {
178+
ProjectSettings::get_singleton()->set_setting("test_setting_1", "value1");
179+
ProjectSettings::get_singleton()->set_setting("test_setting_2", "value2");
180+
ProjectSettings::get_singleton()->set_setting("another_group/setting", "value3");
181+
182+
PackedStringArray changes = ProjectSettings::get_singleton()->get_changed_settings();
183+
CHECK(changes.has("test_setting_1"));
184+
CHECK(changes.has("test_setting_2"));
185+
CHECK(changes.has("another_group/setting"));
186+
}
187+
188+
TEST_CASE("[ProjectSettings] check_changed_settings_in_group") {
189+
ProjectSettings::get_singleton()->set_setting("group1/setting1", "value1");
190+
ProjectSettings::get_singleton()->set_setting("group1/setting2", "value2");
191+
ProjectSettings::get_singleton()->set_setting("group2/setting1", "value3");
192+
ProjectSettings::get_singleton()->set_setting("other_setting", "value4");
193+
194+
CHECK(ProjectSettings::get_singleton()->check_changed_settings_in_group("group1/"));
195+
CHECK(ProjectSettings::get_singleton()->check_changed_settings_in_group("group2/"));
196+
CHECK_FALSE(ProjectSettings::get_singleton()->check_changed_settings_in_group("nonexistent/"));
197+
198+
CHECK(ProjectSettings::get_singleton()->check_changed_settings_in_group("group1"));
199+
CHECK(ProjectSettings::get_singleton()->check_changed_settings_in_group("other_setting"));
200+
}
201+
202+
TEST_CASE("[SceneTree][ProjectSettings] Changes cleared after settings_changed signal") {
203+
SIGNAL_WATCH(ProjectSettings::get_singleton(), SNAME("settings_changed"));
204+
205+
ProjectSettings::get_singleton()->set_setting("signal_clear_test", "value");
206+
207+
PackedStringArray changes_before = ProjectSettings::get_singleton()->get_changed_settings();
208+
CHECK(changes_before.has("signal_clear_test"));
209+
210+
MessageQueue::get_singleton()->flush();
211+
212+
SIGNAL_CHECK("settings_changed", { {} });
213+
214+
PackedStringArray changes_after = ProjectSettings::get_singleton()->get_changed_settings();
215+
CHECK_FALSE(changes_after.has("signal_clear_test"));
216+
217+
SIGNAL_UNWATCH(ProjectSettings::get_singleton(), SNAME("settings_changed"));
218+
}
219+
220+
TEST_CASE("[ProjectSettings] No tracking when setting same value") {
221+
String setting_name = "same_value_test";
222+
String test_value = "same_value";
223+
224+
ProjectSettings::get_singleton()->set_setting(setting_name, test_value);
225+
int count_before = ProjectSettings::get_singleton()->get_changed_settings().size();
226+
227+
// Setting the same value should not be tracked due to early return.
228+
ProjectSettings::get_singleton()->set_setting(setting_name, test_value);
229+
int count_after = ProjectSettings::get_singleton()->get_changed_settings().size();
230+
231+
CHECK_EQ(count_before, count_after);
232+
}
233+
158234
} // namespace TestProjectSettings

0 commit comments

Comments
 (0)