I am using a plain Node2D with a script attached for this in my game. It also puts a primitive timestamp on the file so you don't overwrite your last one. Here's the code I used:
extends Node2D
## this is a dev tool for screenshots because windows sucks.
var _endStamp = 0 ## an extra number at end of filename to prevent duplicates (error handling)
var _canScreenshot = false
func _listenForCommand():
if Input.is_action_just_pressed("_yourCameraInputHere") and _canScreenshot:
_endStamp+=1 ## increase error handling digit
var _timestamp = str(OS.get_ticks_usec())+str(OS.get_ticks_msec()) ## add number of usec and msec to string
var _fileTag = str("user://screenshot"+_timestamp+str(_endStamp)+".png") ## create filename from string and end error handling digit
var _screenshot = get_viewport().get_texture().get_data()
_screenshot.flip_y()
var _img = _screenshot.save_png(str(_fileTag)) ## create file with desired name
func _physics_process(_delta):
_listenForCommand()
You may also want to incorporate a bool logic piece (true/false) like I did above to detect if you can screenshot, that way you don't accidentally access/write to your memory while loading something else or another scene on accident. So, you can like flip the bool while the game is paused, then allow screenshots. However you want to set it up. Usually writing while reading is a recipe for shenanigans.