0 votes
if speed == 100:
    print("hello world!")
    $Timer.start()

I have this code which checks if the player's speed is 100 and if yes, it prints hello world! and starts a timer. But the script runs numerous times but I want it to print only once.

Godot version 3.3.2
in Engine by (55 points)

3 Answers

+2 votes
Best answer

Use a Boolean variable

onready var only_once : bool = true

if speed == 100 && only_once:
    print("hello world!")
    $Timer.start()
    only_once = false
by (48 points)
selected by
0 votes

Make sure your time is set on one shot and create a boolean

if speed == 100 and timerSwitch:
    timer.start()
    timerSwitch = false

I created variables called:

onready var timer = $Timer

var timerSwitch : bool = true

Hello world will still be called multiple times, depending on how much you set your timer. To perform it only once, my code example but without timers, only the boolean.

by (139 points)
0 votes

Your code must not be in a function with delta
Example:

func _process(delta):
   if speed == 100:
         print("hello world!")
         $Timer.start()

DO NOT USE delta

by (86 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 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.