Call a function with Incremental time intervals

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By The_Life
:warning: Old Version Published before Godot 3 was released.

I am planning to show an avatar which appears at random places on screen for short time (ta) and disappears after ta, and this process happens at intervals of time (tb), for this, I used spawner and _on_spawner_timeout( ): where the Wait time is 3 sec where I change the position of avatar to random inside the timer, but this will happen always after 3 sec, I would like to make this wait time shorter after every time the function is called, couldn’t find any solution for this, it’ll be great if someone knows hoe to solve this - Thank you :slight_smile:

This is my code at present

extends Sprite

var _avatar = null
var current_pos = get_pos()
var current_pos2x = Vector2(current_pos).x
var current_pos2y = Vector2(current_pos).y
var screen = get_viewport_rect().size
var screen2 = OS.get_window_size()) 

func _ready():
    _avatar = get_node("../avatar")
    set_process(true)

func _process():
    print(current_pos)
     _on_spawner_timeout()
    
func _on_spawner_timeout( ):
	
	current_pos2x = rand_range(0,Vector2(screen2).x)
	current_pos2y = rand_range(0,Vector2(screen2).y)

	current_pos = Vector2(current_pos2x,current_pos2y)
	print(current_pos2x, " _ " ,current_pos2y)
	
	print(current_pos)
	print("spawner is on bro..!!")
	set_pos(current_pos)
	set_process(false)
	pass 
:bust_in_silhouette: Reply From: avencherus

Doing it this way, you could a reference to the Timer node in your scene, and then change the settings you need to change. Such as set_wait_time()

Timer class in the DOCs: Timer — Godot Engine (stable) documentation in English

@ avencherus Thank you for your time, I am new to Godot, tries as you suggested but couldn’t figure out how to connect it to the function, adding “var delay_time = 5 and _on_spawner_timeout().set_wait_time(delay_time)” inside
func _on_spawner_timeout( ): just freezes the system and inside func _process(): is of no use, can you please explain how to get this done

The_Life | 2017-06-24 12:29

You would need to get a reference to your timer in the scene tree. You didn’t mention where it was at, so the path you’ll have to resolve based on what you need.

It would look something like this.

onready var my_timer = get_node("Timer") # get node reference

var wait_time = 3.0
var reduction = 0.2

func _on_Timer_timeout():
	
	wait_time -= reduction
	
	if(wait_time >= 0.0):
		my_timer.set_wait_time(wait_time) # reference it here

avencherus | 2017-06-24 12:37

@ avencherus I tried as you suggested, where my structure is Nodes 2D ->(Sprite & Timer) hence the path of the timer is just Timer and added the code as suggested and also adding “_on_Timer_timeout()” within “func _on_spawner_timeout( ):” → where the error is
“Attempt to call function ‘set_wait_time’ in base ‘null instance’ on a null instance.” I think I am not getting your suggestion to complete extent and also I have added sprite to “timeout()” manually with the nodes feature in UI, can you please look into this

The_Life | 2017-06-24 12:54

A null instance error means you have nothing in that variable. The most likely reason is that your path in get_node() is not correctly pointing to the timer node.

If Timer is a child of Sprite, and those are their names, and they are children of the node containing this script, the path should be get_node("Sprite/Timer")

avencherus | 2017-06-24 13:59