Call function from another script (GDScript)

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

I currently have a Player scene and its script wants to run a dead() function from an Enemy scene (another scene) that changes a varible and behavior in the Enemy Scene (kills the enemy)
The problem I’m struggling with is running a function via one scene to another scene and changing a variable in the other scene.
What do I do?

:bust_in_silhouette: Reply From: jgodfrey

Assuming you have a reference to the instance of the Enemy scene in question, you can just call the function directly.

So, something like:


…will work as expected, as long as the_enemy is a reference to your enemy instance.

I assume you trying to access the enemy instance from some collision or area entered (or similar) event, where you should have a valid reference to work with.

Do I need to access the other scenes script somehow? Now when i run enemy.dead() I get the following error: “Non-Static function “dead” can only be called from an instance”

Dexter_ | 2020-08-21 20:34

You’ll probably need to show some code so we can see where (in your Player script) you’re trying to access your Enemy. As I mentioned, you need to have a reference to a specific instance of your Enemy in order to call its dead() function.

Generally, if you’re doing that based on some collision-like callback function, you are given the instance you need to work with directly in the callback.

Again, some code would help to clarify exactly what you’re trying to do here…

jgodfrey | 2020-08-21 20:44

var greenslimescene = preload("res://greenslime.tscn")
var instance = greenslimescene.instance()

if Input.is_key_pressed(KEY_T):

I tried adding the scene using the variables above, and the if statement is under func _physics_process(delta):

You’re correct with the collisions, but I just want to make sure that the dead() function works and therefore added it under the if statement seen above

EDIT: the variables and the if statement is in the Player scene and are trying to access dead() from the greenslime script

Dexter_ | 2020-08-21 20:48

And, what was the result of the experiment? Generally, I’d expect it to work, though with a few caveats…

  • You never actually added the new instance to the scene, so you will never see it.
  • The first frame where you press the “T” key should indeed call the instances dead() method, but assuming that call frees the instance, additional frames where the “T” key is still being held down will generate some sort of null reference error, since the reference is no longer valid.

jgodfrey | 2020-08-21 21:29

It prints “kill” into the console but the slime doesn’t die, is unfortunately what happends :frowning:


The dead() function contains the following:

func dead():
	isdead = true
	velocity = Vector2(0,0)

And when isdead is true the movement of the green slime will stop.

Dexter_ | 2020-08-21 21:49

When you say “doesn’t die”, what does that mean? What are you expecting the slime to do? Should it just stop moving, be removed from the scene, ??.

Are you questioning whether the dead() function is actually being called? If that’s the case, you could put a print() statement in it to see if it’s called, which I think it should be.

The original question was “how to call a function in another scene”. Whether or not that function actually does what you intend is a different topic all together…

jgodfrey | 2020-08-21 21:55

Yeah, I get that it’s another topic, just wanted to give you some more information!
What should happen is that the slime should stop moving and be totally still!

And I just added a print to the dead() function and still nothing happends to it, but atleast I access the function from another scene, so thank you very much for the help. Now I just got to sort out why it doesn’t die!

Also quick question, it is possible to change values of vars in the function im calling from another scene, right?

Dexter_ | 2020-08-21 21:59

If you added a print statement to the dead() function and don’t see it when you call the function, then something still seems wrong. It’s hard for me to say more without seeing the project…

Also quick question, it is possible to change values of vars in the
function im calling from another scene, right?

Yes, with a valid reference to the instance, you can change variable values in the instance.

jgodfrey | 2020-08-21 22:08

My bad, the print gets shown, but the greenslime still moves, but i think that’s something else than what i originally asked for!

Dexter_ | 2020-08-21 22:10