0 votes

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!

Godot version v3.3
in Engine by (14 points)

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?

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.

1 Answer

0 votes

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)
by (526 points)

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?

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.

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

https://snipboard.io/4TunsB.jpg

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

enter image description here

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

enter image description here

Does this help?

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)

Alright, we're getting down to the wire, there should be a few more things I need to address. Putting Global.updatescore() in my physicsprocess 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:
enter image description here

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

enter image description here

Is there any way to fix this?

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.

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.