why i get 1milion NPCs when calling return array.front()

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

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

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?

jgodfrey | 2021-01-06 20:09

what i wanna do is like you said add_child 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_child_below_node(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):

ouhellouder | 2021-01-07 07:45

:bust_in_silhouette: Reply From: jgodfrey

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…