Search code examples
classmethodsnodesgodotgdscript

Class method returns null every time


I've made a class script that looks like this:

extends Node

class_name Class

func method():
    pass

Then I've added a child node using that script in my main scene.

Next, in my main scene gdscript, I've added this bunch of code:

extends Node2D

var class = $Class # That's the child node using my previous script

func _ready():
    print(class.method())

And no matter what I put in the method() function, the main script will always print "Null" in my output. I've tried putting stuff to print and it behaves exactly like it should except that it still prints "Null" at the end of my output.

Does anyone know how to fix this?


Solution

  • It doesn't look like class.method() returns anything.

    Currently you're asking GDScript to print the result of class.method(). It will run the method, but if nothing ever gets returned out of it, it just defaults to Null.

    func method():
        return  "Hello World"
    

    If you were to call print on the above, it would print "Hello World" because that's what's being returned.