How can I Create a Score System Based on the Player's Height?

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

I am making a game where the player has to ascend as high as possible. I want to make a score system that is dependent on the player’s y-value, where the higher the player is (or the larger it’s y-value is) the higher the score is. I would imagine a solution would be to send a signal to the Score HUD (where my score variable will be assigned, refreshed and displayed), but I don’t know which signal to use or how to update the score variable once the signal is initialized. Any form of help would be greatly appreciated!

Is this sort of a jumping game where you jump up higher and higher? In your game does the player ever fall down, if so do you want the score to lower or stay the same?

jujumumu | 2022-02-21 03:26

The player does have to scend higher and higher, but the player uses a “drag and release” slingshot, instead of jumping. The player has to grapple onto objects in order to increase his altitude. I wish not for the score to decrease as I plan on eventually adding an area2D that will catch the player if he goes too far down. In this case, if the player falls down, the game ends, and I only want the score to increase.

InsertMeme | 2022-02-21 13:38

:bust_in_silhouette: Reply From: ponponyaya

I don’t know what’s your scene structure.
Anyway, I think use an Singletons (AutoLoad) to save the score variable will make it become easier to pass parameter/signal.

In your case, you can try this:

1.Create an Singletons (AutoLoad), for example “Global.gd”.
Look at this page to set script as Singletons (AutoLoad) if you don’t know.

2.[In Global.gd]

extends Node

signal score_updateed # custom signal.

var score: int  = 0

func updatScore(value: int):
	score += value
	emit_signal("score_updateed")

3.[When your player got higher]

Global.updatScore(10) # suppose add 10 points one time.

4.[In Score HUD’s Script]

func _ready():
	Global.connect("score_updateed", self, "_on_score_updateed")

func _on_score_updateed():
	# update your score on HUD.
	print(Global.score)

I have an issue when initializing the Global.updateScore() to the player. I do not know if I have to include it in a conditional/function or if the code is just in the wrong spot (it is currently in the player script), but putting that line of code in as it is results in an error, stating unexpected token: Identifier: Global. Is there any way to fix this?

InsertMeme | 2022-02-27 01:43

Have you ever set the Global.gd as an Singleton?
I mean set it in “Project → Project Settings->AutoLoad”.(Take a look at the first step)
If you already set the Global.gd as an Singleton, you may give me more information( your codes or image or error message…), so that I can give more help.

ponponyaya | 2022-02-27 03:05

I have already went ahead and named “Globals” under a Singleton here:

Global is called in my Score Hud script and works without any issue:

The Global does not work by itself in the Player Script:

Does this help?

InsertMeme | 2022-02-27 15:32

Ok! The question is those codes line should be put in functions.

If you want the score update every physics frame, try this:

func _physics_process(delta):
	......(skip)
	Global.updatScore(6)

Or you can use another function to update it, like:

func your_custom_function(): # and call this function when you need.
	Global.updatScore(6)

ponponyaya | 2022-02-27 23:20

Alright, we’re getting down to the wire, there should be a few more things I need to address. Putting Global.update_score() in my physics_process function allows the game to run without any issue. However, the score does not update at all. I was thinking about making a variable that sets an equation that creates a value, and that value would be the integer inside the update_score() method. I’m thinking about something along the lines of:

var scoreInt = position.y / 3 [[this number can be adjusted later]]
and then Golbal.update_score(scoreInt)

However, the game still does not show the output of the score. I have showed you my edited code and a picture of my output below:

Player’s script:

Output (the score is shown in the top left, but there is no value shown next to the “meters” label):

Is there any way to fix this?

InsertMeme | 2022-03-01 03:17

In fact, I did not expect you will ask this question. uh…it’s a really basic question. Anyway, we still can try to solve this question.
In the Score HUD’s Script. I wrote this:

func _on_score_updateed():
    # update your score on HUD.
    print(Global.score)

The line here “# update your score on HUD.” is a hint to let you update your node Label( or node RichTextLabel, I don’t know what node you are using). I mean you have to really update the label’s text by using code.

The question here is I don’t know your scene tree structure of Score HUD.
So the follwoing is just an ideas, you have to modify it to fix the question.

[In Score HUD’s Script]

func _on_score_updateed():
    get_node("Label path").text = "Score: " + str(Global.score)
    print(Global.score)

Modify the “Label path” to fix the question.
P.S. you also can use $ to get node.

If you really don’t know how to do that, you can show me the
scene tree structure of Score HUD(using image), so that I can give you more help.

ponponyaya | 2022-03-01 04:36