func _draw(): to fire only once

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pferft

Hi everyone,

It appears that this prints every frame:

func _draw():
	if previous_text != text:
		print("Text changed: ", text)

Is that what func _draw(): does? I’d only need this to print once every time the text changes. Should I use some different function entirely or is there any way to interrupt this one?
( Putting

if previous_text == text:
	return

in front doesn’t work here…)

:bust_in_silhouette: Reply From: jgodfrey

Either of the code blocks you show should short circuit the function and prevent the contained code from executing until the text changes - but only if you UPDATE the value stored in the previous_text variable. You don’t show that happening, so I assume that’s the issue…

Something like this should work:

var previous_text = null

func _draw():
    if previous_text != text:
        previous_text = text
        print("Text changed: ", text)

Your solution works perfectly again.

I tried

if previous_text != text:
	previous_text = text
	print("Text changed: ", text)

with the var previous_text = ""

before, so just "" instead of null.

What a difference this makes, thank you very much!

pferft | 2022-04-12 16:20