Why can I not call methods sometimes?

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

I am spawning an instance of a dagger.tscn. It has a method in it “getDirection”. The script is linked to the root node, which is a RigidBody2D.

When I instance it, and call “getDirection” on the instance, I am unable to call getDirection(), it says that getDirection() is not a known method for RigidBody2D.

:bust_in_silhouette: Reply From: jgodfrey

Your original question is confusing. You say that you spawn an instance of dagger, that has a method getDirection() (which implies to me that the dagger instance has a script which contains a getDirection() function). But, then you say the script is linked to the root node - which is different.

So, what is the script (that contains the getDirection() function) actually associated with - the root or the dagger instance?

Based on the error, I’d guess it’s associated with the dagger itself. If that’s the case, I assume you’re not calling getDirection() on the dagger instance. Without code, it’s just a guess, but that should look something like this:

var dagger = preload("res://Scenes/dagger.tscn")
var instance = dagger.instance()

Notice above, that getDirection() is being called on an instance of dagger in this case.


Sorry yes I meant the root of the dagger scene. It’s working now. Thanks.

indy2005 | 2020-09-24 22:52