Error: The class "position" couldn't be fully loaded (script error or cyclic dependency).

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

I’m working on a Minimax AI and I’ve been getting this error I have 3 scripts:
Board.gd:

extends Object
var minimax = Minimax.new()
var pos = position.new()
func _ready():
    minimax.minimax(pos, 3, -INF, INF, maximizingplayer)

Minimax.gd:

extends Object
class_name Minimax

var grid_height = 6
var grid_width = 7

func minimax(pos, depth, alpha, beta, maximizingplayer):
    if depth == 0 or pos.game_over:
	    return pos.score

    if maximizingplayer:
	    var maxeval = -INF
	    for child in pos.childs:
		    var eval = minimax(child, depth - 1, alpha, beta, false)
		    maxeval = max(maxeval, eval)
		    alpha = max(alpha, eval)
		    if beta <= alpha:
			    break
	    return maxeval

    else:
	    var mineval = INF
	    for child in pos.childs:
		    var eval = minimax(child, depth - 1, alpha, beta, false)
		    mineval = min(mineval, eval)
		    beta = min(alpha, eval)
		    if alpha <= beta:
			    break
	    return mineval

func _ready():
    pass

Position.gd:

extends Object

#2d array
class_name position

func array2d(width, height):
    var a = []

    for y in range(height):
	    a.append([])
	    a[y].resize(width)

	    for x in range(width):
		    a[y][x] = 0

    return a

var grid_width = 7
var grid_height = 6
var grid = array2d(7, 6)

var score:float
var gameover:bool
var Player:float
func piece_drop(collumn, player):
    var height = grid_height-1
    if grid[collumn][0] == 0:
	    for y in range(grid_height):
		    if grid[height-y][collumn] == 0:
			    grid[height-y][collumn] = player
			    break

func debug():
    #Printing out the grid
    for y in range(6):
	    print(grid[y])

func _ready():
    debug()
func get_posible_move(collumn, player):
    var height = grid_height-1
    var child = position.new()
    if grid[collumn][0] == 0:
	    for y in range(grid_height):
		    if grid[height-y][collumn] == 0:
			    child.piece_drop(collumn, player)

func position_childs(player):
    var childs = {}
    for x in range(grid_width):
	    childs[x] = get_posible_move(x, player)

func check_win(player):
    #horisontal check
    var width = grid_width-4
    var height = grid_height-4
    for y in range(grid_height):
	    for x in range(width):
		    var x1 = 0+x
		    var x2 = 1+x
		    var x3 = 2+x
		    var x4 = 3+x
		
		    var y1 = 0+y
		    var y2 = 0+y
		    var y3 = 0+y
		    var y4 = 0+y
		    if grid[y1][x1] == player and grid[y2][x2] == player and grid[y3][x3] == player and grid[y4][x4] == player:
			    gameover = true
			    return

func _init():
    position_childs(Player)
:bust_in_silhouette: Reply From: njamster

You cannot create a new instance of the position-class inside the position-class - that would result in an endless chain of dependencies.

So you have to restructure your code and get rid of this:

var child = position.new()