Created
November 7, 2024 09:02
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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