Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | HarryCourt | |
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