Creating atlas via code

Input:

  • png file with all unts avatars (2 rows, 3 columns, each avatar has hex shape)
    undead_units
  • each hex has size of 160 x 160 px

Goals:

  • Player choose faction - like Undead or Human
  • I load textures from atlas and put these into specific cell of TileMapLayer (units selection panel)
  • Player can put selected unit on battlefield (another TileMapLayer)

My current idea:

  • Create script (some kind of Repository class) to load such textures via code on game starts
  • Don’t create additional scene resources = no creating TileSet / Atlas via Godot editor

Problem:

  • Is is good approach? If yes then how to finish it? I have stuck wich code below
  • If no then how to do it wisely?

Thank you!


var tile_size: Vector2i = Vector2i(160, 160)
const undead_atlas_path: String = "res://assets/units/undead_units.png"
var undead_tiles_set: TileSet
var undead_atlas_id : int
var atlas_source : TileSetAtlasSource

(...)

func initAtlasSource():
	
	atlas_source = TileSetAtlasSource.new()
	atlas_source.texture = preload(undead_atlas_path)
	atlas_source.texture_region_size = tile_size
	
	var size = atlas_source.get_atlas_grid_size()
	
	atlas_source.create_tile(Vector2i(0,0), tile_size)
	atlas_source.create_tile(Vector2i(0,1), tile_size)
	atlas_source.create_tile(Vector2i(0,2), tile_size)
	
	var tile_set = TileSet.new()
	undead_atlas_id = tile_set.add_source(atlas_source)
	tile_set.tile_shape = TileSet.TILE_SHAPE_HEXAGON
	tile_set.tile_size = tile_size
	
	undead_tiles_set = tile_set

(...)

This approach seems fine. Whats the problem with your current script?

1 Like

Seems that issues starts here:

atlas_source.create_tile(Vector2i(0,0), tile_size)
atlas_source.create_tile(Vector2i(0,1), tile_size)
atlas_source.create_tile(Vector2i(0,2), tile_size)

Error:

E 0:00:01:0157   units_repository.gd:37 @ initAtlasSource(): Cannot create tile. The tile is outside the texture or tiles are already present in the space the tile would cover.
  <Błąd C++>     Condition "!room_for_tile" is true.
  <Źródło C++>   scene/resources/2d/tile_set.cpp:4963 @ create_tile()
  <Ślad stosu>   units_repository.gd:37 @ initAtlasSource()
                 units_repository.gd:24 @ _ready()

I know the general cause of this problem and how to fix it via godot editor - but not by code…

apparently you try to create a tile outside of the image. It could be that the “size”-parameter is the amount of tilesizes it uses.
Can you try this?

	atlas_source.create_tile(Vector2i(0,0))
	atlas_source.create_tile(Vector2i(0,1))
	atlas_source.create_tile(Vector2i(0,2))

Ok found the solution for this:

  1. Needed to apply tiles size as units 1:1 (not pixels 160 x 160)
    e.g. atlas_source.create_tile(Vector2i(0, 0), Vector2i(1, 1))

  2. Needed to bind TileSet with existing TileMapLayer in controller script:
    tile_set = UnitsRepository.undead_tiles_set

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.