Invalid set index 'type' (on base: 'Node2D') with value of type 'int'

Godot Version

4.1.3

Question

How to fix this ERROR

Invalid set index ‘type’ (on base: ‘Node2D’) with value of type ‘int’.
In GUI.gd

extends Control
@onready var slot_scene = preload("res://slot.tscn")
@onready var board_grid = $ChessBoard/BoardGrid
@onready var piece_scene = preload("res://piece.tscn")
@onready var chess_board = $ChessBoard

var piece_type : int
var grid_array := []
var piece_array :=[]
var icon_offset := Vector2(39,39)
var fen = "rabqkbar/pppppppp/8/8/8/8/PPPPPPPP/RABQKBAR w KQkq - 0 1"
# Called when the node enters the scene tree for the first time.
func _ready():
	for i in range(64):
		create_slot()
		
	var colorbit =0
	for i in range(8):
		for j in range(8):
			if j%2 == colorbit:
				grid_array[i*8+j].set_backgound(Color.BISQUE)
		if colorbit==0:
			colorbit=1
		else: colorbit=0
		
	piece_array.resize(64)
	piece_array.fill(0)
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta : float)-> void:
	pass

 
func create_slot():
	var new_slot = slot_scene.instantiate()
	new_slot.slot_ID = grid_array.size()
	board_grid.add_child(new_slot)
	grid_array.push_back(new_slot)

func add_piece(piece_type, location)->void:
	var new_piece = piece_scene.instantiate()
	chess_board.add_child(new_piece)
	new_piece.type = piece_type
	new_piece.load_icon(piece_type)
	new_piece.global_position = grid_array[location].global_position + icon_offset
	piece_array[location] = new_piece
	new_piece.slot_ID = location
	
func parse_fen(fen : String)->void:
	var boardstate = fen.split(" ")
	var board_index := 0
	for i in boardstate[0]:
		if i == "/":continue
		if i.is_valid_int():
			board_index += i.to_int()
		else:
			add_piece(DataHandler.fen_dict[i], board_index)
			board_index +=1

func _on_test_button_pressed() ->void:
	parse_fen(fen)

In piece.gd

extends Node2D

var type : int
signal piece_selected(piece)
@onready var icon_path = $Icon
var slot_ID := -1

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass

func load_icon(piece_name) -> void:
	icon_path.texture = load(DataHandler.assets[piece_name])

in DataHandler

extends Node

var assete := []
enum PieceNames {BLUE_ACHER, BLUE_ASSASIN, BLUE_BISHOP, BLUE_JESTER, BLUE_KING, BLUE_LUCKIE, BLUE_MAGICIAN, BLUE_PAWN, BLUE_PHOENIX, BLUE_PIRATE, BLUE_QUEEN, BLUE_REAPER, BLUE_ROOK, BLUE_SPEAR, BLUE_SUMMONER,
				RED_ARCHER, RED_ASSASIN, RED_BISHOP, RED_JESTER, RED_KING, RED_LUCKIE, RED_MAGICIAN, RED_PAWN, RED_PHOENIX, RED_PIRATE, RED_QUEEN, RED_REAPER, RED_ROOK, RED_SPEAR, RED_SUMMONER}
var fen_dict := {"b" = PieceNames.BLUE_BISHOP, "B" = PieceNames.RED_BISHOP,
				 "r" = PieceNames.BLUE_ROOK, "R" = PieceNames.RED_ROOK,
				 "a" = PieceNames.BLUE_ASSASIN, "A" = PieceNames.RED_ASSASIN,
				 "q" = PieceNames.BLUE_QUEEN, "Q" = PieceNames.RED_QUEEN,
				 "k" = PieceNames.BLUE_KING, "K" = PieceNames.RED_KING,
				 "p" = PieceNames.BLUE_PAWN, "P" = PieceNames.RED_PAWN, }
# Called when the node enters the scene tree for the first time.
func _ready():
	assete.append("res://art_assets/Blue_Archer.png")
	assete.append("res://art_assets/Blue_Assasin.png")
	assete.append("res://art_assets/Blue_Bishop.png")
	assete.append("res://art_assets/Blue_Jester.png")
	assete.append("res://art_assets/Blue_King.png")
	assete.append("res://art_assets/Blue_Luckie.png")
	assete.append("res://art_assets/Blue_Magician.png")
	assete.append("res://art_assets/Blue_Pawn.png")
	assete.append("res://art_assets/Blue_Phoenix.png")
	assete.append("res://art_assets/Blue_Pirate.png")
	assete.append("res://art_assets/Blue_Queen.png")
	assete.append("res://art_assets/Blue_Reaper.png")
	assete.append("res://art_assets/Blue_Rook.png")
	assete.append("res://art_assets/Blue_Spear.png")
	assete.append("res://art_assets/Blue_Summoner.png")
	assete.append("res://art_assets/Red_Archer.png")
	assete.append("res://art_assets/Red_Assasin.png")
	assete.append("res://art_assets/Red_Bishop.png")
	assete.append("res://art_assets/Red_Jester.png")
	assete.append("res://art_assets/Red_King.png")
	assete.append("res://art_assets/Red_Luckie.png")
	assete.append("res://art_assets/Red_Magician.png")
	assete.append("res://art_assets/Red_Pawn.png")
	assete.append("res://art_assets/Red_Phoenix.png")
	assete.append("res://art_assets/Red_Pirate.png")
	assete.append("res://art_assets/Red_Queen.png")
	assete.append("res://art_assets/Red_Reaper.png")
	assete.append("res://art_assets/Red_Rook.png")
	assete.append("res://art_assets/Red_Spear.png")
	assete.append("res://art_assets/Red_Summoner.png")


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass

There must be more code that you are not showing, since that specific code by itself shouldn’t show an error. Please share the whole code to be able to help you, also it would be useful if you share it as text instead of an image.

Anyway, generally you should avoid using the word “type” as a variable name, since that is a word used when talking about datatype like TYPE_INT or TYPE_STRING and methods like typeof() etc.

How can I send all the code?

Just edit the original post and copy and paste it. Also it would be nice if you format it like code using preformatted text (Use the button with the </> icon)

I’ve shown all the relevant code.

You are probably getting that error because of the line new_piece.type = piece_type where you are asigning a value to a variable called type but the program can’t find that variable. Are you sure the res://piece.tscn scene has the piece.gd script attached?

If you keep unable to fix this, it would be really useful if you could share a minimal reproducible example of the project so I can take a deeper look.

How can I share examples of minimally repeatable projects?

By sharing a Godot project where we can reproduce the error by ourselves, so we can check exactly what is happening. Upload it to any cloud service, and share the link to download it here. You can also share your whole project if you don’t mind that and tell where the error is.

Ok fine i will share all code. Error in GUI.gd and piece.gd
https://drive.google.com/drive/folders/1lgkKukmkITKOqTmlno9MiQVW_W5XfDfO?usp=sharing

Indeed, as I thought the error is caused by the piece.tscn scene not having the piece.gd script attached.

image

Just add that the piece.gd script, and save it. That error will be gone.

image

1 Like

I have put piece.gd in piece.tscn but it gets another error.

That is due to the fact that you are asking for a variable inside DataHandler called assets but if you take a look inside DataHandler.gd you will see that the variable is wrongly named as assete instead of assets.

Just rename the variable assete inside DataHandler.gd to assets and that error will be solved too.

1 Like

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