Skip to content

Commit 4f43506

Browse files
Fix arrays with resources not being updated correctly in game when edited in the inspector
1 parent 9a5d6d1 commit 4f43506

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

editor/debugger/script_editor_debugger.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,42 @@ void ScriptEditorDebugger::_property_changed(Object *p_base, const StringName &p
14571457
NodePath path = EditorNode::get_singleton()->get_edited_scene()->get_path_to(node);
14581458
int pathid = _get_node_path_cache(path);
14591459

1460+
if (p_value.is_array()) {
1461+
Array array = p_value;
1462+
bool has_resources = false;
1463+
1464+
for (int i = 0; i < array.size(); i++) {
1465+
Ref<Resource> item_res = array[i];
1466+
if (item_res.is_valid() && !item_res->get_path().is_empty()) {
1467+
has_resources = true;
1468+
}
1469+
}
1470+
1471+
if (has_resources) {
1472+
Array res_array;
1473+
Array res_indexes_array;
1474+
res_array.resize(array.size());
1475+
for (int i = 0; i < array.size(); i++) {
1476+
Ref<Resource> item_res = array[i];
1477+
if (item_res.is_valid() && !item_res->get_path().is_empty()) {
1478+
res_array[i] = item_res->get_path();
1479+
res_indexes_array.push_back(i);
1480+
} else {
1481+
res_array[i] = array[i];
1482+
}
1483+
}
1484+
1485+
String script_path;
1486+
Ref<Script> array_script = array.get_typed_script();
1487+
if (array_script.is_valid()) {
1488+
script_path = array_script->get_path();
1489+
}
1490+
1491+
Array msg = { pathid, p_property, res_array, array.get_typed_builtin(), array.get_typed_class_name(), script_path, res_indexes_array };
1492+
_put_msg("scene:live_node_prop_res_array", msg);
1493+
return;
1494+
}
1495+
}
14601496
if (p_value.is_ref_counted()) {
14611497
Ref<Resource> res = p_value;
14621498
if (res.is_valid() && !res->get_path().is_empty()) {
@@ -1477,6 +1513,42 @@ void ScriptEditorDebugger::_property_changed(Object *p_base, const StringName &p
14771513
String respath = res->get_path();
14781514
int pathid = _get_res_path_cache(respath);
14791515

1516+
if (p_value.is_array()) {
1517+
Array array = p_value;
1518+
bool has_resources = false;
1519+
1520+
for (int i = 0; i < array.size(); i++) {
1521+
Ref<Resource> item_res = array[i];
1522+
if (item_res.is_valid() && !item_res->get_path().is_empty()) {
1523+
has_resources = true;
1524+
}
1525+
}
1526+
1527+
if (has_resources) {
1528+
Array res_array;
1529+
Array res_indexes_array;
1530+
res_array.resize(array.size());
1531+
for (int i = 0; i < array.size(); i++) {
1532+
Ref<Resource> item_res = array[i];
1533+
if (item_res.is_valid() && !item_res->get_path().is_empty()) {
1534+
res_array[i] = item_res->get_path();
1535+
res_indexes_array.push_back(i);
1536+
} else {
1537+
res_array[i] = array[i];
1538+
}
1539+
}
1540+
1541+
String script_path;
1542+
Ref<Script> array_script = array.get_typed_script();
1543+
if (array_script.is_valid()) {
1544+
script_path = array_script->get_path();
1545+
}
1546+
1547+
Array msg = { pathid, p_property, res_array, array.get_typed_builtin(), array.get_typed_class_name(), script_path, res_indexes_array };
1548+
_put_msg("scene:live_res_prop_res_array", msg);
1549+
return;
1550+
}
1551+
}
14801552
if (p_value.is_ref_counted()) {
14811553
Ref<Resource> res2 = p_value;
14821554
if (res2.is_valid() && !res2->get_path().is_empty()) {

scene/debugger/scene_debugger.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ Error SceneDebugger::_msg_live_node_prop_res(const Array &p_args) {
292292
return OK;
293293
}
294294

295+
Error SceneDebugger::_msg_live_node_prop_res_array(const Array &p_args) {
296+
ERR_FAIL_COND_V(p_args.size() < 7, ERR_INVALID_DATA);
297+
LiveEditor::get_singleton()->_node_set_res_array_func(p_args[0], p_args[1], p_args[2], p_args[3], p_args[4], p_args[5], p_args[6]);
298+
return OK;
299+
}
300+
295301
Error SceneDebugger::_msg_live_node_prop(const Array &p_args) {
296302
ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
297303
LiveEditor::get_singleton()->_node_set_func(p_args[0], p_args[1], p_args[2]);
@@ -304,6 +310,12 @@ Error SceneDebugger::_msg_live_res_prop_res(const Array &p_args) {
304310
return OK;
305311
}
306312

313+
Error SceneDebugger::_msg_live_res_prop_res_array(const Array &p_args) {
314+
ERR_FAIL_COND_V(p_args.size() < 7, ERR_INVALID_DATA);
315+
LiveEditor::get_singleton()->_res_set_res_array_func(p_args[0], p_args[1], p_args[2], p_args[3], p_args[4], p_args[5], p_args[6]);
316+
return OK;
317+
}
318+
307319
Error SceneDebugger::_msg_live_res_prop(const Array &p_args) {
308320
ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
309321
LiveEditor::get_singleton()->_res_set_func(p_args[0], p_args[1], p_args[2]);
@@ -549,8 +561,10 @@ void SceneDebugger::_init_message_handlers() {
549561
message_handlers["live_node_path"] = _msg_live_node_path;
550562
message_handlers["live_res_path"] = _msg_live_res_path;
551563
message_handlers["live_node_prop_res"] = _msg_live_node_prop_res;
564+
message_handlers["live_node_prop_res_array"] = _msg_live_node_prop_res_array;
552565
message_handlers["live_node_prop"] = _msg_live_node_prop;
553566
message_handlers["live_res_prop_res"] = _msg_live_res_prop_res;
567+
message_handlers["live_res_prop_res_array"] = _msg_live_res_prop_res_array;
554568
message_handlers["live_res_prop"] = _msg_live_res_prop;
555569
message_handlers["live_node_call"] = _msg_live_node_call;
556570
message_handlers["live_res_call"] = _msg_live_res_call;
@@ -1098,6 +1112,32 @@ void LiveEditor::_node_set_res_func(int p_id, const StringName &p_prop, const St
10981112
_node_set_func(p_id, p_prop, r);
10991113
}
11001114

1115+
void LiveEditor::_node_set_res_array_func(int p_id, const StringName &p_prop, const Variant &p_value, const uint32_t &p_typed_builtin, const StringName &p_typed_class_name, const String &p_typed_script_path, const Array &p_res_indexes_array) {
1116+
Array array = p_value;
1117+
1118+
Array res_array;
1119+
Variant script;
1120+
if (!p_typed_script_path.is_empty()) {
1121+
script = ResourceLoader::load(p_typed_script_path);
1122+
}
1123+
1124+
res_array.set_typed(p_typed_builtin, p_typed_class_name, script);
1125+
res_array.resize(array.size());
1126+
1127+
for (int i = 0; i < array.size(); i++) {
1128+
if (array[i].get_type() != Variant::NIL && p_res_indexes_array.has(i)) {
1129+
Ref<Resource> res = ResourceLoader::load(array[i]);
1130+
if (res.is_valid()) {
1131+
res_array[i] = res;
1132+
}
1133+
} else {
1134+
res_array[i] = array[i];
1135+
}
1136+
}
1137+
1138+
_node_set_func(p_id, p_prop, res_array);
1139+
}
1140+
11011141
void LiveEditor::_node_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount) {
11021142
SceneTree *scene_tree = SceneTree::get_singleton();
11031143
if (!scene_tree) {
@@ -1189,6 +1229,32 @@ void LiveEditor::_res_set_res_func(int p_id, const StringName &p_prop, const Str
11891229
_res_set_func(p_id, p_prop, r);
11901230
}
11911231

1232+
void LiveEditor::_res_set_res_array_func(int p_id, const StringName &p_prop, const Variant &p_value, const uint32_t &p_typed_builtin, const StringName &p_typed_class_name, const String &p_typed_script_path, const Array &p_res_indexes_array) {
1233+
Array array = p_value;
1234+
1235+
Array res_array;
1236+
Variant script;
1237+
if (!p_typed_script_path.is_empty()) {
1238+
script = ResourceLoader::load(p_typed_script_path);
1239+
}
1240+
1241+
res_array.set_typed(p_typed_builtin, p_typed_class_name, script);
1242+
res_array.resize(array.size());
1243+
1244+
for (int i = 0; i < array.size(); i++) {
1245+
if (array[i].get_type() != Variant::NIL && p_res_indexes_array.has(i)) {
1246+
Ref<Resource> res = ResourceLoader::load(array[i]);
1247+
if (res.is_valid()) {
1248+
res_array[i] = res;
1249+
}
1250+
} else {
1251+
res_array[i] = array[i];
1252+
}
1253+
}
1254+
1255+
_res_set_func(p_id, p_prop, res_array);
1256+
}
1257+
11921258
void LiveEditor::_res_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount) {
11931259
if (!live_edit_resource_cache.has(p_id)) {
11941260
return;

scene/debugger/scene_debugger.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,10 @@ class SceneDebugger {
9999
static Error _msg_live_node_path(const Array &p_args);
100100
static Error _msg_live_res_path(const Array &p_args);
101101
static Error _msg_live_node_prop_res(const Array &p_args);
102+
static Error _msg_live_node_prop_res_array(const Array &p_args);
102103
static Error _msg_live_node_prop(const Array &p_args);
103104
static Error _msg_live_res_prop_res(const Array &p_args);
105+
static Error _msg_live_res_prop_res_array(const Array &p_args);
104106
static Error _msg_live_res_prop(const Array &p_args);
105107
static Error _msg_live_node_call(const Array &p_args);
106108
static Error _msg_live_res_call(const Array &p_args);
@@ -208,9 +210,11 @@ class LiveEditor {
208210

209211
void _node_set_func(int p_id, const StringName &p_prop, const Variant &p_value);
210212
void _node_set_res_func(int p_id, const StringName &p_prop, const String &p_value);
213+
void _node_set_res_array_func(int p_id, const StringName &p_prop, const Variant &p_value, const uint32_t &p_typed_builtin, const StringName &p_typed_class_name, const String &p_typed_script_path, const Array &p_res_indexes_array);
211214
void _node_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount);
212215
void _res_set_func(int p_id, const StringName &p_prop, const Variant &p_value);
213216
void _res_set_res_func(int p_id, const StringName &p_prop, const String &p_value);
217+
void _res_set_res_array_func(int p_id, const StringName &p_prop, const Variant &p_value, const uint32_t &p_typed_builtin, const StringName &p_typed_class_name, const String &p_typed_script_path, const Array &p_res_indexes_array);
214218
void _res_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount);
215219
void _root_func(const NodePath &p_scene_path, const String &p_scene_from);
216220

0 commit comments

Comments
 (0)