0 votes

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.iskeypressed(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)]].setpos(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]].setpos(img[map[pos.x]].getpos() - 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 fixedprocess(delta):
step(KEYDOWN,Vector2(-1,0));
step(KEY
RIGHT,Vector2(0,-1));
step(KEYLEFT,Vector2(0,1));
step(KEY
UP,Vector2(1,0))

in Engine by (14 points)
edited by

1 Answer

0 votes

try something like this;

var imgNode = load("res://img.tscn")

load is need correct node path

by (69 points)

it's still rooting my img.append(imgNode.instance()) saying attempt to call function 'Instance' in base 'null instance' on a null instance.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.