0 votes

How would I go about calling a function from a different scene? I have a level and I want to change the scene to the victory_achieved scene once my player reaches the target. But I'm lazy so instead of making a victory_achieved scene for every level I want to use the same one but change the text of the labels to read something else. In this case I make a function for this text changing event then I put the text as one of the function arguments. This is all well and good but I need to call it from the level script and that is where I am stuck. How would I call that function, then change the scene but keep the text as what was stored in the arguement?

in Engine by (34 points)
edited by

1 Answer

0 votes

You need an autoload script. Basically you can just create a node with a script attached and add a variable to hold a string. Then go into project > project settings. In the tabs you will see autoload. Go to the autoload screen and you can add any node to this, what this will do is automatically load the scene you add whenever you load any other scene, so it is the equivalent of adding it to each scene you load in the game.

What you can then do is change the variable in the autoload script to hold the text you want and then on your victory screen just look at the text in the autoload script and use that to assign to the label.

by (3,326 points)

I was basically going to suggest the same thing. Autoloads are perfect for managing scene transitions instead of doing that in each script.

My level complete screen is a spatial node with textures so when I autoload it appears in every scene as visible. I add a hide method to the ready function?

You do not make that an autoload, you create an autoload to create the level complete scene and show it, as well as free it when it's no longer required.

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.