0 votes

I was wondering if there is a way to confirm the class of a Node. I'm trying to make one script that can be used on any type of node, checks the Node's class, and sets specific values based on that class.

Basically a more elaborate version of this:

Extends Node

if self.is_class("Control"):
        self.get_stylebox("panel").set_bg_color(Color(#ffffff))
elif self.is_class("Sprite"):
        self.modulate = Color(#ffffff)

Sadly doing this gives an error (error(): The Method "get_stylebox()" isn't declared in the current class), because it extends Node rather than Control. It gives this error even though there is no way the get_stylebox() method will ever call on a non-Control node.

I was hoping there was a way to confirm that self is a Control node, so get_stylebox() is a valid Method, or some other way to work around this.

By the way, making the script extend Control instead, has the issue that the script cannot be used on Sprites or any other non-Control node, so that won't do the trick.

in Engine by (42 points)

1 Answer

+1 vote
Best answer

IMHO what you want to do feels very weird however Godot does not seem to have any restrictions on somevariable.callingsome_function() so just use a variable

extends Node

func _ready():
    var node = self
    if self.is_class("Control"):
        node.get_stylebox("panel").set_bg_color(Color("#ffffff"))
    elif self.is_class("Sprite"):
        node.modulate = Color("#ffffff")
by (4,128 points)
selected by

Thank you! Making it a variable does indeed work.
Feels like I'm cheating the system, but results is all that matters :D

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 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.