0 votes

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?

in Engine by (27 points)

1 Answer

+1 vote
Best answer

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

So, something like:

the_enemy.dead()

...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.

by (21,864 points)
selected by

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"

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...

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

if Input.is_key_pressed(KEY_T):
    print("kill")
    instance.dead()

I tried adding the scene using the variables above, and the if statement is under func physicsprocess(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

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.

It prints "kill" into the console but the slime doesn't die, is unfortunately what happends :(

EDIT:

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.

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...

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?

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.

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

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.