Node keeps disappearing anytime a new game starts

Godot Version

4

Question

i was making a flappy bird clone and i finished the base game and i wanted to add a score counter that uses numbered images. it works at first, but when i enter a game over? and i try to restart the game by clicking the screen, the texture rect node that is a child of my main node disappears ive seen it disappear when i look at the remote tab of my scene. this also causes alot of errors. so sorry if my formatting is bad, this is my first question posted here

extends Node2D

var obstacle_pair = preload("res://scene/obstacle_pair.tscn")
var game_over_scene = preload("res://scene/game_over_state.tscn")
var screen_size
var score = 0
var container
var game_active 
var game_over_state
func _ready():
	screen_size = get_viewport_rect().size
	container = $ScoreCounter
	container.position.x = screen_size.x / 2
	new_game()
	
func _input(event):
	if game_over_state == false:
		if event is InputEventMouseButton:
			if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
				if game_active == false:
					begin_game()
				else:
					if $Plane.flying_state:
						$Plane.flap()
	if game_over_state == true:
		if event is InputEventMouseButton:
			if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
				new_game()
				container = $ScoreCounter
				add_child(container)
				get_tree().call_group('rocks','queue_free')
								
func _process(delta):
	if game_active == false:
		$ParallaxBackground.scroll_speed = 0
	if game_active == true:
		$ParallaxBackground.scroll_speed = 100
		#clean the current displayed score
		for number in container.get_children():
			number.queue_free()
		var score_as_string:String = str(score)
		for character in score_as_string: #this will parse the string from left to right
			var texture_rect:TextureRect = TextureRect.new()
			texture_rect.texture = load("res://assets/Tappy Plane/PNG/Numbers/number" + character + ".png")
			container.add_child(texture_rect)
	
	
	
func spawnObstaclePair():
	var rocks = obstacle_pair.instantiate()
	#spawn in a random location on the y axis
	var randomY = randf_range(150, 400)
	$ParallaxBackground/ParallaxLayer2.add_sibling(rocks)
	rocks.position = Vector2(screen_size.x + 40, randomY)
	rocks.hit.connect(bird_hit)
	rocks.scored.connect(points_scored)

	
	
func game_over():
	game_active = false
	game_over_state = true
	$Plane.flying_state = false
	$Plane.falling_state = true
	get_tree().call_group('rocks','stop_rock_movement')
	var game_over_screen = game_over_scene.instantiate()
	add_child(game_over_screen)
	
		
	
func new_game():
	container = $ScoreCounter
	container.position.x = screen_size.x / 2
	game_active = false
	game_over_state = false
	score = 0
	$Plane.reset()
	

func begin_game():
	game_active = true
	$Plane.flying_state = true
	$Plane.flap()
	
	
func _on_timer_timeout():
	spawnObstaclePair()

func points_scored():
	score += 1
	
func bird_hit():
	print('hit')
	$Timer.stop()
	game_over()

Let me preface this by clearly stating that it’s probably not the best idea to remove and add a bunch of textures every rendered frame.

If this is true, then something either removes the node from the tree or frees it. Both of which does not happen anywhere in the code you shared.

Then it’s usually a good idea to read those… What errors do you get?

1 Like

Attempt to call function ‘get_children’ in base ‘previously freed’ on a null instance.
this happens at the code

for number in container.get_children():

so uh… i don’t why i’m stupid, i found a weird icon on my score counter node and it turns out i added it to a group called rocks. :smiling_face_with_tear: my obstacle_pair scene actually has it’s own group called rocks so when i used queue free on the rocks group they all disappeared. so sorry and thanks for using your time to try and answer my question. i will close this now.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.