How to use _process for a function only one delta ?

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

Hello,

I am trying a project but i am facing a problem, it could be a logical problem because i am a newbie and i never faced this kind of problem. TLTR: sorry if i am a newbie

my program have these condition.

_process(delta):
    function_to_run()

but i want to run it only once when a condition is met. Because i have plenty of conditions that is running to check if everything is met but i can not figure out this one.

i have tried this

_process(delta):
    some_condition == true:
        function_to_run()

function_to_run():
    if some_condition == true:
        run_content
        some_condition = false

But i know that the function never get activated. That’s one of the example i tried.

PLEASE. If you know how to process delta only ONE frame. Can you tell me please. I am stuck with this problem.

Best Regards

If you only want to run the function once, I’m not sure why you’d do it in _process. Can you not just run the function from _ready?

That said, you certainly can run a function only once from _process

jgodfrey | 2020-02-22 03:01

:bust_in_silhouette: Reply From: njamster

The code your posted is no valid gdscript, but at best pseudo-code describing your idea. Therefore it’s hardly possible to help you with your specific problem.

That said, this is what you probably want to do:

var some_condition = false

func _process(delta):
    if some_condition == true:
        function_to_run()

func function_to_run():
    # run content...
    some_condition = false

Here some_condition is a global variable, defaulting to false. The _process-function is run each time a frame is drawn and checks, if that variable is true. Which of course won’t happen, unless you set it to true somewhere else, e.g. in a signal-callback. But once you did that and the next frame is drawn, function_to_run will be executed and set some_condition back to false, so the next frame it won’t be executed again. Checking some_condition again in the beginning of function_to_run is not necessary for this example, but might be done anyways to ensure that the function cannot be called accidentally, i.e. while some_condition is false.

Hi there. I have a question about this code below.:

extends TextureButton

var a = preload("res://icon.png")
var b = preload("res://ubejdat.png")

var coll=[a,b]

func _ready():
	randomize()

func _process(d):
	cicl()
	yield(get_tree().create_timer(5),"timeout")
	set_process(false)
	
func cicl():
	for i in coll.size():
		texture_normal=(coll[randi()%coll.size()])

I want to randomly iterate the TextureButton object texture_normal images which included in array var named coll. But i don’t want it iterates infinitely in _process(d) func. I used “set_process(false)”… Can i use any another method to do same without set_process(false) or in static func to have the random iteration effect like in casino 777 games, and stop iteration after some seconds for example…? Thanks in advance…

leone | 2020-07-04 16:06