Normal rendering without game-window for AI training

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Skipperro

Hello,

I have a bit on unusual problem.
I’m using GODOT 4.0 for training AI using reinforcement learning on full game frames. To do this I’m sending “screenshot” of the game to my AI software every frame (using gRPC).

This is how the processing function looks like:

public override void _PhysicsProcess(double delta)
{
	// Do capturing only if AI is listening
	if (ChannelController.GetChannelsCount() > 0)
	{
		Task.Run(() =>
		{
			// Capture game frame as JPG
			var img = GetViewport().GetTexture().GetImage().SaveJpgToBuffer();
			var fd = new FrameData();
			// Convert JPG to ByteString for gRPC compatibility
			fd.VideoData = Google.Protobuf.ByteString.CopyFrom(img);
			// Send captured frame to AI
			GrpcMaster.SendFrameData(fd);
		});
	}
}

This function works fine and allows me to capture and process game frames with great performance. Only issue is - it works only when game window is visible.

The moment I minimize the window or switch to different workspace, game is still running, but update of viewport stops, so the result is, that I’m sending to the AI same frame over and over again.
If I restore the game window, everything goes back to normal.

So, my question:
Is it possible to force normal rendering and viewport updates even if game window is minimized?
Allowing it to run minimized, or even without window at all, would be a great help for my project.

:bust_in_silhouette: Reply From: Skipperro

Found an answer or at least workaround. I don’t know if this is the proper way of doing it, but this line of code inside _PhysicsProcess function make rendering happen on minimized game:

if (RenderingServer.HasChanged()) RenderingServer.ForceDraw();

If anyone knows better, more elegant solution, please let me know.