Last active
March 15, 2024 14:40
-
-
Save jotson/84681c2064653d093083a690e9fa5998 to your computer and use it in GitHub Desktop.
Godot GDScript screenshot function
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
# This is a function for capturing screenshots with GDScript | |
func screenshot(): | |
# Capture the screenshot | |
var size = OS.window_size | |
var image = get_viewport().get_texture().get_data() | |
# Setup path and screenshot filename | |
var date = OS.get_datetime() | |
var path = "user://screenshots" | |
var file_name = "screenshot-%d-%02d-%02dT%02d:%02d:%02d" % [date.year, date.month, date.day, date.hour, date.minute, date.second] | |
var dir = Directory.new() | |
if not dir.dir_exists(path): | |
dir.make_dir(path) | |
# Find a filename that isn't taken | |
var n = 1 | |
var file_path = path.plus_file(file_name) + ".png" | |
while(true): | |
if dir.file_exists(file_path): | |
file_path = path.plus_file(file_name) + "-" + str(n) + ".png" | |
n = n + 1 | |
else: | |
break | |
# Save the screenshot | |
image.flip_y() | |
image.resize(size.x, size.y, Image.INTERPOLATE_NEAREST) | |
image.save_png(file_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, very useful!!