← Back to 2D Structs
Map
⚠️ Experimental - Pre-Release
Map is a key-value dictionary that stores pairs of keys and values. Maps are created using Map<[KeyType: ValueType]> syntax.
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]>()Methods
Map Literal Syntax
Create a map with initial values using literal syntax:
var prices: Map<[string: decimal]> = <["price": 19.99], ["tax": 1.50]>
var scores: Map<[string: int]> = <["player1": 100], ["player2": 200]>new Map<[KeyType: ValueType]>() → Map
Creates a new empty map.
var scores = new Map<[string: int]>()var data = new Map<[string: string]>()Map.insert(key: KeyType, value: ValueType)
Inserts or updates a key-value pair in the map.
var scores = new Map<[string: int]>()
scores.insert("player1", 100)
scores.insert("player2", 200)
scores.insert("player1", 150) // Updates existing keyMap.get(key: KeyType) → ValueType?
Gets the value associated with the key. Returns null if the key doesn't exist.
var score = scores.get("player1")if score != null { Console.print("Score: " + score)}Map.remove(key: KeyType)
Removes the key-value pair with the specified key.
Map.contains(key: KeyType) → bool / Map.contains_key(key: KeyType) → bool
Returns true if the map contains the specified key.
if scores.contains("player1") { Console.print("Player1 exists")}Map.len() → int / Map.size() → int
Returns the number of key-value pairs in the map.
Map.clear()
Removes all key-value pairs from the map.
Usage Example
@script GameManager extends Node var player_scores = new Map<[string: int]>() fn init() { player_scores.insert("Alice", 100) player_scores.insert("Bob", 200) } fn update_score(player: string, points: int) { var current = player_scores.get(player) if current != null { player_scores.insert(player, current + points) } else { player_scores.insert(player, points) } }