Godot: GDScript cannot always infer types

Created on 5 Apr 2020  路  3Comments  路  Source: godotengine/godot

Godot version:
3.2.1

OS/device including version:
Windows 10

The gdscript editor cannot seem to infer object types most of the time making it impossible to get popup help for near enough all objects. Without this functionality, coding is near impossible, especially for newcomers or those wishing to ensure the method/constant they are typing is correct.

i.e. you expect on pressing '.' that the editor knows what the object type is and provide the correction information. Most other dynamically typed language editors provide this.

Example:
In the image below a collider was retrieve, yet on typing 'collider.' only top level functions were provided. It was only when the type was explicitly declared as with collider2, that the extra functions, specifically 'is_in_group' in my test case became available.

image

gdscript

Most helpful comment

I can see what you're saying, but the only objects a raycast2d collider can return (currently) is Area2D and a PhysicsBody2D.

Hmmm, I think you've got a point here.
To any project contributor reading this: is there a reason that RayCast2D.get_collider() returns Object and not something more narrow like Node or CollisionObject2D?

If everything by default simply returns an 'Object'

Not everything returns Object, have a look at the other RayCast methods, they all return different types that make sense for them to return.

I'm just wondering/asking if there's isn't a better way to at least infer the best highest level object type.

Use type casting!

var collider_object := get_collider() as PhysicsBody2D
# or
var collider_object: PhysicsBody2D = get_collider()

This forces the specified type onto the variable and gives the editor the ability to give you better suggestions. This section in the docs discusses just that!

All 3 comments

Well, judging by what you described, it's normal behavior.
Here:

var collider
collider = ray.get_collider()

the editor doesn't give you specific suggestions because get_collider() (assuming it's a method of RayCast node) returns a generic Object and thus the only methods and properties it can suggest are that of Object. The editor can't know what object type collider will get until you run the game and that object becomes available.

In the second case:

var collider2: CollisionObject2D
collider2 = ray.get_collider()

The editor provides you with more suggestions because you specified that collider2 will hold an object of type CollisionObject2D and therefore the editor knows members of which type to suggest.

Well, judging by what you described, it's normal behavior.

I can see what you're saying, but the only objects a raycast2d collider can return (currently) is Area2D and a PhysicsBody2D. Both stem from Node.

If everything by default simply returns an 'Object' then it'll be impossible to return even the most basic of information, of which is_in_group is part of Node, meaning when coding intellisense is of no use and won't even show is Node properties/methods which it should be showing.

I'm just wondering/asking if there's isn't a better way to at least infer the best highest level object type.

I can see what you're saying, but the only objects a raycast2d collider can return (currently) is Area2D and a PhysicsBody2D.

Hmmm, I think you've got a point here.
To any project contributor reading this: is there a reason that RayCast2D.get_collider() returns Object and not something more narrow like Node or CollisionObject2D?

If everything by default simply returns an 'Object'

Not everything returns Object, have a look at the other RayCast methods, they all return different types that make sense for them to return.

I'm just wondering/asking if there's isn't a better way to at least infer the best highest level object type.

Use type casting!

var collider_object := get_collider() as PhysicsBody2D
# or
var collider_object: PhysicsBody2D = get_collider()

This forces the specified type onto the variable and gives the editor the ability to give you better suggestions. This section in the docs discusses just that!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

testman42 picture testman42  路  3Comments

rgrams picture rgrams  路  3Comments

blurymind picture blurymind  路  3Comments

mefihl picture mefihl  路  3Comments

EdwardAngeles picture EdwardAngeles  路  3Comments