0 votes


Using Godot 3.1, I'm trying to do this:


var Foo 


var Bar : classA

But I get an 'classA is not a valid type' error. Any hints?

Thanks in advance,

in Engine by (12 points)

2 Answers

0 votes

The operator : in Godot is used to say which is the variable type. For example, if you have a variable example and you want to make sure that it will always be and integer, you can do var example : int.
If you want to create a class and create a variable in this class, you need to do the following:

class Foo:
    var bar = “var1”
    var foo : int = 1

To get a variable/call a method outside the class, you need to do Foo.new().bar, which will return “var1”.

Unless you really need it, I would not recommend using classes. At least for me, I never found myself in a situation where a class would help me.

If you want a script to perform multiple things and keep you code organizated (at least for me this is the reason I would use a class) you can create a node only to hold a script and if you need this script in multiple places you can save this node as a scene and instanciate it where you need.

by (519 points)
+2 votes

You need to either use class_name for classA or load classA as a resource in classB:

var cA = load("classA.gd")
var instanceOfTypeClassA : cA

See Docs on static typing in the section "Custom variable types"

by (1,517 points)
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 [email protected] with your username.