The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes
if speed == 100:
    print("hello world!")

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 (56 points)

3 Answers

+6 votes
Best answer

Use a Boolean variable

onready var only_once : bool = true

if speed == 100 && only_once:
    print("hello world!")
    only_once = false
by (56 points)
selected by
+1 vote

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

if speed == 100 and timerSwitch:
    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 (173 points)
0 votes

Your code must not be in a function with delta

func _process(delta):
   if speed == 100:
         print("hello world!")

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 Frequently asked questions and 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 [email protected] with your username.