Quickstart
Quickstart
This page shows how to install Ark, and gives a minimal usage example.
Finally, it points into possible directions to continue.
Installation
To use Ark in a Go project, run:
1go get github.com/mlange-42/ark
Usage example
Here is the classical Position/Velocity example that every ECS shows in the docs.
1package main
2
3import (
4 "math/rand/v2"
5
6 "github.com/mlange-42/ark/ecs"
7)
8
9// Position component
10type Position struct {
11 X float64
12 Y float64
13}
14
15// Velocity component
16type Velocity struct {
17 X float64
18 Y float64
19}
20
21func main() {
22 // Create a new World
23 world := ecs.NewWorld()
24
25 // Create a component mapper
26 // Save mappers permanently and re-use them for best performance
27 mapper := ecs.NewMap2[Position, Velocity](&world)
28
29 // Create entities
30 for range 1000 {
31 // Create a new Entity with components
32 _ = mapper.NewEntity(
33 &Position{X: rand.Float64() * 100, Y: rand.Float64() * 100},
34 &Velocity{X: rand.NormFloat64(), Y: rand.NormFloat64()},
35 )
36 }
37
38 // Create a filter
39 // Save filters permanently and re-use them for best performance
40 filter := ecs.NewFilter2[Position, Velocity](&world)
41
42 // Time loop
43 for range 5000 {
44 // Get a fresh query
45 query := filter.Query()
46 // Iterate it
47 for query.Next() {
48 // Component access through the Query
49 pos, vel := query.Get()
50 // Update component fields
51 pos.X += vel.X
52 pos.Y += vel.Y
53 }
54 }
55}
What’s next?
If you ask “What is ECS?”, take a look at the great ECS FAQ by Sander Mertens, the author of the Flecs ECS.
To learn how to use Ark, read the following chapters, browse the API documentation, or take a look at the examples in the GitHub repository.