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 countfunc_update_player_viewports():match num_players:0:%HBTop.visible=false%HBBottom.visible=false1:%HBTop.visible=true%HBTop/VPC1.visible=true%HBTop/VPC2.visible=false%HBBottom.visible=false2:%HBTop.visible=true%HBTop/VPC1.visible=true%HBTop/VPC2.visible=true%HBBottom.visible=false3:%HBTop.visible=true%HBTop/VPC1.visible=true%HBTop/VPC2.visible=true%HBBottom.visible=true%HBBottom/VPC1.visible=true%HBBottom/VPC2.visible=false4:%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 countfunc_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?
The match statement is entirely gone.
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).
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 playersfunc_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 countfunc_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
Development snapshot #4 of Godot Engine 4.1 is here. Among many other changes, it fixes a lighting issue related to using Light-only mode in CanvasItemMaterial (#44559). Unfortunately, it also introduced a UX issue with gradient color pickers (#77745), which makes it quite difficult to work with gradients at all. If you use gradients, I recommend …
Need optimized export templates with PCK encryption support but don’t want to have to set up a build pipeline or download the entire compilation toolchain on your computer? Well, now it is. Fill out the form, checkout, and we’ll email you the download link after the compilation completes. Compilation may take 30 minutes to 3 …
The latest version of the inventory system is available and includes refinements for the interaction demo and the main tour. New features: We’ve also got a handful of bug fixes:
The latest version includes a few new enhancements, and an experiment: The sequencer demo uses inventory instances to hold music notes, which can be played back. This was inspired by music trackers that were popular in the 90s, such as Scream Tracker and Impulse Tracker. The sequencer isn’t meant to be a production-ready digital audio …
A GDScript refactoring exercise
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:
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:
But is it as intuitive?
true
until it’sfalse
. Example for 2 players:Well, it’s shorter anyway.
One final touch: I want to always show the first viewport, even when there are no players:
Related Posts
Godot Engine 4.1.dev4 is available
Development snapshot #4 of Godot Engine 4.1 is here. Among many other changes, it fixes a lighting issue related to using Light-only mode in CanvasItemMaterial (#44559). Unfortunately, it also introduced a UX issue with gradient color pickers (#77745), which makes it quite difficult to work with gradients at all. If you use gradients, I recommend …
Introducing GodotBuilder: Custom Export Templates built on demand
Need optimized export templates with PCK encryption support but don’t want to have to set up a build pipeline or download the entire compilation toolchain on your computer? Well, now it is. Fill out the form, checkout, and we’ll email you the download link after the compilation completes. Compilation may take 30 minutes to 3 …
Inventory System v1.5 available
The latest version of the inventory system is available and includes refinements for the interaction demo and the main tour. New features: We’ve also got a handful of bug fixes:
Inventory System v1.8 available
The latest version includes a few new enhancements, and an experiment: The sequencer demo uses inventory instances to hold music notes, which can be played back. This was inspired by music trackers that were popular in the 90s, such as Scream Tracker and Impulse Tracker. The sequencer isn’t meant to be a production-ready digital audio …