Hi,

I'm just starting out with Godot, and have some experience of C#.

My game has an energy counter in the corner which is a RichTextLabel.

The label has an Int Energy = 100 which is the Energy level, and it has a method which I was hoping to call from somewhere else to increase and decrease the Energy Level:

public void UpdateEnergy(int change)
    {
        Energy += change;
        this.Text = "Energy: " + Energy;
    }
}

Then from the Player KinematicBody2D, I want to call the UpdateEnergy, I do this by getting the Energy RichTextLabel by doing this:

    Node EnergyNode = GetNode("/root/Node2D/EnergyCounter");
    EnergyCounter = EnergyNode as RichTextLabel;

Then I was hoping to call:

EnergyCounter.UpdateEnergy(-10);

But I get a build error:

'RichTextLabel does not contain a definition for UpdateEnergy...'

Any advise on why I can't access UpdateEnergy? Thanks

I've edited the OP for formatting.


This discussion was caught in the moderation queue since you have not confirmed your account yet.
Upon creating your account you should have received an account verification email. The confirmation email may have been incorrectly flagged as spam, so please also check your spam filter. Without confirming your account, future posts may also be caught in the moderation queue. You can resend a confirmation email when you log into your account if you cannot find the first verification email.
If you need any help, please let us know! You can find ways to contact forum staff on the Contact page. Thanks! :smile:

Welcome to the forums @archie456!

You need to cast the object to the class that contains the UpdateEnergy function. The code isn’t working because you are casting it to a RichTextLabel, which doesn’t have the custom function. My guess is that your class extends the RichTextLabel class, but to access the custom functions you’ll need to cast it to your class so C# knows what to call.

I think you may be able to also use the Call function and therefore not need to cast to your custom class, but this is probably not the best way to handle it, as you miss out on C# specific benefits (code completion, argument detection, etc) and its harder to debug.

2 years later