Skip to content

Instantly share code, notes, and snippets.

@JEuler
Forked from jotson/screenshot.gd
Created August 15, 2023 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JEuler/7c3239cc71738ad3d8160572d2589f6d to your computer and use it in GitHub Desktop.
Save JEuler/7c3239cc71738ad3d8160572d2589f6d 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment