I'm trying to set up a sort of screen capture that lets me capture the game screen without any HUD overlays or menus.

It's a 2D game and when the user presses the 'capture photo' button, I want to write everything in the root viewport into an image and save it to disk. The problem I'm having right now is that HUD overlays and menus are being written as well. I'm thinkiing of maybe having the 2D game rendered to a subviewport and then the subviewport rendered to the screen, but am not sure this would work well with the Camera2D.

Is there a good way for me to capture just what the Camera2D sees of my game world and not include any of the overlays?

kitfox changed the title to How to screenshot 2D game without overlays and menus? .

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.