← Back to Documentation

Variables & Types

⚠️ Experimental - Pre-Release

Pup is a statically-typed language. Variables must be declared with a type, and the type system ensures type safety throughout your code.

Variable Declaration

Variables are declared using the var keyword followed by the variable name and type:

var health: int = 100var speed: float = 5.5var name: string = "Player"var is_alive: bool = true

You can also declare variables without an initial value, but you must specify the type:

var health: intvar position: Vector2var texture: Texture

Basic Types

int

Integer numbers (whole numbers).

var health: int = 100var score: int = 0var count: int = -5

float

Floating-point numbers (decimal numbers).

var speed: float = 5.5var rotation: float = 45.0var damage: float = 12.75

string

Text strings.

var name: string = "Player"var message: string = "Hello, World!"

bool

Boolean values (true or false).

var is_alive: bool = truevar is_visible: bool = false

Container Types

Array<Type>

Dynamic arrays that can hold multiple values of the same type.

var items: Array<string> = new Array<string>()var numbers: Array<int> = new Array<int>()var positions: Array<Vector2> = new Array<Vector2>()

See the Array documentation for methods.

Map<[KeyType: ValueType]>

Key-value dictionaries that store pairs of keys and values. Maps can be created with literal syntax or using the constructor.

// Literal syntaxvar prices: Map<[string: decimal]> = <["price": 19.99], ["tax": 1.50]> // Constructorvar scores: Map<[string: int]> = new Map<[string: int]>()

See the Map documentation for methods.

Engine Types

Pup has access to all engine types, including nodes, structs, and other engine objects:

// Node typesvar player: Sprite2D = new Sprite2D()var camera: Camera2D = new Camera2D()var area: Area2D = new Area2D() // Struct typesvar position: Vector2 = new Vector2(10.0, 20.0)var transform: Transform2D = new Transform2D()var color: Color = new Color(1.0, 0.0, 0.0, 1.0) // Engine typesvar texture: Texture = Texture.load("res://sprite.png")

See the Nodes and Structs documentation for available types.

Type Casting

You can cast values to different types using the as keyword. This is useful when working with DynNode or when you need to convert between compatible types:

// Cast DynNode to specific node typevar child: DynNode = self.get_node("Player")var player: Sprite2D = child as Sprite2D // Check type before castingif child.get_type() == NodeType.Sprite2D {    var sprite = child as Sprite2D    sprite.texture = Texture.load("res://sprite.png")}

Type Inference

When you provide an initial value, Pup can sometimes infer the type automatically. However, explicit type annotations are recommended for clarity:

// Explicit type (recommended)var health: int = 100 // Type can sometimes be inferred, but explicit is clearervar speed = 5.5  // Still recommended to write: var speed: float = 5.5

Related