Function in _physics_process() running twice

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

I have a simple timer and it runs twice on the process function.

var days = 0    
var dayTime = 0

func _physics_process(delta):

	dayTimer(delta)

func dayTimer(delta):
	
    dayTime += delta
	
	if dayTime > 3:
		
		days += 1
		print("day: " + str(days))
		weHaveChild()
		dayTime = 0

func weHaveChild():
	var partners = floor(adults/2)	
	for i in partners:
		var chance = randi()%100+1
		if chance <= 5:
			self.add_child(child_born.new())
			print("Nacio un pibe")
		print("hay " + str(partners) + " parejas")
		print("con chance de " + str(chance))

The dayTimer() function runs twice.
thanks

I’m not sure what you mean by “runs twice”. What do you expect this to do? Since the _physics_process() function is called once for each physics frame, your dayTimer() function will be called once per physics frame also…

jgodfrey | 2022-12-26 19:03

it should print

“day: 1
hay 1 parejas
con chance de 9
day: 2
hay 1 parejas
con chance de 9”

instead I get:

“day: 1
hay 1 parejas
con chance de 9
day: 1
hay 1 parejas
con chance de 35
day: 2
hay 1 parejas
con chance de 9
day: 2
hay 1 parejas
con chance de 83”

chantunRey | 2022-12-26 19:25

:bust_in_silhouette: Reply From: jgodfrey

Based on the above discussion…

I assume you must have (unintentionally) connected the script to 2 separate nodes. Running the above script on a single test node works as expected (only prints once for each day).

Thanks! The script was a singleton too!

chantunRey | 2022-12-26 20:56