Code golf is a way of writing code to make it as short as possible. It’s completely impractical for making a game, but I think it’s a fun exercise for me to try at least.
So, let’s say we have a basic 2D platformer character controller script. I’d normally write it like this:
extends CharacterBody2D
@export var speed := 600
@export var gravity := 1_000
@export var jump_velocity = -700
func _physics_process(delta: float) -> void:
velocity.y += gravity * delta
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = jump_velocity
var h_input := Input.get_axis("left", "right")
velocity.x = h_input * speed
move_and_slide()
However, if we’re playing code golf, there’s a lot of characters that can be cut out without the player noticing. Let’s start by removing all the @export variables and replacing them with magic numbers:
extends CharacterBody2D
func _physics_process(delta: float) -> void:
velocity.y += 1000 * delta
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = -700
var h_input := Input.get_axis("left", "right")
velocity.x = h_input * 600
move_and_slide()
This may make the code harder to adjust, but it’s shorter now! Next, let’s remove all the type hinting; we don’t need it. And let’s rename all the variables and actions to something shorter.
extends CharacterBody2D
func _physics_process(d):
velocity.y += 1000 * d
if Input.is_action_just_pressed("j") and is_on_floor():
velocity.y = -700
var h = Input.get_axis("l", "r")
velocity.x = h * 600
move_and_slide()
We have so much wasted space on… actual space characters. Let’s get rid of them!
extends CharacterBody2D
func _physics_process(d):
velocity.y+=1000*d
if Input.is_action_just_pressed("j")&&is_on_floor():
velocity.y=-700
var h=Input.get_axis("l","r")
velocity.x=h*600
move_and_slide()
Next, let’s get rid of the creation of any new variables by putting all the velocity calculations on a single line with ternary if.
extends CharacterBody2D
func _physics_process(d):
velocity=Vector2(Input.get_axis("l","r")*600,-700 if Input.is_action_just_pressed("j")&&is_on_floor()else velocity.y+1000*d)
move_and_slide()
Man, the class of the node takes up so much space! Let’s avoid this by moving the script to a parent node of the CharacterBody2D that extends Node instead to save space! While we’re at it, let’s switch from _physics_process to _process since the player won’t notice. The player is named p to make the get_node as short as possible as well.
extends Node
func _process(d):
$p.velocity=Vector2(Input.get_axis("l","r")*600,-700 if Input.is_action_just_pressed("j")&&$p.is_on_floor()else$p.velocity.y+1000*d)
move_and_slide()
Finally, we can remove all the newline characters by replacing them with semicolons.
extends Node;func _process(d):$p.velocity=Vector2(Input.get_axis("l","r")*600,-700 if Input.is_action_just_pressed("j")&&$p.is_on_floor()else$p.velocity.y+1000*d);$p.move_and_slide()
The original code had 389 characters, while the final code only has 184! That’s a difference of 205 characters and a reduction of 53%!
Let me know if there are even more ways to reduce the character count! Input.is_action_just_pressed() is such a long function call ![]()