Skip to content

Instantly share code, notes, and snippets.

@jotson
Last active March 15, 2024 14:40
Show Gist options
  • Save jotson/84681c2064653d093083a690e9fa5998 to your computer and use it in GitHub Desktop.
Save jotson/84681c2064653d093083a690e9fa5998 to your computer and use it in GitHub Desktop.
Godot GDScript screenshot function
# 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)
@officialBurtaxz
Copy link

Thank you, very useful!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment