Godot Version
4.3
Question
Can anyone help figure whats wrong i get an error nvalid assignment of property or key ‘position’ with value of type ‘Vector2’ on a base object of type ‘Nil’.”
extends Node2D
#grid Variables
@export var width : int;
@export var height : int;
@export var x_start: int;
@export var y_start: int;
@export var offset: int;
# the piece array
#preload in scenes into memory
var possible_pieces = [
preload("res://scenes/blue_piece.tscn"),
preload("res://scenes/green_piece.tscn"),
preload("res://scenes/pink_piece.tscn"),
preload("res://scenes/lightgreen_piece.tscn"),
preload("res://scenes/orange_piece.tscn"),
preload("res://scenes/yellow_piece.tscn"),
];
#the current pieces in the scene
var all_pieces = [];
#touch variables
var first_touch = Vector2(0,0);
var final_touch = Vector2(0,0);
var controlling = false;
# Called when the node enters the scene tree for the first time.
func _ready():
all_pieces = make_2d_array();
spawn_pieces();
func make_2d_array():
var array = [];
for i in width:
array.append([])
for j in height:
array[i].append(null)
return array;
func spawn_pieces():
for i in width:
for j in height:
var rand = randi_range(0, possible_pieces.size()-1);
var piece = possible_pieces[rand].instantiate();
add_child(piece);
piece.set_position(grid_to_pixel(i, j));
func pixel_to_grid(pixel_x, pixel_y):
var new_x = round((pixel_x - x_start) / offset);
var new_y = round((pixel_y - y_start) / -offset);
return Vector2(new_x, new_y);
func grid_to_pixel(column,row):
var new_x = x_start + offset * column; #y=mx + b
var new_y = y_start + -offset * row;
return Vector2(new_x, new_y)
func is_in_grid(column,row):
if column >= 0 && column < width:
if row >=0 && row < height:
return true;
return false;
func touch_input():
if Input.is_action_just_pressed("ui_touch"):
first_touch = get_global_mouse_position();
var grid_position = pixel_to_grid(first_touch.x, first_touch.y);
if is_in_grid(grid_position.x, grid_position.y):
controlling = true
else:
controlling = false ;
print(grid_position);
if Input.is_action_just_released("ui_touch"):
final_touch = get_global_mouse_position();
var grid_position = pixel_to_grid(final_touch.x,final_touch.y);
if is_in_grid(grid_position.x,grid_position.y) && controlling:
touch_difference(pixel_to_grid(first_touch.x, first_touch.y), grid_position);
func swap_pieces(column,row,direction):
var first_piece = all_pieces[column][row]
var other_piece = all_pieces[column +direction.x][row + direction.y];
all_pieces[column][row] = other_piece;
all_pieces[column + direction.x][row + direction.y] = first_piece;
first_piece.position = grid_to_pixel(column + direction.x,row + direction.y)
other_piece.position = grid_to_pixel(column, row);
func touch_difference(grid_1, grid_2):
var difference = grid_2 - grid_1;
if abs(difference.x) > abs(difference.y):
if difference.x > 0:
swap_pieces(grid_1.x,grid_1.y, Vector2(1,0));
elif difference.x < 0:
swap_pieces(grid_1.x,grid_1.y, Vector2(-1,0));
elif abs(difference.y) > abs(difference.x):
if difference.y > 0:
swap_pieces(grid_1.x,grid_1.y, Vector2(0,1))
elif difference.y < 0:
swap_pieces(grid_1.x,grid_1.y, Vector2(0,-1))
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
touch_input();