Game freezing when loading a saved photo

Godot Version

4.2.2

Problem

I am trying to make a game where you take photos of the surroundings of the player, and they are saved. I was trying to test loading and displaying the photos that the player takes and displaying on the photos.

Every time that I take the photo, the game is meant to then load that photo and display it in a TextureRect on the screen, so that I can see if it works, but instead of displaying it, or coming up with an error, it instead just freezes.

In the drop down underneath is the code for the process to take the photos, and then load them to the UI for me to see.

Code
var Capture = get_viewport().get_texture().get_image()
var _time = Time.get_datetime_string_from_system()
_time = _time.replace(':', '-')
_time = _time.replace('T', '')
var filename = "user://Screenshot-{0}.jpg".format({"0":_time})
Capture.save_jpg(filename)
		
var image = Image.new()
image.load(filename)
var texture = ImageTexture.new()
texture.set_image(image)
get_node("TextureRect").texture = texture

Thank you in advance, I hope that I have provided enough information.

EDIT: I had the TextureRect set to KeepSize, so It was showing the image at the full size of the screen, so it appered to have frzeon, but instead it was just taking up the whole screen, I changed it to set the size of the photo to the bounds of the box and it works, thank you so much for your help, I am very greatful :slight_smile:

I ran your code and it works fine for me. Can you post how this code is called?

Why not use the image directly?

get_node("TextureRect").texture = get_viewport().get_texture().duplicate(true)

Do you know which part of the script the game freezes at? Is there a stack trace?

I didn’t want to use it directly because I also wanted the image to be saved to the system aswell, so that they can be viewed by the player when they’re in game, like a little photobook

You can use it directly and save it to disk, you would be preventing your code from writing and reading a jpeg file. Where does it freeze?

When the player holds down the right mouse button, the camera will zoom in, and if they press the left mouse button when the camera is zoomed in, it will take the photo.

func _camera_aim():
	if Input.is_action_pressed("aim_camera") and Camera.fov > 60:
		is_aimed = true
		Camera.fov = lerp(Camera.fov, 60.0, 0.1)
		if Camera.fov > 60 and Camera.fov < 61:
			Camera.fov = 60
	elif not Input.is_action_pressed("aim_camera") and Camera.fov < 90:
		is_aimed = false
		Camera.fov = lerp(Camera.fov, 90.0, 0.1)
		if Camera.fov < 90 and Camera.fov > 89:
			Camera.fov = 90
	
	if Input.is_action_just_pressed("take_photo") and is_aimed:
		var Capture = get_viewport().get_texture().get_image()
		var _time = Time.get_datetime_string_from_system()
		_time = _time.replace(':', '-')
		_time = _time.replace('T', '')
		var filename = "user://Screenshot-{0}.jpg".format({"0":_time})
		Capture.save_jpg(filename)
		
		var image = Image.new()
		image.load(filename)
		var texture = ImageTexture.new()
		texture.set_image(image)
		get_node("TextureRect").texture = texture

This is the entire function that handles the zooming in and out, and also handles the taking of the photo and displaying of it, the function is called every each time the _physics_proceses() function is called

you will need to confirm which one of these code actually made the freezing, is it on loading the image or saving?
are there any captured jpg on user file folder(save data)?

They already solved it, see the first post edit

didnt read that fully, thanks for telling. this post should be put to solved

1 Like