Updating The Score Label After Player Hits a collectable.

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

Im Struggling To Understand how to get this to work so any help would be appreciated.
Im trying to update the label With The current score.

The Label is in the main scene, Its a child of a canvas layout while the collectable is its own scene that isn’t called in the scene, its called by a script after a timer.

Here is my current on-hit update score script.

func _on_Collectable_body_entered(body):
if body is KinematicBody:
	if (body.get_name() == "Player"):
		global.score += 1
		print(global.score)
		queue_free()
	else:
		print("gone")
		queue_free()

This works perfectly! it updates the global.score.

But how can I get it to update the label with the global.score variable after the collectable is removed?

I’ve tried the “$” way as suggested in the “build your first game” docs. This doesn’t work.
get_node doesn’t find anything at all.

I’ve tried preload. Ive added a function in the label script and it doesn’t find it or crashes.

All i want is for the score to be updated. I’ve been trying to work out how to update it via emit_signal But this still isn’t working. I’m really struggling with this issue. Can anyone help me with this problem?

I think I’ve explained it correctly.


Just in case:
When the player hits the collectable, The collectable being in its own scene, it then updates the label with the current score and constantly updates.

Thank you =)

:bust_in_silhouette: Reply From: wyattb

Did you try using an absolute path E.g. var obj=get_node(“/root/scenename/nodename”)

Also post code sample when asking questions like this.

Thank you for that. It worked. I saw this solution a bunch and it didn’t work. Idk why yours worked fine? Strange But thank you every much!

Appreaciated

Archtects | 2018-09-19 11:23

Just one more comment. What you should now understand is how to use relative paths. The above absolute path idea is to show that getting the node works. But it is not the recommended way of doing this.

Figure out where your node is in relation to the scene and use relative paths which allows you more flexibility to move parent nodes around.

E.g. obj=get_node("../nodename") which if this worked it means the current node is a child of the node containing it.

FYI you can use the shortcut $"/root/scenename/nodename" or $"../nodename" instead of the get_node() function.

wyattb | 2018-09-19 15:31