0 votes

i have

enum {

    shelf
    window
    hall
}


var state 

func _ready():
    randomize()


func _process(_delta):

    match state:

        shelf:
            var shelf = $hotovaskrinaotvorena
            print ("shelf")


        hall:
            var TSP = $MainHall/Teddyspawnpoint
            var spawnteddy = Teddy.instance()
            add_child_below_node(TSP, spawnteddy)
            print("hall")

        window:
            print ("window")
            pass


func choose(array):

    array.shuffle()
    return array.front()


func _on_Timer_timeout():
    state = choose([hall,shelf,window])

After this all it spawns my NPC but its spawning it till new is choosed or my game crushed is any solution for this? if is please respond

Godot version 3.2
in Engine by (15 points)
edited by

I'm not sure what you're trying to do. But, it looks like you have a timer (not sure what its interval is) that picks one of 3 elements of your array. If the selected element is hall, you add a new node to the scene in every frame. Yikes...

That can't be what you intend. What are you actually trying to do here?

what i wanna do is like you said addchild to be more accurate instance scene then add that scene as child bellow node but every as i made some tests it runs this
hall:
var TSP = $MainHall/Teddyspawnpoint
var spawnteddy = Teddy.instance()
add
childbelownode(TSP, spawnteddy)
print("hall")
but its adding new childs till new state is picked i tryed to remove return then game dont crushed but it also dont add child becouse when
func choose(array):

array.shuffle()
return array.front()

runs that return means( returns value that has been choosed ) and i dont know how to fix that or make some other function same as func Choode(array):

1 Answer

0 votes

Hmmm... Based on our above discussion, it sounds like you want to add a new child node each time your timer interval is reached. If that's the case, you don't want to add the new child node in _process() as that'll add one in every frame (as you've noticed). Instead, you want to add the node in the timer's timeout event. So, something like this (copying your existing match code)...

func _on_Timer_timeout():
    state = choose([hall,shelf,window])

    match state:
        shelf:
            var shelf = $hotovaskrinaotvorena
            print ("shelf")

        hall:
            var TSP = $MainHall/Teddyspawnpoint
            var spawnteddy = Teddy.instance()
            add_child_below_node(TSP, spawnteddy)
            print("hall")

        window:
            print ("window")
            pass

That way, a new node is only added when the timer counts down, not (potentially) in every frame...

by (10,856 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 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 webmaster@godotengine.org with your username.