+1 vote

I am currently having issues with this code from the internet.

onready var reach = $Player/Pivot2/Camera/Reach
onready var hand = $Player/Hand    
onready var axemodel = preload('res://Scenes/Axe.tscn')

func _process(_delta):
        if reach.is_colliding():
            if reach.get_collider().get_name() == "Axe":
                weapon_to_spawn = axemodel.instance()
            else:
                weapon_to_spawn = null
        else:
            weapon_to_spawn = null


        if hand.get_child(0) != null:
            if hand.get_child(0).get_name() == "Axe":
                weapon_to_drop = axemodel.instance()
        else:
            weapon_to_drop = null
        if Input.is_action_just_pressed("TakeItem"):
            if weapon_to_spawn != null:
                if hand.get_child(null) != null:
                    get_parent().add_child(weapon_to_drop)
                    weapon_to_drop.global_transform = hand.global_transform
                    weapon_to_drop.dropped = true
                    hand.get_child(0).queue_free()
                reach.get_collider().queue_free()
                hand.add_child(weapon_to_spawn)
                weapon_to_spawn.rotation = hand.rotation

What this script is trying to do is search for nearby items that can be grabbed, and if you press E on them you will equip it. If you have any item in your hand you will drop it and equip a new one.

However this line right here throws an error:

if hand.get_child(0) != null:

error:

   E 0:00:05.648   get_child: Index p_index = 0 is out of bounds (data.children.size() = 0).
  <C++ Source>  scene/main/node.cpp:1296 @ get_child()
  <Stack Trace> World.gd:33 @ _process()

I can't get my head around this and i don't know how to fix it as it seems an engine error but what do i know. Sorry if this is a dumb question i'm new to Godot

in Engine by (13 points)

2 Answers

+1 vote

The error is generated when the child array doesn't contain any elements. You can't ask about the 1st element ([0]) if there are no elements.

Generally, the fix for that would be to first ensure that the array contains something prior to attempting to access the first element.

Here's one way to do that:

if child.size() > 0:
    # child array has at least one element, so you can safely access child[0] now
by (16,670 points)
edited by
+1 vote

This error happens when the child doesn't have any elements inside, so you should check if there is any element inside this child.
you could try this

if hand.get_child_count() > 0:

to check if there is a element inside the hand

by (16 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.