0 votes

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:
https://dosya.co/1z2swnttu6xw/gms.rar.html
I'm new to Godot So I'm waiting for your suggestions on code and engine on this project. :)

Godot version 3.2.3
in Engine by (47 points)

1 Answer

+1 vote
Best answer

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 https://docs.godotengine.org/en/stable/getting_started/step_by_step/singletons_autoload.html.
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()
by (413 points)
selected by

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

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.