Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Andrea |
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
E.g.:
$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?
EDIT:
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()
img.load(path)
tex.create_from_image(img)
$TextureRect.texture=tex
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!
EDIT 2:
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:
var snd_file=File.new()
snd_file.open(path, File.READ)
var stream=AudioStreamOGGVorbis.new()
stream.data=snd_file.get_buffer(snd_file.get_len())
$AudioStreamPlayer.stream=stream
snd_file.close()
for the .wav extension:
var snd_file=File.new()
snd_file.open(path, File.READ)
var stream = AudioStreamSample.new()
stream.format = AudioStreamSample.FORMAT_16_BITS
stream.stereo = true
stream.data = snd_file.get_buffer(snd_file.get_len())
$AudioStreamPlayer.stream=stream
snd_file.close()
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
Hahah - I still use Godot 2. While Godot 3 may have new and flashy features (and a much welcome re-working of the basic object syntax, eg. sprite.x, instead of sprite.getpos().x), some things are a bit unfinished, and also they completely ditched the older version of OpenGL the rendering was using, which means Godot 3 no longer works on some older video cards. For me right now, Godot 2 is the best choice, as Godot 3 doesn’t offer any extra features that are relevant to my game at the moment.
Domarius | 2018-11-04 00:49