0

I am new to godot, and programming. I'm having trouble implementing a Timer node. I am writing a simple script to implement a zelda-like camera system. The camera system works as intended, however I would like to freeze the character (and eventually npc's) during the camera lerp. I was going to try to use a timer for this, and thats when I hit a snag!

extends Camera2D

var width: = 1024
var height: = 640
var x_offset = width/2
var y_offset = height/2

var new_position = Vector2()
var timer 

func _ready() -> void:
    global_position = Vector2(x_offset,y_offset)
    new_position = global_position
    timer = Timer.new()
    timer.connect("timeout", self, "_grid_transition_pause")
    timer.one_shot = true
    timer.wait_time = 1
    add_child(timer)


func _grid_transition_pause() -> void:
    print("success")

func _process(delta: float) -> void:

    var player_position = get_node("../Player").global_position

    var x = floor(player_position.x/width) * width + x_offset
    var y = floor(player_position.y/height) * height + y_offset
    new_position = Vector2(x,y)

    if new_position != global_position:
        global_position = lerp(global_position, new_position, 0.07)
        new_position = global_position
        print("position changed")
        timer.start()

    if Input.is_action_just_released("test_key"):
        print("test key")
        timer.start()

I added an Input Key to see if I could start the timer that way. And I could. One strange thing though: The Timer can be started with the input key only if the camera is in its original position. Once The camera has moved at all, the input key no longer works, even if returning to the original position.

Am I fundamentally misunderstanding how to use a timer node? Thanks for your time and helping a guy out!

CAGoss
  • 1
  • 1

1 Answers1

0

Your timer can only be started once because you set timer.one_shot = true. Remove this line of you want to start your timer multiple times.

rcorre
  • 6,477
  • 3
  • 28
  • 33