Why does the counter only work once and then never agian?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By HarryCourt
:warning: Old Version Published before Godot 3 was released.

So, imagine a coin counter. If the player collects the coin, it adds one to the counter, and it will do that forever. However, my script doesn’t work and once it collects one coin/tile, it won’t add to anymore. I think I may have done something wrong with the addition. Look for the comment that states where I am having trouble.

Player.gd:

extends KinematicBody2D
    
#const GRAVITY = 200.0
const WALK_SPEED = 1200
var MyPos = self.get_pos()
var victory = false

var velocity = Vector2()
func _ready():
	set_fixed_process(true)
	#var teleportTile = get_tree().get_root().get_node("World/TogglableNodes/TeleportOutput").get("teleportPos")

func _fixed_process(delta):
	
	var motion = velocity * delta
	move(motion)
	
	if is_colliding():
		var collider = get_collider()
		
		var count = get_tree().get_root().get_node("World/Counter").get("counter")
		
		# I'm having trouble here.
		if collider.is_in_group("Tile"):
			var textCount = get_tree().get_root().get_node("World/Counter")
			
			count += 1
			textCount.clear()
			textCount.add_text(str("Tiles: ", count))
			
			get_node("SamplePlayer").play("TileOn2")
			collider.queue_free()
			
		elif collider.is_in_group("Teleporter"):
			get_node("SamplePlayer").play("Finish")
			
			#set_global_pos("teleportTile")
			set_global_pos(Vector2(get_tree().get_root().get_node("World/TogglableNodes/TeleportOutput").get("teleportPos")))
			collider.queue_free()
			
		elif collider.is_in_group("Goal"):
			victory = true
	
	velocity.x += delta #* GRAVITY
	velocity.y += delta #* GRAVITY
	
	if (Input.is_action_pressed("ui_left")):
	    velocity.x = -WALK_SPEED
	elif (Input.is_action_pressed("ui_right")):
	    velocity.x =  WALK_SPEED
	elif (Input.is_action_pressed("ui_down")):
	    velocity.y = WALK_SPEED
	elif (Input.is_action_pressed("ui_up")):
	    velocity.y = -WALK_SPEED
	else:
	    velocity.x = 0
	    velocity.y = 0

Counter.gd (Not much here, thought I might add it in anyways):

extends RichTextLabel

var counter = 0
var text = set_text(str("Tiles: ", counter))

func _ready():
	set_process(true)
	
func _process(delta):

I’m not so sure how this line works:
var count = get_tree().get_root().get_node("World/Counter").get("counter")
but are you sure it gets the reference and not only the value?
try replacing the use of the count variable by textCount.counter or something alike so it will access the counter variable directly instead of trying to pass the reference around

rustyStriker | 2017-09-24 12:51

:bust_in_silhouette: Reply From: quijipixel

You are forgetting to save the counter data back to the Counter node

What happens is that you get the counter initial value, add 1 to the local copy of the counter value (not the counter in your World/Counter node) and when the _fixed_process finishes, that local value is lost. So, it will always have the same value forever. Add this line after changing the text:

 get_tree().get_root().get_node("World/Counter").set("counter", counter)

Much thanks :slight_smile:

HarryCourt | 2017-09-25 07:45