diff --git a/core/variant/variant_parser.cpp b/core/variant/variant_parser.cpp index f5f96456d32..7ec58dd3144 100644 --- a/core/variant/variant_parser.cpp +++ b/core/variant/variant_parser.cpp @@ -2155,6 +2155,11 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str List props; obj->get_property_list(&props); + thread_local HashSet used_objects; + if (p_recursion_count == 0) { + used_objects.clear(); + } + bool first = true; for (const PropertyInfo &E : props) { if (E.usage & PROPERTY_USAGE_STORAGE || E.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) { @@ -2165,9 +2170,20 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str } else { p_store_string_func(p_store_string_ud, ","); } - + Variant v = obj->get(E.name); + if (v.get_type() == Variant::OBJECT) { + if (v.get_validated_object() == obj) { + p_store_string_func(p_store_string_ud, "\"" + String("self")); + continue; + } + if (used_objects.has(obj)) { + p_store_string_func(p_store_string_ud, "\"" + obj->get_class() + "\": Recursive object"); + continue; + } + used_objects.insert(obj); + } p_store_string_func(p_store_string_ud, "\"" + E.name + "\":"); - write(obj->get(E.name), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, p_recursion_count, p_compat); + write(v, p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, p_recursion_count, p_compat); } }