+1 vote

Hello,

im making a simple game for my coursework, i have a coin scene, menu scene, and level scene. i want to show how much i collect coin on level to menu scene but i dont know how.
help me please

in Engine by (15 points)

4 Answers

+1 vote

You can use an autoloaded script to save/share state between scenes. See https://docs.godotengine.org/en/3.1/getting_started/step_by_step/singletons_autoload.html for getting it set up.

Here is a minimal use case outline:

# GameState.tscn
var coins_collected = 0

Then in your level scene (I don't know how you are detecting when a coin is collected, but just move this to wherever that might be)

# Level.tscn

func _on_coin_collected():
    GameState.coins_collected += 1

Then in your menu scene:

# Menu.tscn

func _ready():
    $CoinsLabel.text = str(GameState.coins_collected)
by (1,616 points)
edited by

hi why do you put dollar sign in front of the code?
Necessary?

Assuming the node that the script is attached to has a child node called CoinsLabel, instead of using get_node("CoinsLabel"), you can use the $ short-hand and just reference it as $CoinsLabel

0 votes
get_tree().get_root().get_node("Spatial/node1/node2").get("variable")

The other way is by adding the scenes in [project settings] > [AutoLoad]

so, if you adds level 1, and name it to level1, then you can get access to it like:

level1.get_node("node1").get("coins_variable")
by (195 points)
0 votes

I have founded a good Video to your question
https://youtu.be/w_guC-Wq3jc

by (14 points)
0 votes

You need to understand and make use of signals. We need to make our scenes independent of outside scenes. Your scene should work stand-alone for testing purposes. Have a look at my tutorial here to get a solid understanding of this topic: https://gdscript.com/solutions/signals-godot/

by (191 points)
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 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 webmaster@godotengine.org with your username.