SubViewport in Godot – Complete Guide

Welcome to an explorative journey through the realm of Godot 4, where today’s stop is the innovative SubViewport feature. This tutorial promises to guide you through the intricacies of the SubViewport class, a uniquely streamlined method of managing game worlds which adds depth and versatility to your development arsenal without the need for creating new windows or directly drawing to the screen. Whether you’re just starting or are well-versed in the Godot engine, ready your creative muscles to harness the power of isolated scene rendering for both aesthetic appeal and functional design in your games.

What Is SubViewport?

A SubViewport in Godot 4 is akin to a specialized camera in your game’s world that captures a particular scene or element without the need to draw directly onto the main screen. Think of it like having a mini-stage within your larger play, where you can orchestrate separate performances simultaneously. SubViewports are crucial for rendering different parts of your game, such as UI elements in 3D space or creating picture-in-picture effects, independent from the main viewport.

What Is It For?

SubViewports serve various purposes, greatly enhancing a developer’s toolset for creating immersive experiences. With the ability to isolate parts of a scene, they can be used for:

– Displaying multiple camera angles.
– Rendering 3D models within a 2D interface.
– Creating dynamic split-screen games.
– As a render target for post-processing effects.

Why Should I Learn It?

Understanding SubViewports opens up a new dimension of visual and interactive possibilities:

Increased Flexibility: SubViewports enable a modular approach to scene management, providing the flexibility to control how and when parts of a scene are rendered.
Performance Optimization: They can optimize performance by limiting rendering to only the visible or necessary parts of a game scene.
Creative Freedom: With SubViewports, your creative vision is not confined to the main game screen, which means you can craft intricate gameplay mechanics and visuals that stand out.

With this fundamental knowledge, you’re well on your way to transforming your game development skills and expanding your creative horizons. Let’s embark on this coding adventure and make your game worlds come alive with SubViewports.

CTA Small Image
FREE COURSES AT ZENVA
LEARN GAME DEVELOPMENT, PYTHON AND MORE
ACCESS FOR FREE
AVAILABLE FOR A LIMITED TIME ONLY

Developer API

The API for Godot version 4.2 is displayed below. For the latest version please visit the official documentation.

Properties

ClearModerender_target_clear_mode0
UpdateModerender_target_update_mode2
Vector2isizeVector2i(512, 512)
Vector2isize_2d_overrideVector2i(0, 0)
boolsize_2d_override_stretchfalse

Enumerations

enum ClearMode:

CLEAR_MODE_ALWAYS = 0 Always clear the render target before drawing.

CLEAR_MODE_NEVER = 1 Never clear the render target.

CLEAR_MODE_ONCE = 2 Clear the render target on the next frame, then switch to CLEAR_MODE_NEVER.


enum UpdateMode:

UPDATE_DISABLED = 0 Do not update the render target.

UPDATE_ONCE = 1 Update the render target once, then switch to UPDATE_DISABLED.

UPDATE_WHEN_VISIBLE = 2 Update the render target only when it is visible. This is the default value.

UPDATE_WHEN_PARENT_VISIBLE = 3 Update the render target only when its parent is visible.

UPDATE_ALWAYS = 4 Always update the render target.


Property Descriptions

ClearMode render_target_clear_mode = 0 The clear mode when the sub-viewport is used as a render target.


UpdateMode render_target_update_mode = 2 The update mode when the sub-viewport is used as a render target.


Vector2i size = Vector2i(512, 512) The width and height of the sub-viewport.


Vector2i size_2d_override = Vector2i(0, 0) The 2D size override of the sub-viewport.


bool size_2d_override_stretch = false If true, the 2D size override affects stretch as well.

 

Continuing Your Godot Journey

The realm of game development is vast and constantly evolving, with much to explore and create. If this introduction to SubViewports in Godot 4 has sparked your curiosity and you’re eager to delve deeper into game creation, we wholeheartedly encourage you to continue your learning journey. To aid you along this path, our Godot Game Development Mini-Degree is an excellent resource to expand your knowledge and skills in building cross-platform games.

In this comprehensive collection of courses, you’ll explore various game mechanics and genres, from RPGs and platformers to RTS and survival games. By creating a portfolio of real projects, you’ll solidify your understanding and gain hands-on experience that’s essential for tackling the challenges of game development with confidence. The flexibility of our courses ensures that regardless of your current level—from beginner to an experienced developer—you’ll find valuable content tailored to your needs.

For those who seek a broader selection, we invite you to check out our full range of Godot courses. We take pride in equipping learners with real-world skills through structured lessons featuring live coding, quizzes, and project-based learning. With Zenva, you’ll have the chance to not just play games but to create them, potentially opening doors to a vibrant career in the game development industry. Let the adventure continue, and may your passion for game creation grow ever stronger with each line of code you write.

Conclusion

As we reach the end of our adventure with SubViewports in Godot 4, we hope your mind brims with the possibilities that this powerful feature unlocks for your projects. Remember, what we’ve covered today is just the tip of the iceberg in the boundless sea of game development with Godot. Embrace these tools, experiment with them, and craft immersive experiences that leave players in awe.

Continue with us on your journey to mastery by diving into our Godot Game Development Mini-Degree. Let each course be a stepping stone towards realizing your dream games. With dedication and the right resources at your fingertips, there’s no limit to what you can create. Let your creativity soar, and transform your virtual worlds into reality with Zenva—your gateway to becoming a champion of game development.

Did you come across any errors in this tutorial? Please let us know by completing this form and we’ll look into it!

FREE COURSES
Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.