Skip to content

Commit

Permalink
Allow typed variables and function arguments to be null
Browse files Browse the repository at this point in the history
  • Loading branch information
toasterofbread committed Aug 15, 2021
1 parent e1129ad commit 47bbcb9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) {

if (p_type_from == p_type_to)
return true;
if (p_type_to == NIL && p_type_from != NIL) //nil can convert to anything
if (p_type_to == NIL || p_type_from == NIL) //nil can convert to and from anything
return true;

if (p_type_from == NIL) {
Expand Down Expand Up @@ -439,7 +439,7 @@ bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type

if (p_type_from == p_type_to)
return true;
if (p_type_to == NIL && p_type_from != NIL) //nil can convert to anything
if (p_type_to == NIL || p_type_from == NIL) //nil can convert to and from anything
return true;

if (p_type_from == NIL) {
Expand Down
4 changes: 4 additions & 0 deletions core/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,10 @@ Variant Variant::construct(const Variant::Type p_type, const Variant **p_args, i
} else if (p_argcount == 1 && (!p_strict || Variant::can_convert(p_args[0]->type, p_type))) {
//near match construct

// nil should be passed to any type as nil
if (p_args[0]->type == NIL)
return Variant();

switch (p_type) {
case NIL: {

Expand Down
4 changes: 4 additions & 0 deletions modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6232,6 +6232,10 @@ bool GDScriptParser::_is_type_compatible(const DataType &p_container, const Data
if (!p_container.has_type || !p_expression.has_type) {
return true;
}
// Nil can be assigned to any type
if (p_expression.builtin_type == Variant::NIL) {
return true;
}

// Should never get here unresolved
ERR_FAIL_COND_V(p_container.kind == DataType::UNRESOLVED, false);
Expand Down

0 comments on commit 47bbcb9

Please sign in to comment.