Ok so, in my project i need to load textures and sounds from files outside of the project folder.
If the picture is inside of the project folder, when I launch Godot it will create the .import file for that resource, and the load() function works properly
$TextureRect.texture=load(absolute_path), where absolute path is the one starting with C:/User/.... works fine and texture is loaded.
However, when the absolute path point outside of the project folder, meaning no .import file, load() fails and the texture is left as null.
So the question is: how do I load files into resources from script, without letting godot import them before?
I found out that load() works with Image type of variable, which is a different thing from Texture. So basically i have to load the image and then create the texture out of it:
var img = Image.new()
var tex = ImageTexture.new()
its a little bit twisted but it works flawlessy for any picture (imported or not).
I suppose there is an equivalent piece of code for the AudioStream but i couldnt find it out, any help is more then welcomed!
For the audio file, you basically have to open the file, extract the raw data, and give that raw data to the audiostream. The difficult part is that every kind of file has different way of storing bit data, so you need different code.
For the .ogg extension:
for the .wav extension:
var stream = AudioStreamSample.new()
stream.format = AudioStreamSample.FORMAT_16_BITS
stream.stereo = true
stream.data = snd_file.get_buffer(snd_file.get_len())
Format and stereo have to be the right value for the file you are attempting to open, because every typology stores the data flow in a slightly different way. If you try and open an audio file using the wrong setting, you will hear distorted sounds.
Currently there is no
FORMAT_24_BITS, which means that a whole category of wav sound cannot be reproduced with this method (if you play them with this code, you will only hear white sound).
Is it me or this way to load files is ridiculously overly complicated for such an easy access game engine?
Reading comments on the internet, it seems like in Godot 2 the system was much simpler (using
load() for everything) and then the devs willingly moved to this system in Godot3