0 votes

I can call the class, but how do I call the function?

This is the code for the class I want to call.


class_name MyClass

func FuncA():

I want to call FuncA in this class.


extends KinematicBody

func _ready():

    var myClass = MyClass.new() #Non Error.

    myClass.FuncA() #This is Error.

Sorry for the elementary question. Am I missing something?

Godot version 3.4
in Engine by (25 points)

1 Answer

0 votes

UPDATE - The below answer was based on a misunderstanding of the original question. See the conversation in comments below for more helpful information. (I wish the forum supported strike-through text for better visibility here...)

You're probably running into the same misunderstanding as described (and resolved) here:


by (12,460 points)
edited by

I have done the same but it does not work.
its Q&A seems to have calling a script, but I seem to have calling a reference on my end.
Also, what is the difference between "class" and "class_name"?

Hmmm.. Sorry, looks like I misread your post. What you show should work as expected.

If I create a file named MyClass.gd with this content:

class_name MyClass

func FuncA():
    print("Called FuncA")

... and then, from another script, do this (for example):

func _ready():
    var myClass = MyClass.new()

The function is called and I see called FuncA printed in the console. Is what you're doing somehow different?

To your earlier question class_name just registers a name (and, optionally, an icon) for the class represented by the current script. Theclass keyword creates an inner class.

Search for those terms here for more info: https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html?highlight=class_name

I was trying different things and it worked!
As a matter of fact, I had named the class StateMachine (for clarity for questions).
It did not work with the name StateMachine, but it worked fine with the names MyStateMachine and StateMachine_.

I didn't understand what was going on, but it worked anyway. Thank you for your patience.

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.