So My code keeps breaking at a certain line and i cant figure out why

Godot Version

`func _process(delta):
if has_overlapping_bodies() and Input.is_action_just_pressed(“Left”):
var body = get_overlapping_bodies().find(Number)
var ToDestroy = node.get_child(body)
while not ToDestroy.is_in_group(“Left”):
Number += 1
ToDestroy = node.get_child(body)
ToDestroy.queue_free()
bf_act_1.animation = “Left”

Question

So Im trying to get my code to destroy an object and it is checking to make sure it is in the right group(THIS IS NECCESSARY) and it keeps breaking at the while loop that is readjusting it to the right place the and the Number Variable is in the code but idk why it is Breaking SOMEONE PLS HELP

I think I mentioned in another post of yours, don’t use the find function. Just use the array index syntax

get_overlapping_bodies()[Number]

It didnt work it gave me an error Invalid type in function ‘get_child’ in base Node(main.gd). Cannot convert arguement 1 from to int
The whole script:
extends Area2D
@onready var bf_act_1 = $“…/…/BF Act 1”
@onready var node = $“…/…”
var Number = 0

Called when the node enters the scene tree for the first time.

func _ready():
pass

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta):
if has_overlapping_bodies() and Input.is_action_just_pressed(“Left”):
var body = get_overlapping_bodies()[Number]
var ToDestroy = node.get_child(body)
while not ToDestroy.is_in_group(“Left”):
Number += 1
ToDestroy = node.get_child(body)
ToDestroy.queue_free()
bf_act_1.animation = “Left”

Don’t use get child the overlapp call gives you the node. It’s not needed.

Can you give me an example? cus im stupid and dont know what you are talking about cus im kind of new to this

I’ll give you a tip hold Ctrl key and click on the functions you are using. It will take you to the documentation of the functions.

When you use find(Number) it looks for a number in the array. But this array doesn’t have numbers it is an Array[Node2D]. Find I’ll not throw an error but will return -1. Get_child will accept a negative number. But it will give you a node at the end of the child list.

But again you don’t need to use get_child because you already have the node in the get_overlapping_bodies(), as it returns an array[node2d]

Oh my gosh thank you so much

I cant express how happy i am rn this is my 4th iteration trying to get something to work thank you so much

1 Like

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