Merge pull request #70980 from vonagam/fix-array-as-default-parameter
GDScript: Fix array as default value for parameter
This commit is contained in:
commit
2e11627110
3 changed files with 37 additions and 1 deletions
|
@ -2025,7 +2025,7 @@ GDScriptFunction *GDScriptCompiler::_parse_function(Error &r_error, GDScript *p_
|
|||
uint32_t par_addr = codegen.generator->add_parameter(parameter->identifier->name, parameter->initializer != nullptr, par_type);
|
||||
codegen.parameters[parameter->identifier->name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::FUNCTION_PARAMETER, par_addr, par_type);
|
||||
|
||||
if (p_func->parameters[i]->initializer != nullptr) {
|
||||
if (parameter->initializer != nullptr) {
|
||||
optional_parameters++;
|
||||
}
|
||||
}
|
||||
|
@ -2103,6 +2103,17 @@ GDScriptFunction *GDScriptCompiler::_parse_function(Error &r_error, GDScript *p_
|
|||
return nullptr;
|
||||
}
|
||||
GDScriptCodeGenerator::Address dst_addr = codegen.parameters[parameter->identifier->name];
|
||||
|
||||
// For typed arrays we need to make sure this is already initialized correctly so typed assignment work.
|
||||
GDScriptDataType par_type = dst_addr.type;
|
||||
if (par_type.has_type && par_type.builtin_type == Variant::ARRAY) {
|
||||
if (par_type.has_container_element_type()) {
|
||||
codegen.generator->write_construct_typed_array(dst_addr, par_type.get_container_element_type(), Vector<GDScriptCodeGenerator::Address>());
|
||||
} else {
|
||||
codegen.generator->write_construct_array(dst_addr, Vector<GDScriptCodeGenerator::Address>());
|
||||
}
|
||||
}
|
||||
|
||||
codegen.generator->write_assign_default_parameter(dst_addr, src_addr);
|
||||
if (src_addr.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
|
||||
codegen.generator->pop_temporary();
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
func print_untyped(array = [0]) -> void:
|
||||
print(array)
|
||||
print(array.get_typed_builtin())
|
||||
|
||||
func print_inferred(array := [1]) -> void:
|
||||
print(array)
|
||||
print(array.get_typed_builtin())
|
||||
|
||||
func print_typed(array: Array[int] = [2]) -> void:
|
||||
print(array)
|
||||
print(array.get_typed_builtin())
|
||||
|
||||
func test():
|
||||
print_untyped()
|
||||
print_inferred()
|
||||
print_typed()
|
||||
print('ok')
|
|
@ -0,0 +1,8 @@
|
|||
GDTEST_OK
|
||||
[0]
|
||||
0
|
||||
[1]
|
||||
2
|
||||
[2]
|
||||
2
|
||||
ok
|
Loading…
Reference in a new issue