How to count up from inside a function?

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

New to programming and I’m having some trouble with incrementing a value up from within a function. I’m using the following code:

extends Node2D

var coins = 0

func testfunction(a):
	a += 1
	print(a)

func _on_Timer_timeout():
	testfunction(coins)

All this does is print the number 1 over and over again every time the Timer times out (every 0.5 seconds), when I want it print 1, 2, 3 and so on. What am I doing wrong here?

:bust_in_silhouette: Reply From: sirAlexDev

Try this. But i recommend you to learn singleton and use it in the future for such global information like coins collected by player.

extends Node2D

var coins = 0

func testfunction() -> void:
    coins += 1
    print(coins)
    

func _on_Timer_timeout():
    testfunction()