Topic was automatically imported from the old Question2Answer platform.
Asked By
gp1
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.
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.
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?
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].