Cool idea but I lacks on skills...

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

Hi, i want to make a cool money counter that slowly change its value when you found a bunch of coins.
I tried coding a function that do this, but I cant figure out how to make it work…
I have the func in the CoinsCounter scene (autoloaded) and i want to call the function in every scene i found coin.
Pls help me :frowning:

:bust_in_silhouette: Reply From: SnapCracklins

A loop, array and label can probably do this “rolling” effect you are wanting. You can try this, which I did, and it will get the effect you want. This is just a Global script attached to a 2D node with a Label as child. You will likely want to play with something along this line.

Essentially:

  1. you create an empty array and fill it every time you have a money pickup.
  2. you create a timer in script (you could also just make a node if you want)
  3. A loop goes through the pickup array and adds it to your total
  4. The “update” waits for the timer between each amount by yielding to its timeout signal.
  5. This gets you your rolling effect as it allows you to see the value each add thanks to the buffer gap. I also exported the buffer time as a variable so you can adjust it from the editor.

You will likely want to make this into an extension/class_name to re-use later or a global, as you said.

extends Node2D


var money_grabbed = []
var money_total = 0
var buffer = Timer.new()
export var bufferTime = 0.5

func on_money_grabbed(money_grabbed):
	for i in money_grabbed:
		money_total = money_total + i
		print(money_total)
		$Label.set_text(str(money_total))
		buffer.start()
		yield(buffer, "timeout")
		
func _ready():
	add_child(buffer)
	buffer.set_wait_time(bufferTime)
	money_grabbed = [1, 2, 3, 4, 5]
	on_money_grabbed(money_grabbed)
:bust_in_silhouette: Reply From: clappy

I think in the while loop where the coins are sorted out for a smooth display on the screen, you need to apply a delay using

yield(get_tree().create_timer(0.1), "timeout")