Godot: Allow images from internet

Created on 7 Feb 2020  路  2Comments  路  Source: godotengine/godot

Godot version: Vulkan branch

OS/device including version: Linux, Arch, Manjaro

Issue description: I can't download images from the internet and display them in game at run time. I want to do this as I am integrating mod.io with Godot and want to display a thumbnail while is pulled from the internet to the user.

Steps to reproduce: run attached zip

Minimal reproduction project:
TextureUrl.zip

archived

Most helpful comment

You need to load the image and then convert it to a texture. The load method of ImageTexture class comes handy in this case and does that for you:

extends TextureRect
func _ready():
    load_from_url("https://thumb.mod.io/mods/9c16/18996/crop_320x180/d6pkf-zu0aekfvj_002.jpg", "temp.jpg");

func load_from_url(p_url, p_filename):
    var cache_path: String = "res://.cache/" + p_filename;

    var d = Directory.new()
    if !d.dir_exists(cache_path.get_base_dir()):
        d.make_dir(cache_path.get_base_dir())

    var request = HTTPRequest.new();
    request.download_file = cache_path;

    add_child(request);

    request.connect("request_completed", self, "on_request_completed", [cache_path]);
    request.request(p_url);


func on_request_completed(result, response_code, headers, body, cache_path):
    # var tex = load(cache_path) that only loads the image data from the disk
    # set_texture(tex); This did not work because `tex` is not a texture

    var img_texture = ImageTexture.new()
    img_texture.load(cache_path)

    set_texture(img_texture)

Also in GDScript you dont need to end the lines with ; :relaxed:

All 2 comments

You need to load the image and then convert it to a texture. The load method of ImageTexture class comes handy in this case and does that for you:

extends TextureRect
func _ready():
    load_from_url("https://thumb.mod.io/mods/9c16/18996/crop_320x180/d6pkf-zu0aekfvj_002.jpg", "temp.jpg");

func load_from_url(p_url, p_filename):
    var cache_path: String = "res://.cache/" + p_filename;

    var d = Directory.new()
    if !d.dir_exists(cache_path.get_base_dir()):
        d.make_dir(cache_path.get_base_dir())

    var request = HTTPRequest.new();
    request.download_file = cache_path;

    add_child(request);

    request.connect("request_completed", self, "on_request_completed", [cache_path]);
    request.request(p_url);


func on_request_completed(result, response_code, headers, body, cache_path):
    # var tex = load(cache_path) that only loads the image data from the disk
    # set_texture(tex); This did not work because `tex` is not a texture

    var img_texture = ImageTexture.new()
    img_texture.load(cache_path)

    set_texture(img_texture)

Also in GDScript you dont need to end the lines with ; :relaxed:

Thanks! I had to fiddle a bit due to working in the Vulkan branch so I've ended up with:

`
var image = Image.new();
image.load(cache_path);

var img_texture = ImageTexture.new();
img_texture.create_from_image(image);

set_texture(img_texture);

`

I do need to use ; so when I jumping between programming languages things keep compiling :)
I would use { and } in gdscript if I could to keep it close to C++!

Was this page helpful?
0 / 5 - 0 ratings