|
|
|
|
Reply From: |
Bush2Tree |
SOLVED: I wrote an alternative tilemap
extends Node2D
export var tile_size_x = 32
export var tile_size_y = 32
export var tile_offset_x = 0
export var tile_offset_y = 0
export var paths_to_tiles = [""]
var tile_textures = []
var tiles = {}
func _ready():
for tile in paths_to_tiles:
tile_textures.append(load(tile))
func set_cell(x, y, index):
if(index == -1):
if(tiles.has(Vector2(x, y))):
tiles[Vector2(x, y)].queue_free()
elif(index >= 0):
var sprite = Sprite.new()
sprite.visible = false
tiles[Vector2(x, y)] = sprite
sprite.texture = tile_textures[index]
sprite.position.x = (x * tile_size_x) + tile_offset_x
sprite.position.y = (y * tile_size_y) + tile_offset_y
sprite.visible = true
add_child(sprite)
else:
get_tree().quit()
func set_cellm(x, y, index, col):
if(index == -1):
if(tiles.has(Vector2(x, y))):
tiles[Vector2(x, y)].queue_free()
elif(index >= 0):
var sprite = Sprite.new()
sprite.visible = false
tiles[Vector2(x, y)] = sprite
sprite.texture = tile_textures[index]
sprite.position.x = (x * tile_size_x) + (tile_size_x / 2) + tile_offset_x
sprite.position.y = (y * tile_size_y) + (tile_size_y / 2) + tile_offset_y
sprite.modulate = col
sprite.visible = true
add_child(sprite)
else:
get_tree().quit()
func set_cell_modulate(x, y, col):
if(tiles.has(Vector2(x, y))):
tiles[Vector2(x, y)].modulate = col
Due to being based off of instanced sprites, it most likely has worse performance than the built-in tilemap. It also obviously has far less features. However, it works for what I need to do.
It exports 5 variables. tile_size_x
and tile_size_y
are pretty self-explanatory. tile_offset_x
and tile_offset_y
will move each cell a certain distance. paths_to_tiles
is a list of strings, and functions as a crude TileSet
. Unfortunately, the editor interface for this variable is very clunky, so users must manually select string to be the type of every single entry, and users must manually enter every path.
set_cell
does the same thing as it does for the built-in TileMap
. set_cellm
is like set_cell
, but it also sets the modulate of the cell. set_cell_modulate
sets the modulate of a cell.
I really like the idea of your custom solution from a practicality standpoint. If it works for you, then more power to you.
Another approach would be to programmatically generate the tileset with the color + shape variations you’re looking for, which would save you the trouble of creating a bunch of sprite textures by hand.
normalocity | 2022-02-23 20:43
I also discovered that you can set the modulate of a tile in the tileset (rather than trying to override the modulate of a cell in the tilemap), which means you can use a small bit of code to change the tile in a cell, accomplishing your original goal while using the power of the TileMap to its fullest.
The other potential benefit to using a TileSet + TileMap is that you can do things such as change the modulate of a single tile, and that modulate will affect all cells referencing that tile.
YMMV, but I’ve been looking for a solution similar to the one you’ve outlined, and I have a feeling that programmatically constructing the TileSet to make all the combinations you want from a simple base texture might accomplish what you want.
The total number of combinations of all of these things can, of course, start to get large very quickly, but it’s something to think about.
normalocity | 2022-02-23 20:47
Another possibility is, rather than extend
from Node2D
, try extending TileMap
or TileSet
instead, adding the programmatic features you want while inheriting the other capabilities.
As you might have guessed, I came to your question while looking for a similar solution myself. I have a very simple prototype where I have 2 shapes, and 8 players (with a unique color per player). I plan to accomplish this by having ONLY TWO textures - a square and a circle. I will load those textures, then add them to a TileSet
once for every player (8x), setting the modulate
on each texture as I add it the TileSet
. If this works, then I’ll wind up with a 16-tile TileSet
, but I only have to maintain two base images and a little bit of code for all of this.
normalocity | 2022-02-23 20:52