0 votes

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?

in Engine by (12 points)

1 Answer

0 votes

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