[4.6.2] Issue loading files outside godot or .pck

Godot Version

[4.6.2] 2D

Question

Hello again!
New conundrum: I need to access files within the executable’s folder. I have…kind-of.

Right now, the way I am doing it is a bit silly, but it is as follows:
var exePath = "/"

func _ready() -> void:
	var rebuild = str(OS.get_executable_path()).split("/")
	print(rebuild)
	rebuild.remove_at(len(rebuild)-1)
	rebuild.remove_at(0)
	print(rebuild)
	for ii in len(rebuild):
		exePath += rebuild[ii] + "/"
	print(exePath)

This worked perfectly for .txt files, so long as I simply append them accordingly. However, attempting to instead append a .png, .otf, or otherwise returns:

E 0:00:00:809   Maker.gd:114 @ Styles(): No loader found for resource: {PATH TRUNCATED} (expected type: unknown)
  <C++ Error>   Method/function failed. Returning: Ref<Resource>()
  <C++ Source>  core/io/resource_loader.cpp:358 @ _load()
  <Stack Trace> Maker.gd:114 @ Styles()
                Maker.gd:64 @ Manage()
                Maker.gd:23 @ _ready()

This is odd. I would further suspect load(), but if I am to instead pull from res://, in the editor (by simply replacing exePath with “res://”), it works just fine. The files are indeed in the appropriate file location, too.
The error message was pulled from a png, specifically for assignment to a button. I’ve put the Styles() function below accordingly, though the error still occurs outside the function.

Styles()
func Styles(type, loader, _margin):
	ActiveStyles.append(StyleBoxTexture.new())
	ActiveStyles[si].set_texture(load(exePath + loader))
	#commented code removed
	ActiveInstances[i].add_theme_stylebox_override(type, ActiveStyles[si])
	si += 1

Probably this is happening for you:

From @GDScript.load docs:

Note: Files have to be imported into the engine first to load them using this function. If you want to load Images at run-time, you may use Image.load(). If you want to import audio files, you can use the snippet described in AudioStreamMP3.data.

Unfortunate. I had attempted to use ResourceLoader.load and fill in the type hint, but that gave the sane error. Is it the same deal with ResourceLoader and FileAccess?

It’s not to use the ResourceLoader, as this have the same limitation as the @GDScript.load. You should use the examples mentioned in the docs.

For an image:

var img := Image.new()
var err := img.load(path_to_your_image)
if err == OK:
    # You image loaded successfully, do your stuff

For an a mp3 file:

func load_mp3(path):
    var file = FileAccess.open(path, FileAccess.READ)
    var sound = AudioStreamMP3.new()
    sound.data = file.get_buffer(file.get_length())
    return sound
1 Like

‘err’, or in the example, ‘b’, does not load the file.
Given:

var a := Image.new()
var b := a.load(exePath + loader)
if b == OK:
	print(a)
	print(b)

Output:
():<Image#-9223372009474357809>
0

Attempting to use a or b results in an error. The commands likely would not work out regardless due to how objects and memory is handled in my game.

Using b just results in the engine being mad at me for putting an int into a slot for images, while using a results in just nothing happening, likely because the image doesn’t have anything attached.