0 votes

I'm trying to convert a tutorial script that's in gdscript to c# as I am more comfortable with it. However, in the tutorial script they are able to call a method from an object, whose datatype will be a RigidBody child, from the function parameters only if it exists, i.e. after checking the existence with the has_method function. However, I can't seem to find a way to call the function from the object in c# as it's a function that's going to be a custom method in a child class (of a RigidBody). Is there a way to do this in c# or is it only usable for gdscript?

Godot version v3.3.4
in Engine by (52 points)

Can you show us the GDscript example?

The GDScript example goes as shows:

func collided(body):
    if hit_something == false:
        if body.has_method("bullet_hit"):
            body.bullet_hit(BULLET_DAMAGE, global_transform)

hitsomething is a property of the overall object. bullethit is a method of a different class that will be created later in the tutorial. For c#, is there a way to call it even if the class has not been created yet similar to the gdscript or will I only be able to call the function once the class is created and the function is defined?

1 Answer

+1 vote
Best answer

Let's assume your RigidBody has a C# script named "Player" attached (class Name) and the Player has a public method named "BulletHit".

This should fit your needs:

private void Collided(Node body) 
    if (!hitSomething && body is Player player)
        player.BulletHit(BULLET_DAMAGE, GlobalTransform);

You just check if the colliding Node is of type Player.

by (718 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.