Skip to content

Instantly share code, notes, and snippets.

@Shilo
Created November 7, 2024 09:02
Show Gist options
  • Save Shilo/c0274f2b4bcab51d0da514ccf9a476e5 to your computer and use it in GitHub Desktop.
Save Shilo/c0274f2b4bcab51d0da514ccf9a476e5 to your computer and use it in GitHub Desktop.
Godot 4 method to screenshot at resource path viewport with or without transparent background.
func screenshot_viewport(transparent_background: bool = false) -> void:
var time_string: String = Time.get_datetime_string_from_system().replace(":", "-").replace("T", "-")
var path := "res://screenshot-" + time_string + '-' + str(randi() % 8999 + 1000) + ".png"
var viewport := get_viewport()
if transparent_background:
viewport.transparent_bg = true;
await RenderingServer.frame_post_draw
var error: Error = viewport.get_texture().get_image().save_png(path)
if transparent_background:
viewport.transparent_bg = false;
if !error:
print("Screenshot saved: ", path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment