0 votes

Currently I want to import all my image files at runtime, which i've successfully managed to store in an array.

However, I cannot seem to access ANY variable found in a singleton. I just get an error.

For example, I have a singleton pics_array.gd, here is the code for it:

extends Node2D


func _ready():
    var pics = []
    var a = 1
    var path = "path/to/imagesFolder"
    var dir = Directory.new()
    dir.open(path)
    dir.list_dir_begin()
    while true:
        var file_name = dir.get_next()

        if file_name == "":
            #break the while loop when get_next() returns ""
            break
        elif !file_name.begins_with(".") and !file_name.ends_with(".import"):
            #if !file_name.ends_with(".import"):
            pics.append(load(path + "/" + file_name))
    dir.list_dir_end()

In my Picture_display node I have the following code:

extends Sprite

func _ready():
    print(pics_array.a)

When I run this scene I get the error:
"Invalid get index "a"(on base: 'Node2D (pics_array.gd)').

This is really confusing me as I am following at least what I think the documentation is telling me. Also the Enable next to the singleton is checked. I even added the Pictureimport scene (which has the picsarray node in it) but i'm still not getting any variable access.

I should add that i've already searched online but I havn't found the answer i'm looking for yet.

in Engine by (57 points)

1 Answer

+1 vote
Best answer

So, a number of problems based on the code you posted...

First, the a variable in the pics_array.gd script has nothing to do with the pics array. That is, they're completely independent variables. I'm not sure why you'd expect to be able to access pics.a.

Second, the pics variable and the a variable are local to the _ready function in the global script. So, even if you were using them correctly, you couldn't access them from somewhere else.

Based on what you seem to be trying to do, you probably want something like:

In the singleton (at the top, outside of any function):

var pics = {"a": 1, "b": 2}

That's a Dictionary, which will let you access the value (1) by its key (a) using:

print(pics_array.pics.a)
by (13,810 points)
selected by

The second paragraph about local variables is exactly what I needed to know.

With regards to the accessing of pics_array.a if there is something I don't understand I tend to work with basic testing variables that arn't related to anything else in the code and then work from whatever success I can find. I know its basic but i'm still a novice and i'm not sure of a better way of testing things out just yet (I havn't got round to learning how to use most debuggers).

Thank you so much for the detailed and quick response, just managed to finish my first project in Godot based off your answer.

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 Frequently asked questions and 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 webmast[email protected] with your username.