0 votes

Hello,

using Godot 3.0.6, can I download an image from the server at runtime and then cut it to multiple pieces as per the project's needs?

Note:
basically, the image will not be exported with the package.
In other words, the project will have no prior knowledge about this image until the download moment.

Thank you in advance!

in Engine by (56 points)

1 Answer

+2 votes

You can use the HTTPRequest class to download an image and load it at run-time. See the snippet below for an example:

func _ready():
    # Create an HTTP request node and connect its completion signal
    var http_request = HTTPRequest.new()
    add_child(http_request)
    http_request.connect("request_completed", self, "_http_request_completed")

    # Perform the HTTP request. The URL below returns a PNG image as of writing.
    var http_error = http_request.request("https://via.placeholder.com/500")
    if http_error != OK:
        print("An error occurred in the HTTP request.")

# Called when the HTTP request is completed.
func _http_request_completed(result, response_code, headers, body):
    var image = Image.new()
    var image_error = image.load_png_from_buffer(body)
    if image_error != OK:
        print("An error occurred while trying to display the image.")

    var texture = ImageTexture.new()
    texture.create_from_image(image)

    # Assign to the child TextureRect node
    $TextureRect.texture = texture

The Image class also has load_jpg_from_buffer() and load_webp_from_buffer() to load JPEG and WebP images.

by (10,475 points)

Thank you.
Basically, we can use images that are not exported with the project but we will not have the benefits of the importation process as per the following link "Importing Images" until the ticket #17848 is completed "Cannot import any resource from any folder other than res://".
Is my understanding correct?

Actually, the ticket #17848 is better explained in the original ticket, #17748 "Expose load/import methods for use with file from user directory".

Hi Calinou,
Above example fine working,
when i try facebook user photo url to load in textureRect but in android devices blank(black) image set [In Ios and simulator properly work].

In android device file extension(.png,.jpg) url fine work, but without file extension url not set image it's blank image set.
In Android Work fine this url : http://docs.godotengine.org/en/stable/_static/docs_logo.png

how can i fix this one give me any help, Thank you.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.