परिचय
Godot 4 ने TileMap सिस्टम को पूरी तरह से फिर से लिखा है। अगर आपने Godot 3 में TileMap का उपयोग किया था, तो लगभग सब कुछ बदल गया है — TileSet editor, tiles कैसे परिभाषित होते हैं, auto-tiling, layers और GDScript API। यह गाइड नई architecture को शुरुआत से समझाती है।
Godot 3 उपयोगकर्ताओं के लिए: पुराने autotile, set_cellv() और cell_size सभी हट गए हैं। पेज के नीचे Godot 3 से माइग्रेशन सेक्शन देखें।
नई TileMap architecture
Godot 4 के TileMap सिस्टम के दो मुख्य घटक हैं:
- TileMap node — वह node जिसे आप अपने scene में जोड़ते हैं। यह एक या अधिक layers रखता है और एक TileSet resource को reference करता है।
- TileSet resource — tiles को स्वयं परिभाषित करता है। इसमें sources (atlas, scene collection आदि), physics layers, navigation layers, custom data layers और terrain sets शामिल होते हैं।
मुख्य बदलाव: Godot 3 में tile size, TileMap node पर सेट होता था। Godot 4 में tile size, TileSet resource में परिभाषित होता है, और TileMap बस उसे reference करता है।
TileSet sources
एक TileSet के कई sources हो सकते हैं, जिनमें से प्रत्येक को एक source_id (integer) द्वारा पहचाना जाता है। source के प्रकारों में शामिल हैं:
- TileSetAtlasSource — सबसे आम प्रकार। एक ही texture image जिसमें grid में व्यवस्थित कई tiles होते हैं।
- TileSetScenesCollectionSource — प्रत्येक tile एक packed scene होता है, जो animated या interactive tiles के लिए उपयोगी है।
एक TileSet सेट अप करना
चरण 1: TileSet resource बनाना
- अपने scene में एक TileMap node जोड़ें।
- Inspector में, Tile Set property पर क्लिक करें और New TileSet चुनें।
- Tile Size सेट करें (जैसे, 16x16 या 32x32)।
चरण 2: एक atlas source जोड़ना
- निचले panel में TileSet editor खोलने के लिए TileSet पर क्लिक करें।
- + बटन पर क्लिक करें और Atlas चुनें।
- अपनी tileset image को Texture property में ड्रैग करें।
- editor इसे आपकी tile size के आधार पर स्वतः tiles में विभाजित कर देता है। आप texture region और margins समायोजित कर सकते हैं।
चरण 3: tile properties कॉन्फ़िगर करना
TileSet editor में, आप विभिन्न layer प्रकार जोड़ सकते हैं जो सभी tiles पर लागू होते हैं:
- Physics Layers — tiles के लिए collision shapes
- Navigation Layers — pathfinding के लिए walkable polygons
- Custom Data Layers — कोई भी typed data (bool, int, float, String आदि)
- Terrain Sets — auto-tiling के लिए (Godot 3 autotile का प्रतिस्थापन)
TileMap layers
एक ही TileMap node में कई layers हो सकते हैं। यह Godot 3 की तुलना में एक बड़ा सुधार है, जहाँ आपको background और foreground के लिए अलग-अलग TileMap nodes की आवश्यकता होती थी। प्रत्येक layer के अपने z-index, y-sort और modulate settings हो सकते हैं।
आम layer विन्यास:
- Layer 0 — background (जमीन, फर्श) — z-index: -1
- Layer 1 — terrain (दीवारें, बाधाएँ) — z-index: 0
- Layer 2 — foreground (player के ऊपर सजावट) — z-index: 1
Layers के लिए GDScript API
# Set a cell on layer 0
tilemap.set_cell(0, Vector2i(5, 3), source_id, atlas_coords)
# Get cell info
var source = tilemap.get_cell_source_id(0, Vector2i(5, 3))
var coords = tilemap.get_cell_atlas_coords(0, Vector2i(5, 3))
# Erase a cell
tilemap.erase_cell(0, Vector2i(5, 3))
# Check if a cell is occupied
if tilemap.get_cell_source_id(0, Vector2i(5, 3)) != -1:
print("Cell has a tile")
API नोट: set_cell() में पहला parameter हमेशा layer index (0, 1, 2...) होता है। दूसरा Vector2i के रूप में cell position है। तीसरा source_id है, और चौथा Vector2i के रूप में atlas_coords है।
Terrain सिस्टम (auto-tiling)
Terrain सिस्टम Godot 3 के autotile को प्रतिस्थापित करता है। यह पड़ोसी tiles के आधार पर स्वतः सही tile variant का चयन करता है।
Terrains सेट अप करना
- TileSet में, एक Terrain Set जोड़ें (Inspector → Terrain Sets → Add Element)।
- terrain mode चुनें: Match Corners and Sides (47 tiles), Match Corners (16 tiles) या Match Sides (16 tiles)।
- set के भीतर terrain types जोड़ें (जैसे, "Grass", "Dirt", "Water")।
- TileSet editor में, Select mode पर स्विच करें, एक tile चुनें, और प्रत्येक tile पर terrain peering bits पेंट करें।
Terrains पेंट करना
एक बार terrains कॉन्फ़िगर हो जाने पर, TileMap editor पर स्विच करें और Terrains tab चुनें। अपना terrain type चुनें और सीधे map पर पेंट करें। Godot स्वतः सही tile variant का चयन कर लेगा।
# Set terrain programmatically
tilemap.set_cells_terrain_connect(0, [Vector2i(5, 3)], 0, 0)
# Parameters: layer, cells array, terrain_set, terrain index
Tiles पर physics
Tiles में collision जोड़ने के लिए:
- TileSet Inspector में एक Physics Layer जोड़ें।
- कॉन्फ़िगर करें कि यह physics layer कौन-सी collision layer और mask उपयोग करता है।
- TileSet editor में, Physics tab पर स्विच करें और प्रत्येक tile पर collision polygons बनाएँ।
टिप: आप विभिन्न tile प्रकारों के लिए कई physics layers रख सकते हैं। उदाहरण के लिए, दीवारों के लिए एक layer (movement को ब्लॉक करता है) और खतरों के लिए दूसरा (Area2D overlap के माध्यम से damage ट्रिगर करता है)।
Tiles पर custom data
Custom data layers आपको किसी भी tile से typed metadata जोड़ने देते हैं। यह gameplay logic के लिए अत्यंत उपयोगी है।
Custom data सेट अप करना
- TileSet Inspector में, एक Custom Data Layer जोड़ें।
-
इसे नाम दें (जैसे, "is_destructible") और type सेट करें (जैसे,
bool)। - TileSet editor में, Custom Data tab पर स्विच करें और प्रति tile values सेट करें।
कोड में custom data पढ़ना
var tile_data = tilemap.get_cell_tile_data(0, Vector2i(5, 3))
if tile_data:
var is_destructible = tile_data.get_custom_data("is_destructible")
var damage = tile_data.get_custom_data("damage")
if is_destructible:
destroy_tile(Vector2i(5, 3))
custom data के आम उदाहरण:
is_destructible: booldamage: intmovement_cost: floattile_type: String(जैसे, "water", "lava", "ice")spawn_chance: float
Procedural tile placement
नई API के साथ tiles को प्रोग्रामेटिक रूप से रखना सीधा है:
# Fill a rectangular area
for x in range(20):
for y in range(10):
tilemap.set_cell(0, Vector2i(x, y), 0, Vector2i(0, 0))
# Random tile placement
for x in range(50):
for y in range(50):
if randf() > 0.7:
tilemap.set_cell(0, Vector2i(x, y), 0, Vector2i(1, 0))
else:
tilemap.set_cell(0, Vector2i(x, y), 0, Vector2i(0, 0))
# Get all used cells on a layer
var used_cells: Array[Vector2i] = tilemap.get_used_cells(0)
print("Layer 0 has ", used_cells.size(), " tiles")
# Clear all tiles on a layer
tilemap.clear_layer(0)
# Clear everything
tilemap.clear()
World-to-Map coordinate रूपांतरण
# Convert world position to map coordinates
var map_pos: Vector2i = tilemap.local_to_map(world_position)
# Convert map coordinates to world position (center of tile)
var world_pos: Vector2 = tilemap.map_to_local(Vector2i(5, 3))
# Example: check what tile the player is standing on
var player_tile = tilemap.local_to_map(tilemap.to_local(player.global_position))
var tile_data = tilemap.get_cell_tile_data(0, player_tile)
if tile_data:
var tile_type = tile_data.get_custom_data("tile_type")
print("Player is on: ", tile_type)
Godot 3 से माइग्रेशन
| Godot 3 | Godot 4 |
|---|---|
TileMap.cell_size |
TileSet.tile_size |
set_cellv(pos, tile_id) |
set_cell(layer, pos, source_id, atlas_coords) |
get_cellv(pos) |
get_cell_source_id(layer, pos) |
autotile |
Terrain सिस्टम (Terrain Sets) |
| प्रति TileMap node एक layer | एक TileMap में कई layers |
TileMap.tile_set = preload(...) |
TileSet resource को Inspector में या कोड के माध्यम से असाइन किया जाता है |
world_to_map() |
local_to_map() |
map_to_world() |
map_to_local() |
आम गलतियाँ
-
पहले TileSet बनाना भूल जाना। जब तक आप एक TileSet resource असाइन नहीं करते, TileMap node tile editor नहीं दिखाएगा। Inspector में एक बनाएँ।
-
गलत source_id। अगर आपके पास केवल एक atlas source है, तो उसका
source_id0होता है। दूसरा source जोड़ने से उसेsource_id = 1मिलता है। TileSet editor के निचले panel की जाँच करें — प्रत्येक source tab अपना ID दिखाता है। -
atlas_coords को tile IDs के साथ भ्रमित करना। Godot 4 में सरल tile IDs नहीं होते। इसके बजाय, आप tiles को atlas grid में उनकी स्थिति के अनुसार
Vector2i(column, row)के रूप में reference करते हैं। -
layer parameter निर्दिष्ट न करना। प्रत्येक cell operation को पहले argument के रूप में एक layer index की आवश्यकता होती है। इसे भूलने से argument errors होती हैं।
-
set_cellv()का उपयोग करना। यह method अब मौजूद नहीं है। नई signature के साथset_cell()का उपयोग करें।