You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Issue description:
TileSetAtlasSource's create_tile() and texture codependency /ordering not explicit enough.
Proposal:
Update the documentation for TileSetAtlasSource.create_tile() and the texture property to clearly state that the atlas texture must be assigned/valid before calling create_tile(). This is necessary because the texture defines the atlas grid's dimensions, and failing to assign it first leads to undefined texture bounds and errors such as !room_for_tile.
For example, the docs should include a note like:
Note: Always call set_texture() with a valid texture before invoking create_tile(). Failure to do so will result in undefined texture bounds and may trigger a !room_for_tile error.
Simple Code Example (Correct Usage):
# Assume the following constants are defined:constTILE_SIZE: Vector2i=Vector2i(16, 16) # Each tile's size: 16x16constIMAGE_TEXTURE_SIZE: Vector2i=Vector2i(16, 16) # Size of the drawn image per tileconstTEXTURE_REGION_SIZE: Vector2i=Vector2i(16, 16) # Region size in the atlas (16x16)constMARGIN: Vector2i= (TEXTURE_REGION_SIZE-IMAGE_TEXTURE_SIZE) /2# Margin (0,0 in this case)constATLAS_MARGINS: Vector2i=Vector2i(0, 0) # No extra atlas marginsconstGRID_TILE_SIZE: Vector2i=Vector2i(1, 1) # One grid cell per tileconstATLAS_SEPARATION: Vector2i=Vector2i(0, 0) # No extra separation between tilesconstSOURCE_ID: int=1234# Unique ID for TileMap.set_cell()funccreate_and_save_tileset() ->TileSet:
vartile_colors= [Color.RED] # Single tile: red onlyvartileset: TileSet=TileSet.new() # Create a new TileSet resourcetileset.set_tile_size(TILE_SIZE) # Set tile size to 16x16varatlas_source: TileSetAtlasSource=TileSetAtlasSource.new() # Create an atlas sourceatlas_source.set_margins(ATLAS_MARGINS) # Set atlas margins (none)atlas_source.set_separation(ATLAS_SEPARATION) # Set tile separation (none)atlas_source.set_use_texture_padding(true)
atlas_source.set_texture_region_size(TEXTURE_REGION_SIZE)
# Create an atlas image sized for one tile (16x16)varatlas_texture_width=TEXTURE_REGION_SIZE.xvaratlas_texture_height=TEXTURE_REGION_SIZE.y*tile_colors.size()
varatlas_image: Image=Image.create_empty(atlas_texture_width, atlas_texture_height, false, Image.FORMAT_RGBA8)
forxinrange(IMAGE_TEXTURE_SIZE.x): # Loop over tile width (16 pixels)foryinrange(IMAGE_TEXTURE_SIZE.y): # Loop over tile height (16 pixels)atlas_image.set_pixel(x, y, tile_colors[0])
varfinal_texture: ImageTexture=ImageTexture.create_from_image(atlas_image)
# CORRECT: Assign valid texture it BEFORE creating tilesatlas_source.set_texture(final_texture)
# CORRECT: Now, create the tile definition safely with a valid textureatlas_source.create_tile(Vector2i(0, 0), GRID_TILE_SIZE)
tileset.add_source(atlas_source) # Add the atlas source to the TileSettileset.set_source_id(0, SOURCE_ID) # Set the unique source ID for later use in TileMap.set_cell()ResourceSaver.save(tileset, "res://Resources/TileSets/red_tileset.tres") # Save the TileSetreturntileset
Misleading/potential issue code:
funccreate_and_save_tileset_error() ->TileSet:
vartile_colors= [Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW]
vartileset: TileSet=TileSet.new()
tileset.set_tile_size(TILE_SIZE)
varatlas_source: TileSetAtlasSource=TileSetAtlasSource.new()
[...]
foriinrange(tile_colors.size()):
varcolor=tile_colors[i]
varx_offset: int=MARGIN.x+ATLAS_MARGINS.xvary_offset: int=TEXTURE_REGION_SIZE.y*i+MARGIN.y+ATLAS_MARGINS.yforxinrange(IMAGE_TEXTURE_SIZE.x):
foryinrange(IMAGE_TEXTURE_SIZE.y):
atlas_image.set_pixel(x_offset+x, y_offset+y, color)
varatlas_coords: Vector2i=Vector2i(0, i)
# ERRONOUS: Create the tile before assigning the texture (triggers "!room_for_tile" error)atlas_source.create_tile(atlas_coords, GRID_TILE_SIZE)
varfinal_texture: ImageTexture=ImageTexture.create_from_image(atlas_image)
# ERRONOUS: Assigning the texture to the atlas source was too late, and thus the tiles are created without a valid textureatlas_source.set_texture(final_texture)
tileset.add_source(atlas_source)
tileset.set_source_id(0, SOURCE_ID)
ResourceSaver.save(tileset, "res://Resources/TileSets/erroneous_tileset.tres")
returntileset
NOTE: this is my first attempt in participating in the godot engine repo's, so if possible I would also appreciate any sort of guidance on a potential code fix if that is more appropriate. For example, the error message that occurs from the out of order tileset property instantiation is very misleading because it arises in other areas unrelated to order of texture instantiation/tileset creation.
<C++ Error> Condition "!room_for_tile" is true. is not descriptive enough in the case of improper texture state when creating the tileset, so if possible a code refactor might be more effective here rather than just a documentation update
The text was updated successfully, but these errors were encountered:
Your Godot version:
4.4
Issue description:
TileSetAtlasSource's
create_tile()
andtexture
codependency /ordering not explicit enough.Proposal:
Update the documentation for
TileSetAtlasSource.create_tile()
and thetexture
property to clearly state that the atlas texture must be assigned/valid before callingcreate_tile()
. This is necessary because the texture defines the atlas grid's dimensions, and failing to assign it first leads to undefined texture bounds and errors such as!room_for_tile
.For example, the docs should include a note like:
Simple Code Example (Correct Usage):
Misleading/potential issue code:
original ticket regarding the solution and discovery of the issue godotengine/godot#98991 (comment)
URL to the documentation page (if already existing):
https://docs.godotengine.org/en/stable/classes/class_tilesetsource.html
NOTE: this is my first attempt in participating in the godot engine repo's, so if possible I would also appreciate any sort of guidance on a potential code fix if that is more appropriate. For example, the error message that occurs from the out of order tileset property instantiation is very misleading because it arises in other areas unrelated to order of texture instantiation/tileset creation.
<C++ Error> Condition "!room_for_tile" is true.
is not descriptive enough in the case of improper texture state when creating the tileset, so if possible a code refactor might be more effective here rather than just a documentation updateThe text was updated successfully, but these errors were encountered: