Godot: GDScript shows no error on invalid typecasting

Created on 14 Aug 2019  路  7Comments  路  Source: godotengine/godot

Godot version:
3.1

OS/device including version:
Mac 10.14.5

Issue description:
When creating a variable I set the type and then assign the variable. But when I then access the variable it doesn't give me any feedback when I actually take a property that doesn't exist and set that to a value.

The same with taking a property that exists and setting that to the wrong type value.

Steps to reproduce:

  1. Create some node as a child and attach a script to the parent.
    in my example I just had a node2D for the world with a script and a ColorRect as child
var colorRectChild: ColorRect = get_child(0)
colorRectChild.color = 12 # This doesn't give any kind of compile error or warning

# Neither does this
colorRectChild.sdlfkjsdlf = 20
archived gdscript

Most helpful comment

Regardless of what get_child returns, the typecast to ColorRect should give it enough information to return an error.

All 7 comments

The problem is caused by the get_child() call. It can't tell if the node you got truly will be a ColorRect. Hinting _should_ take precedence here. But the use of get_child is _also_ bad practice.

Tbh, I don't recall it breaking like that in any of my projects. There could be something else at play.

This _will_ crash once you run it btw.

Thanks for the quick reply @TheDuriel

The problem is caused by the get_child() call. It can't tell if the node you got truly will be a
ColorRect. Hinting _should_ take precedence here. But the use of get_child is _also_ bad practice.

Got in to Godot a couple of days ago, how would I go about getting a child node then?

Tbh, I don't recall it breaking like that in any of my projects. There could be something else at play.

This is a newly created project and happens on both Mac and Windows. I believe this is a result of the nature of a scripting language which lets you call properties even though they don't exist. But I believe the static typing's should "fix" that, I may be completely wrong about it though.

This will crash once you run it btw.

I know this will break when running but the point of static typing is so that I am aware of that before I actually run it, is it not?

But the use of get_child is also bad practice.

The function is there for a reason. It's useful when iterating with get_child_count() and for very simple scenes with one or two children, where you know the structure won't change.

how would I go about getting a child node then?

In big scenes, the practice is to use an onready variable with get_node(). Or to be more flexible (like, when node name changes), exporting a NodePath.

btw, GDScript doesn't need semicolons and uses # for comments.

@KoBeWi Thanks for the info and now I know a little more about when to use onready and get_node()

Edited the code to use # for comments and no semicolons

Regardless of what get_child returns, the typecast to ColorRect should give it enough information to return an error.

Regardless of what get_child returns, the typecast to ColorRect should give it enough information to return an error.

@RobertBrunhage I think you should rename the issue to something like "GDScript shows no error on invalid typecasting".

var colorRectChild: ColorRect = get_child(0)
colorRectChild.color = 12 # This doesn't give any kind of compile error or warning

This isn't a GDScript bug. There is a valid conversion from int to Color.

Maybe we want to change Color, to not accept int directly (only with the explicit constructor) but it's another discussion.

EDIT: You can easily see this working by assigning: colorRectChild.color = Color.blue.to_rgba32(). It will be set as blue.

# Neither does this
colorRectChild.sdlfkjsdlf = 20

This is also not a problem. It can be a child class of ColorRect, which can have the property and the code would be valid due to duck-typing. The line is not marked as type-safe (green line number) so it's working as intended.

Was this page helpful?
0 / 5 - 0 ratings