All Questions

Community

W
Wuz Works

Explicit Object Typing

In one of the older tutorials I have, there is the following section of code:

var body = get_slide_collision(0).collider
if body.is_in_group('character'):				
  body.take_damage(self, 4)

This really surprised me as there is cerainly no "take_damage()" method on Godot's collider class.

1) I initially would have expected a collider object to be returned, not a collider:character:player object. In C#, I'd expect to have to do this explicitly, so I'm guessing this is a common behavior pattern throughout Godot?

2) Is there a more explicit way to do this rather than check a tag and then presume the indicated function/object type is present? Like:

var body = (character)get_slide_collision(0).collider

3) Or is there a way we could have done a typeof("character") test instead? That would telegraph what's going on a little better? Or is that slow, like reflection in C#?

I'm definitely not expecting intellisense to work for this approach.