How to create atlas by using the script??

Godot Version

v4.2.2

Question

Hi. I’m making an isometric tile based game. I’ve seen many tutorials that how can I draw a tile on the TileMap by using the set_cell() function. But, in order to use set_cell(), we need an organized atlas system. The lots of tutorials suggest to drag the png file (which involves many tiles) and drop to the empty spot in the TileSet - Tile. Once I drop the png file into the empty spot, then godot automatically create tiles in the atlas, and allocate the source_id as well. Then I could easily use the set_cell() function. But, I want to do this process by the script code. How can I do this? So thank you for your advice.

I cant think of a reason why you would want to do this in script. What you can do is to create a Tileset beforehand and save it in your files and then load the Tileset you currently want to use.
If you want to do it in code anyway for some reason, you have to do something like this:

var tilemap = TileMap.new()
var tileset = TileSet.new()
var tileset_atlas = TileSetAtlasSource.new()
tileset_atlas.texture = your_texture_here
tileset.add_source(tileset_atlas)
tilemap.tile_set = tileset
add_child(tilemap)