0 votes

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):
in Engine by (162 points)

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

1 Answer

0 votes
Best answer

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)
by (695 points)
selected by

Much thanks :)

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 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 webmaster@godotengine.org with your username.