node not disappearing when calling queue_free

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

I’m working on a game, and I’m trying to make a eating feature, I got the player’s part down for the most part, but the food’s script does not want to work it seems. I’ve tried so many different ways to make the food disappear once it’s substance has reached 0 or less. sorry if the code below is hard to read, this is my first time making a game and my first time posting a question. print “signal1” is the only one that doesn’t activate at any time, and there are no errors. the(top) main script is attached to the parent node of both the food and player scripts. The (mid)food script is attached to a kinematicbody2D, and the (bottom)player script is attached to another separate kinematicbody2D node.
Please let me know if there is a better way to do this

main

extends KinematicBody2D
func _on_FoodPH_eaten():
print("signal1")
food.visible = not get_node("Food").visible
food.queue_free()

food

extends KinematicBody2D

#variables for substance and freshness, freshness is unused for now.
signal eaten
export (int) var Substance = clamp(10,0,10)
export (int) var Freshness = clamp(10,0,10)
var velocity = Vector2.ZERO
export (int) var gravity = 4000
export (int) var speed = 1200
#function for eating, it lowers substance each bite until the food is eaten
func Eating():
	Substance -= 1
	print("eating food")
	if Substance <= 0:
		Eaten()
#function for removing the food once all the substance is gone.
#queue causes crash, find fix

func Eaten():
	emit_signal("eaten")
	print("food eaten")
	

#useless for now
func _ready():
	pass


func Eaten():
emit_signal("eaten")
print("food eaten")
`

player

extends KinematicBody2D

#signals for different actions to let other scenes/scripts/nodes react
#most unused for now
#WIP signals
signal Eat
#signal Hurt
#signal Roar
#signal Hide

#variables for a bunch of stuff, will organize later on.
var Food = load("res://Food/FoodPH.gd").new()
#var FoodSprite = load("").new()
var should_change_animation = true
var state_machine
var velocity = Vector2.ZERO
export (int) var Hunger = clamp (5,0,10)
export (int) var Limit = clamp (100,0,100)
export (int) var Health = clamp (10,0,10)
export (int) var speed = 1200
export (int) var jump_speed = -1800
export (int) var gravity = 4000

#starting function, sets position and activates movement
# also turns on idle animation
func start(pos):
	set_physics_process(true)
	position = pos
	#add starting animation stuff here
	#state_machine.travel("Idle")

#function for dying, works, animation is only a placeholder, ceases movement
#print is only for confirmation, can be removed if needed
func die():
	#add dying animation once made
	state_machine.travel("Death") 
	set_physics_process(false)
	print("you died")


func _ready():
	state_machine = $AnimationTree.get("parameters/playback")
# warning-ignore:return_value_discarded, find a fix soon
	$HungerCD.connect("timeout", self, "depleteHunger")
	#AnimationPlayer.start

func depleteHunger(): 
	Hunger -= 1
func get_input():
	velocity.x = 0
	
	if Input.is_action_pressed("Slide"):
		#if Input.is_action_pressed("right"):
		state_machine.travel("Slide")
			
	if Input.is_action_pressed("right"):
		velocity.x += speed
		#state_machine.travel("RightNorm")
	else:
		state_machine.travel("Idle")
		
	if Input.is_action_pressed("left"):
		velocity.x -= speed
		#state_machine.travel("LeftNorm")
	else:
		state_machine.travel("Idle")

func _physics_process(delta):
	get_input()
	#to check hunger/health value
	if Input.is_action_just_pressed("dev"):
		print("Hunger = ", Hunger, " Health = ", Health)
	#death funcion working SM
	if Hunger == 0:
		Health -= 1
		Hunger += 1
	if Health == 0:
		die()

		Hunger += 1
	velocity.y += gravity * delta
	velocity = move_and_slide(velocity, Vector2.UP)
	#if not is_on_floor():
		#state_machine.travel("JumpNorm")
	if Input.is_action_just_pressed("jump"):
			
		if is_on_floor():
			velocity.y = jump_speed
		
	if should_change_animation:
		should_change_animation = false
		state_machine.travel("Idle")

#eating mechanic WIP

			
		
func _input(event):
	if event.is_action_pressed("Bite"):
		for body in $Player.get_overlapping_areas():
			if body.is_in_group("Food"):
				#TestSingle.Eating()
					Food.Eating()
					emit_signal ("Eat")
func _on_Player_Eat():
	Hunger += 2
	print("yum!")

What prints out?

exuin | 2022-12-24 23:54

“eating food”+“yum!” are printed out each bite until the substance reaches 0 or less, then it prints out “eating food”+“food eaten”+“yum!” for each bite after that, and it never prints out “signal1”

feara | 2022-12-25 00:58

How are you connecting the signal?

exuin | 2022-12-25 05:32

I dont see any code to connect the signal in your “main” code above, if it doesnt connect the signal then it never looks out for it. Have you included all of the code? Also can you highlight the code and select the curly braces in the control buttons to make it viewable properly like this

func example():
    some_code_stuff as properly_formatted

Gluon | 2022-12-25 08:50

I connected the signal through the engine’s UI, and I’ve added the rest of the code and properly formatted it.
https://godotforums.org/assets/files/2022-12-25/1671942495-574545-thisone.png

feara | 2022-12-25 18:05

Fantastic news I am glad you managed to solve it!

Gluon | 2022-12-25 21:07

:bust_in_silhouette: Reply From: Gluon

Okay well I think the simplest explanation is still that the signal isnt connected properly but as a work around you could do the below as the food script to avoid the issue altogether.

export (int) var Substance = clamp(10,0,10)
export (int) var Freshness = clamp(10,0,10)
var velocity = Vector2.ZERO
export (int) var gravity = 4000
export (int) var speed = 1200
#function for eating, it lowers substance each bite until the food is eaten
func Eating():
    Substance -= 1
    print("eating food")
    if Substance <= 0:
        Despawn()

    
#just despawns itself rather than getting another node to do it.    
func Despawn():
	self.queue_free()

when I do this the game crashes once the substance has reached 0 or less, and it sends out the error “E 0:00:11.630 call_ptr: Attempted method call on a deleted object.
<C++ Source> core/variant_call.cpp:1222 @ call_ptr()
Player.gd:103 @ _input()”

feara | 2022-12-25 19:17

I think I managed to fix it! your answer helped quite a bit, it led me in the right direction. Thank you so much! (is there a way to mark this as solved?)

feara | 2022-12-25 19:34