extends Node
@onready var captured_image: TextureRect = $CapturedImage
@onready var capture_button: Button = $CaptureButton
func _ready() -> void:
# Focus button for keyboard/gamepad-friendly navigation.
capture_button.grab_focus()
func _on_capture_button_pressed() -> void:
# Retrieve the captured image.
var img := get_viewport().get_texture().get_image()
# Create a texture for it.
var tex := ImageTexture.create_from_image(img)
img.save_png("user://screenshot.png")
OS.shell_open(ProjectSettings.globalize_path("user://screenshot.png"))
# Set the texture to the captured image node.
captured_image.set_texture(tex)
# Colorize the button with a random color, so you can see which button belongs to which capture.
capture_button.modulate = Color.from_hsv(randf(), randf_range(0.2, 0.8), 1.0)
The demo project does work for me in 4.3 and so does the Chore 4 demo project’s screenshot in 4.3 and 4.2 (the code I posted).
However inside my game the same code no longer works (my games use Chore as a framework). I can run the game in 4.2 and have screenshots, but running the game in any 4.3 gives blank PNGs, sometimes if I fiddle enough I will get a black PNG instead. Maybe it has to do with my game being more complex than these demo projects, or a change in how viewports are found, or something else, but something changed with 4.3 and I can’t figure out what.