Repository: godotengine/godot
File: scene/2d/tile_map_layer.cpp (C++)

Code before:
	// Cleanup existing scene.
	Node *node = nullptr;
	if (tile_map_node) {
		// Compatibility with TileMap.
		node = tile_map_node->get_node_or_null(r_cell_data.scene);
	} else {
		node = get_node_or_null(r_cell_data.scene);
	}
	if (node) {
		node->queue_free();
	}
	r_cell_data.scene = "";
}

void TileMapLayer::_scenes_update_cell(CellData &r_cell_data) {
	// Clear the scene in any case.
	_scenes_clear_cell(r_cell_data);

	// Create the scene.
	TileMapCell &c = r_cell_data.cell;

	TileSetSource *source;
	if (tile_set->has_source(c.source_id)) {
		source = *tile_set->get_source(c.source_id);

		int alternative_tile = c.alternative_tile; // Store the original alternative tile value.
		c.alternative_tile = TileSetAtlasSource::alternative_no_transform(alternative_tile); // Remove any transformation flags to get the original tile.
		if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
			TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
			if (scenes_collection_source) {
				Ref<PackedScene> packed_scene = scenes_collection_source->get_scene_tile_scene(c.alternative_tile);
				c.alternative_tile = alternative_tile; // Restore the original alternative tile value.
				if (packed_scene.is_valid()) {
					Node *scene = packed_scene->instantiate();
					Control *scene_as_control = Object::cast_to<Control>(scene);
					Node2D *scene_as_node2d = Object::cast_to<Node2D>(scene);
					if (scene_as_control) {
						scene_as_control->set_position(tile_set->map_to_local(r_cell_data.coords) + scene_as_control->get_position());
					} else if (scene_as_node2d) {
						Transform2D xform;
						xform.set_origin(tile_set->map_to_local(r_cell_data.coords));
						scene_as_node2d->set_transform(xform * scene_as_node2d->get_transform());
						_set_scene_transformed_alternative(c.alternative_tile, scene_as_node2d);
					}
					if (tile_map_node) {
						// Compatibility with TileMap.
						tile_map_node->add_child(scene);
					} else {
						add_child(scene);
					}
					r_cell_data.scene = scene->get_name();

Review comment (suggestion):
This is not the right way to implement it. If I understand correctly, this is just used to make sure `has_alternative_tile` returns `true` even if tiles are transformed. Instead, you should modify the implementation of `TileSetSceneCollectionSource::has_alternative_tile` so that it ignores the transfomation bits instead.

Code after:
	// Cleanup existing scene.
	Node *node = nullptr;
	if (tile_map_node) {
		// Compatibility with TileMap.
		node = tile_map_node->get_node_or_null(r_cell_data.scene);
	} else {
		node = get_node_or_null(r_cell_data.scene);
	}
	if (node) {
		node->queue_free();
	}
	r_cell_data.scene = "";
}

void TileMapLayer::_scenes_update_cell(CellData &r_cell_data) {
	// Clear the scene in any case.
	_scenes_clear_cell(r_cell_data);

	// Create the scene.
	const TileMapCell &c = r_cell_data.cell;

	TileSetSource *source;
	if (tile_set->has_source(c.source_id)) {
		source = *tile_set->get_source(c.source_id);

		if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
			TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
			if (scenes_collection_source) {
				Ref<PackedScene> packed_scene = scenes_collection_source->get_scene_tile_scene(c.alternative_tile);
				if (packed_scene.is_valid()) {
					Node *scene = packed_scene->instantiate();
					Control *scene_as_control = Object::cast_to<Control>(scene);
					Node2D *scene_as_node2d = Object::cast_to<Node2D>(scene);
					if (scene_as_control) {
						scene_as_control->set_position(tile_set->map_to_local(r_cell_data.coords) + scene_as_control->get_position());
					} else if (scene_as_node2d) {
						Transform2D xform;
						xform.set_origin(tile_set->map_to_local(r_cell_data.coords));
						scene_as_node2d->set_transform(xform * scene_as_node2d->get_transform());
						_set_scene_transformed_alternative(c.alternative_tile, scene_as_node2d);
					}
					if (tile_map_node) {
						// Compatibility with TileMap.
						tile_map_node->add_child(scene);
					} else {
						add_child(scene);
					}
					r_cell_data.scene = scene->get_name();
				}
			}
		}