Call function from Script attached to Instance

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DerErsteMuselmann

Im really new to Godot and have a fairly simple question that I just cant seem to wrap my head around: How to i call a function of a Script attached to an Instance?

For example:

In the Main Script of a test Project of mine there are the following lines of code:

var scene = preload("res://Ball.tscn")

func _ready():
	var test = scene.instance()
	test.test()

The Script attached to Ball contains the following:

func _test():
	print("Success")

The error i get if i try to run this code is "Invalid call. Nonexistent function ‘test’ in base ‘RigidBody2D (Ball.gd)’

Obviously there is no function called test in RigidBody2D which seems to be the error here, but then how do i refer to the Script of an Instance?

Thanks in advance for any help provided.

:bust_in_silhouette: Reply From: p7f

Hi,

In your script, the function is called _test() but then you try to call it test.test(). Have you tried removing the underscore before test in your script?

Thanks that was it, i thought you had to start every function name with an underscore because the integrated functions had them, thanks a lot :smiley:

DerErsteMuselmann | 2019-01-19 09:18

Your welcome!

p7f | 2019-01-19 11:20