Score variable does not change

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

I tried to make an endless running game to find out. but my score variable doesn’t change with every block the character jumps.

my project:

I’m new to Godot So I’m waiting for your suggestions on code and engine on this project. :slight_smile:

:bust_in_silhouette: Reply From: Scavex

You can create a global script for your score ! It’s probably one of the solutions you can choose. Surely someone else will come up with even a better solution but I want you to do the following as it’s also a way to deal with it :

  1. Create a new script and name it global or whatever you want to. This will be your global script. We will be going with the Singleton approach with this solution. You can learn more about singletons from here Singletons (Autoload) — Godot Engine (stable) documentation in English.
  2. Once you have made your global script just declare the var count = 0 in it instead of declaring it in your world.gd
  3. Now you have to go to Project > Project Settings from the menu and select Autoload tab. It will be the fourth tab, you’ll easily see it.
  4. There you will have an option of Path. Select the folder icon in front of it and choose the global script that you just created in Step 1. There will also be an Option of Node Name where you can give a name to your singleton. I recommend naming it to global. Finally hit Add
  5. Now make this change in your world.gd :
func score():
	global.score += 1
	print("score ",global.score)

Now you can use global.score globally in any script you want to.
Also do this for resetting the score to 0 once the scene restarts :

func _on_Area_body_entered(_body):
	global.score = 0
	get_tree().reload_current_scene()

thank you so much my friend. You are the king. :slight_smile:

SDGN16 | 2021-02-01 18:04