Declarative programming

2026/05/27

Type
Learning Resource
Format
Glossary Article
Version
General
Subject Tags
Code
MIT
Game Assets
2016-2026, GDQuest© - All rights reserved
All else
2016-2026, GDQuest© - All rights reserved
Created
2026/06/06
Updated
2026/05/27

Declarative programming

Declarative programming is a way of programming where you describe the result the code should produce without giving step-by-step instructions on how the computer should produce this result.

In Godot, creating scenes in the editor is a form of declarative programming, even though it is visual and done in the editor. A scene represents a node structure; you don't have to tell the engine exactly how to create each node or in which order to create them. Instead, the scene represents the final structure you want.

The following scene has four nodes: a CharacterBody2D node at the root, and child CollisionShape2D, Sprite2D, and Timer nodes. You don't have to write code to create these nodes, add them as children of one another, or set their properties. You just organize them and set their properties in the editor.

A scene with three nodes

Imagine that you've changed a few properties in the editor, like the radius of the CollisionShape2D node, the texture of the Sprite2D node, and the wait time of the Timer node.

When you run the scene, the scene structure reproduced by Godot may be equivalent to running this code:

var character_body := CharacterBody2D.new()

var collision_shape := CollisionShape2D.new()
collision_shape.shape = CircleShape2D.new()
collision_shape.shape.radius = 32.0

var sprite := Sprite2D.new()
sprite.texture = preload("res://character.png")

var timer := Timer.new()
timer.wait_time = 2.0
timer.one_shot = true

get_tree().root.add_child(character_body)
character_body.add_child(collision_shape)
character_body.add_child(sprite)
character_body.add_child(timer)

This code is imperative because it gives step-by-step instructions to create the nodes and add them as children of one another. When you use declarative code, there's always more code behind the scenes written in a different paradigm that makes it work.

In Godot, you can combine imperative and declarative programming styles. The GDScript programming language is mostly imperative and object-oriented, while the editor gives you tools that are like declarative programming.

You can also write declarative code in GDScript. There are built-in functions that describe a result rather than how to get it. For example, imagine you have an array of items in a player's inventory and you want to find all weapons. Here how you would filter and find them using a loop. This is imperative code:

enum ItemTypes { WEAPON, POTION }
var inventory := [
	{ "name": "Iron sword", "type": ItemTypes.WEAPON },
	{ "name": "Health potion", "type": ItemTypes.POTION },
	{ "name": "Wooden bow", "type": ItemTypes.WEAPON },
	{ "name": "Steel dagger", "type": ItemTypes.WEAPON },
]

var weapons := []
for item in inventory:
	if item.type == ItemTypes.WEAPON:
		weapons.append(item)

Here is the declarative equivalent using the Array.filter() function:

var weapons := inventory.filter(func(item): return item.type == ItemTypes.WEAPON)

With the imperative code, you tell the computer step by step how to build the result:

  1. Create an empty array
  2. Loop over each item
  3. Check a condition
  4. Append matching items

With the declarative code, you describe the result you want: "a filtered version of the inventory containing only weapons." The filter() function handles the looping and appending for you internally (the imperative code still exists behind the scenes, it is just hidden from you).

Some languages are designed to be more declarative than others. SQL and Prolog, for instance, are highly declarative languages where you almost always describe the result you want and let the language figure out how to compute it.

Become an Indie Gamedev with GDQuest!

Don't stop here. Step-by-step tutorials are fun but they only take you so far.

Try one of our proven study programs to become an independent Gamedev truly capable of realizing the games you’ve always wanted to make.

Nathan

Founder and teacher at GDQuest
  • Starter Kit
  • Learn Gamedev from Zero
Check out GDSchool

You're welcome in our little community

Get help from peers and pros on GDQuest's Discord server!

20,000 membersJoin Server

Contribute to GDQuest's Free Library

There are multiple ways you can join our effort to create free and open source gamedev resources that are accessible to everyone!

Site in BETA!found a bug?