Browse Source

Merge pull request #26304 from bojidar-bg/26066-fix-exported-poolarray

Fix GDScript exports having the wrong type of default value by converting it
Rémi Verschelde 6 years ago
parent
commit
28a57c2c29
2 changed files with 22 additions and 2 deletions
  1. 12 2
      editor/editor_properties_array_dict.cpp
  2. 10 0
      modules/gdscript/gdscript_parser.cpp

+ 12 - 2
editor/editor_properties_array_dict.cpp

@@ -418,13 +418,14 @@ void EditorPropertyArray::_length_changed(double p_page) {
 		return;
 
 	Variant array = object->get_array();
+	int previous_size = array.call("size");
+
 	array.call("resize", int(p_page));
-	emit_changed(get_edited_property(), array, "", false);
 
 	if (array.get_type() == Variant::ARRAY) {
 		if (subtype != Variant::NIL) {
 			int size = array.call("size");
-			for (int i = 0; i < size; i++) {
+			for (int i = previous_size; i < size; i++) {
 				if (array.get(i).get_type() == Variant::NIL) {
 					Variant::CallError ce;
 					array.set(i, Variant::construct(subtype, NULL, 0, ce));
@@ -432,7 +433,16 @@ void EditorPropertyArray::_length_changed(double p_page) {
 			}
 		}
 		array = array.call("duplicate"); //dupe, so undo/redo works better
+	} else {
+		int size = array.call("size");
+		// Pool*Array don't initialize their elements, have to do it manually
+		for (int i = previous_size; i < size; i++) {
+			Variant::CallError ce;
+			array.set(i, Variant::construct(array.get(i).get_type(), NULL, 0, ce));
+		}
 	}
+
+	emit_changed(get_edited_property(), array, "", false);
 	object->set_array(array);
 	update_property();
 }

+ 10 - 0
modules/gdscript/gdscript_parser.cpp

@@ -4691,6 +4691,16 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
 
 						ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
 						if (cn->value.get_type() != Variant::NIL) {
+							if (member._export.type != Variant::NIL && cn->value.get_type() != member._export.type) {
+								if (Variant::can_convert(cn->value.get_type(), member._export.type)) {
+									Variant::CallError err;
+									const Variant *args = &cn->value;
+									cn->value = Variant::construct(member._export.type, &args, 1, err);
+								} else {
+									_set_error("Cannot convert the provided value to the export type.");
+									return;
+								}
+							}
 							member.default_value = cn->value;
 						}
 					}