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,

asked Jan 26, 2019 in Engine by cybin (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.

answered Jan 26, 2019 by fpicoral (502 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"

answered Jan 26, 2019 by omggomb (653 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 How to use this Q&A? before posting your first questions.