Go, Go, Godot!
  • 0

A GDScript refactoring exercise

December 22, 2022

Arguably, more fun than writing code is removing code. I was assembling a split-screen multiplayer UI.

The goal behavior is to show/hide the appropriate displays for the players, depending on how many players there are.

Initially, the code to update the UI was very simple, because I started with two players. In that case, you can get away with just toggling the visibility of the second player’s display.

Once I added more scenarios, the code got lengthier. In order to support 0 – 4 players correctly, it ended up looking like this:

## Show the correct viewports based on player count
func _update_player_viewports():
    match num_players:
        0:
            %HBTop.visible = false
            %HBBottom.visible = false
        1:
            %HBTop.visible = true
            %HBTop/VPC1.visible = true
            %HBTop/VPC2.visible = false
            %HBBottom.visible = false
        2:
            %HBTop.visible = true
            %HBTop/VPC1.visible = true
            %HBTop/VPC2.visible = true
            %HBBottom.visible = false
        3:
            %HBTop.visible = true
            %HBTop/VPC1.visible = true
            %HBTop/VPC2.visible = true
            %HBBottom.visible = true
            %HBBottom/VPC1.visible = true
            %HBBottom/VPC2.visible = false
        4:
            %HBTop.visible = true
            %HBTop/VPC1.visible = true
            %HBTop/VPC2.visible = true
            %HBBottom.visible = true
            %HBBottom/VPC1.visible = true
            %HBBottom/VPC2.visible = true

If you’re familiar with the match statement syntax, this code is really quite straightforward. It’s a bit naïve verbose, but it is easy to follow and structured enough to be readable. But could it be shorter? The cases for three and four players look nearly identical.

Sometimes it can be risky to refactor something verbose for a bit more brevity. Some solutions might end up being “too clever”. I try to aim for clarity first unless there are specific performance demands.

Usually, it’s a matter of how to approach the problem. The code above very clearly divides up the use cases. If I’m dealing with three players, I know I need to look at the 3: block and that I can ignore the other blocks of code. It’s very light in terms of cognitive load.

This code snippet achieves that same behavior, but in only 8 lines of code instead of 30:

## Show the correct viewports based on player count
func _update_player_viewports():
    %HBTop.visible = num_players > 0
    %HBTop/VPC1.visible = num_players > 0
    %HBTop/VPC2.visible = num_players > 1
    %HBBottom.visible = num_players > 2
    %HBBottom/VPC1.visible = num_players > 2
    %HBBottom/VPC2.visible = num_players > 3

But is it as intuitive?

  1. The match statement is entirely gone.
  2. Each node is updated exactly once (but with a boolean expression instead of a boolean constant; arguably less declarative and needs computation by the reader).
  3. The order of nodes is specified so that the visibility is true until it’s false . Example for 2 players:
## Update the viewports to reflect the configured players
func _update_player_viewports():
    %HBTop.visible = num_players > 0
    %HBTop/VPC1.visible = num_players > 0
    %HBTop/VPC2.visible = num_players > 1
    %HBBottom.visible = num_players > 2
    %HBBottom/VPC1.visible = num_players > 2
    %HBBottom/VPC2.visible = num_players > 3

Well, it’s shorter anyway.

One final touch: I want to always show the first viewport, even when there are no players:

## Show the correct viewports based on player count
func _update_player_viewports():
    %HBTop.visible = num_players >= 0
    %HBTop/VPC1.visible = num_players >= 0
    %HBTop/VPC2.visible = num_players > 1
    %HBBottom.visible = num_players > 2
    %HBBottom/VPC1.visible = num_players > 2
    %HBBottom/VPC2.visible = num_players > 3
developer experienceobject-oriented Programmingprogramming
Posted in Godot.
Share
PreviousIs Godot is the Linux of Game Engines?
NextWhen not all strings are Strings. Detect bugs in your GDscript more easily with static typing

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Posts

  • October 24, 2024

    Inventory System v1.17 available

    A new version of the Inventory System is available. This release includes various refinements to existing systems to flesh out more combat-related functionality. Inventory Ammo Provider The GGAmmoProvider component is responsible for providing ammunition to equipped weapons. Previously, it only had a “simple” strategy: Creating ammunition out of thin air. The new GGAmmoProviderInventoryStrategy pulls ammunition …

  • January 15, 2025

    Inventory System 2 Alpha 1 available

    The first alpha of the inventory system v2 is now available. For v2, a lot of refactoring work has been done with a focus on improving the developer experience. As part of that, the naming convention of the addon classes was streamlined and is more consistent now. The GDScript documentation comments have been improved significantly …

  • August 2, 2022

    Launching Godot Games on Steam Deck from the command line via Remote Shell

    For some reason, my Godot game would no longer launch on the Steam Deck. I could hear it running as if it were running in the background, but the screen kept showing the Steam Deck’s UI. Since it’s all Linux I figured it should be possible to just drop to the command line and launch …

  • November 27, 2024

    Toggling Visibility of Nested CanvasLayers

    I had a setup with nested CanvasLayer nodes. Toggling the visibility of the root CanvasLayer doesn’t hide any nested CanvasLayer nodes. My solution was to listen to the visibility_changed signal, find any CanvasLayer child nodes, and apply the same visibility to them.

    © 2025 GoGoGodot.io. All rights reserved.