Skip to main content

Elixir

Core Language

The programming language of choice is Elixir. Many things in the engine category will relate to Elixir and feature Elixir code.

Why Elixir?

Skill Level Guide

Documentation pages indicate the required Elixir knowledge:

BadgeMeaning
BeginnerBasic syntax, simple functions
IntermediatePattern matching, modules, GenServer
AdvancedMacros, OTP supervision, metaprogramming

Essential Elixir Concepts

Before You Start

These concepts are fundamental to understanding the engine:

Pattern Matching

Used extensively for handling game state:

# Matching on function arguments
def handle_event(%{type: :combat, attacker: attacker, defender: defender}) do
calculate_damage(attacker, defender)
end

def handle_event(%{type: :trade, from: from, to: to, goods: goods}) do
transfer_goods(from, to, goods)
end

Pipe Operator

Perfect for data transformation pipelines:

entity
|> Entity.get_component(:economy)
|> calculate_income()
|> apply_taxes()
|> Entity.update_component(:economy)

Immutability

All game state transformations return new data:

# State is never mutated, always transformed
game_world_turn2 =
game_world_turn1
|> update_population()
|> update_resources()
|> update_diplomacy()
Performance & Structural Sharing

You might wonder if immutability kills performance. In practice, Elixir uses persistent data structures with structural sharing—instead of copying the entire data structure, unchanged parts are reused by reference. Only the modified portions are actually new. This makes immutable updates efficient for most use cases.

Learning Path

Quick Reference

Common Types in the Engine

@type entity_id :: integer()
@type component :: atom()
@type game_hour :: integer()
@type result :: {:ok, term()} | {:error, term()}

Frequently Used Functions

Entity Module - Engine.Entity

Entity.create(components)      # Create new entity
Entity.get(id) # Fetch entity by ID
Entity.update(id, changes) # Update entity
Entity.query(has: [:component]) # Find entities

More complex features will explain required Elixir concepts inline. For comprehensive learning, see the official Elixir guides.