Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Knep |
I need help
im making a puzzle game in GD script and have my code from a video that helped me create this although the video was made over 3 years ago and some of my code doesn’t work.
if im to comment out to see the next line that includes “img” gives me an error message saying “Invalid get index ‘0’ (on base: ‘Array’)”
Could someone please give me some help on how to fix this and if it’s because of my images name? because the name is numbers.
my entire code is:
extends Node2D
var map = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]]
var imgNode = load(“img.tscn”); var img =
var move = Vector2(0,0); var speed = 4 # 1,2,4
func _ready():
for n in range(16):
img.append(imgNode.instance()) ## <-------- is rooted and says attempt to call function ‘instance’ in base ‘nulll instance’ on a null instance
img[n].set_frame(n) ## <------ says Invalid get index '0' (on base: 'Array').
add_child(img[n])
for y in range(4):
if map[y].find(n) != -1:
img[n].set_pos(Vector2(map[y].find(n)*100,y*100))
img[15].set_hidden(true)
func step(key,dir):
if Input.is_key_pressed(key) and move == Vector2(0,0) or move == dir:
for y in range(4):
if map[y].find(15) != -1:
var pos = Vector2(y,map[y].find(15)) + dir
pos.x = min(max(pos.x,0),3); pos.y = min(max(pos.y,0),3)
img[map[y][map[y].find(15)]].set_pos(Vector2(pos.y100,pos.x100))
if img[map[pos.x][pos.y]].get_pos() != Vector2(map[y].find(15)100,y100):
move = dir
img[map[pos.x][pos.y]].set_pos(img[map[pos.x]].get_pos() - Vector2(dir.yspeed,dir.xspeed))
else: map[y][map[y].find(15)] = map[pos.x][pos.y]; map[pos.x][pos.y] = 15; move = Vector2(0,0)
break
func _fixed_process(delta):
step(KEY_DOWN,Vector2(-1,0));
step(KEY_RIGHT,Vector2(0,-1));
step(KEY_LEFT,Vector2(0,1));
step(KEY_UP,Vector2(1,0))