0 votes

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 onspawner_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 :)

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 
in Engine by (15 points)

1 Answer

0 votes
Best answer

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: http://docs.godotengine.org/en/stable/classes/class_timer.html#class-timer-set-wait-time

by (5,278 points)
selected by

@ 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 delaytime = 5 and _onspawnertimeout().setwaittime(delaytime)" inside
func onspawner_timeout( ): just freezes the system and inside func _process(): is of no use, can you please explain how to get this done

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 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 "onTimertimeout()" within "func _onspawnertimeout( ):" --> 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

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")

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.