Cannot load texture from pck file created using PCKPacker

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Delin

I’m making a pck file and putting the default icon.png in it this way

var pack = new PCKPacker();
pack.PckStart("dlc.pck", 4);
pack.AddFile("res://qwop.png", "icon.png");
pack.Flush(true);
ProjectSettings.LoadResourcePack("dlc.pck");
var sprite = new Sprite();
sprite.Texture = (Texture)ResourceLoader.Load("res://qwop.png");
AddChild(sprite);

Problem is when I load the “qwop.png” texture to a sprite I get this error

_load: Resource file not found: res://qwop.png.
load: Error loading resource: 'res://qwop.png'.

What am I doing wrong?

:bust_in_silhouette: Reply From: SylvanSign

This was super confusing for me too :slight_smile:

Please read this thread: Clarify what an imported resource really is and how to load external files (png, jpg, ogg, wav...) · Issue #2148 · godotengine/godot-docs · GitHub

The TL;DR is that you need to treat “imported” and “not imported” files as separate things.

So the code you have looks fine until you call ResourceLoader.Load on the packed png and expect to get a Texture back. Instead, you would have to do the C# equivalent of this

var image = Image.new()
var texture = ImageTexture.new()
image.load('res://qwop.png')
texture.create_from_image(image)
sprite.texture = texture

This was one of the first and few topics I found on this so I figure I should add an answer here too - there’s a way to add images/sounds/etc via PCKPacker but it’s not straightforward, as noted here (and in my comment below).

The tl;dr is that you need to add .import files instead of the media files themselves, and then you need to add the dependencies of that .import file. You can use ConfigFile.load to read .import files and add the file paths listed in deps/dest_files via PCKPacker.add_file and then loading the resulting .pck file’s media resources should work.