Marco Mulgado
Marco Mulgado

Reputation: 11

invalid get index '0' on base array

Iam doing a game in godot and i dont know why appears me that error, is just for save some costs in an array to next choose the most cheap.

The error is commented, is almoust in the bottom of the code. I did a little changes in the 2D screen and the error started to appears me

#Algoritmo de busqueda A*
extends KinematicBody2D
onready var position_pacman = get_parent().get_node("Pacman")
onready var paredes = get_parent().get_node("Paredes")

var pos_fantasma_rojo = Vector2() #Almacena la posicion xy del fantasma
var pos_player = Vector2() #Almacena la posicion xy del jugador 
var posible_celda_expansion = Vector2() #Casilla a comprobar expansiones
var hijo = Vector2() #Nodo que almacena una posicion de la expansion
var celdas = []
var comprobar_hijos = Vector2()
var primer_elemento = Vector2()
var lista_hijos = []
var celda_actual = []
var funcion
var timer 
var lista_costos = [] 
var costo_minimo 
var indice_costo_minimo 
var valor_heuristica
var zero = 0



func hijo_abajo():
    comprobar_hijos = Vector2(0,0)
    comprobar_hijos.x = ((posible_celda_expansion.x-4)/8)
    comprobar_hijos.y = ((posible_celda_expansion.y-4)/8)+1
    hijo = posible_celda_expansion
#   print("Comprobar hijos abajo:", comprobar_hijos)
    if comprobar_hijos in celdas:
        hijo.y += 8
        funcion = distancia_Manhattan(hijo) + heuristica(hijo)
        lista_costos.append(funcion)
        lista_hijos.append(hijo)


func evaluacion():
#   print("Costos:", lista_costos)
    costo_minimo = lista_costos[zero] #THERE IS THE PROBLEM! !!!!!!!!!!!!!
#   print("Costo minimo:", costo_minimo)
    for i in range(1, len(lista_costos)):
        if lista_costos[i] < costo_minimo:
            costo_minimo = lista_costos[i]
    indice_costo_minimo = lista_costos.find(costo_minimo, 0)
#   print("Indice costo minimo:", indice_costo_minimo)

func astar():
#   print("Celda actual:", celda_actual)
    if len(celda_actual) > 1:
        primer_elemento = celda_actual[indice_costo_minimo]
#       print("Primer elemento if:", primer_elemento)
    else:
        primer_elemento = celda_actual[0]
#       print("Primer elemento else:", primer_elemento)
    posible_celda_expansion = primer_elemento
#   print("Posible celda expansion:", posible_celda_expansion)
    hijo_arriba()
    hijo_izquierda()
    hijo_abajo()
    hijo_derecha()
    
    


I have to tried, change the '0' for a var with value 0 but it do not worked

Upvotes: 1

Views: 6481

Answers (1)

Theraot
Theraot

Reputation: 40295

The error:

invalid get index '0' on base array

Means that the array does not have a zero element. And since the arrays are zero based, that implies the array is empty.

So the problem is that you are not adding elements to your array before the code executes.

To fix it… Make sure that when the code executes the array is guaranteed to have at least one element.

You can, for example, check if the array has at least one element (using size or empty) and use that to divert the flow of execution around your code. Something like this:

if lista_costos.empty():
    # The array is empty, do what you do when the array is empty
else:
    # The array is not empty, do what you do when the array is not empty

Or if you prefer to exit early from the method, you can do this:

if lista_costos.empty():
    # The array is empty, do what you do when the array is empty
    return

# The array is not empty, do what you do when the array is not empty

Alternatively, make sure that method is only called when the code that populates the array has executed already.

Upvotes: 2

Related Questions