How to uniquely reference tiles instead of using game objects?

Hi. I’m new to Defold and mostly new to game programming, but not new to programming in general. Ran into a problem and I’m not sure the best way to solve it.

I’m making a game that’s a modified version of roulette with new rules to spice things up. I’ve constructed a tilemap for the roulette board and it’s laid out in such a way that each tile is one place on the board where the player can place some chips. The idea is that the player simply clicks to place a chip down on a particular number.

The problem is that I can only refer to the tiles by their x/y coordinates on the board, which is just a raw number. I don’t see a good way of referring to them by a more unique name like street1 or single10. So, as it currently exists, I’d have to refer to each tile by raw numbers (e. g. tile number ‘50’). That just doesn’t seem like the right way to do this. The hope was to avoid making dozens individual game objects for each of the potential bets a player can place and carefully squishing them into the board. It seemed much easier to rely on a tilemap to lay things out, especially since the board doesn’t change during gameplay. But, it’s not clear to me how to do that.

Any help is appreciated.

One way is to use a lookup table, or more likely two lookup tables to convert to and from tile coordinates and names. Like so:

local nameToPos = {
	-- Just examples, wouldn't write these by hand.
	-- street1 = { x = 5, y = 3 },
	-- single10 = { x = 1, y = 2 },
}

local posToName = { -- A 2D array of names.
	-- [1] = { [2] = "single10" },
	-- [5] = { [3] = "street1" },
}

local function nameTile(name, x, y)
	nameToPos[name] = { x = x, y = y }
	posToName[x] = posToName[x] or {}
	posToName[x][y] = name
end

local function getTileName(x, y)
	local column = posToName[x]
	if column then
		return column[y]
	end
end

local function getTilePos(name)
	local pos = nameToPos[name]
	if pos then
		return pos.x, pos.y
	end
end

local function removeTileName(name) -- Remove based on name.
	local pos = nameToPos[name]
	if pos then
		nameToPos[name] = nil
		posToName[pos.x][pos.y] = nil
	end
end

local function unNameTile(x, y) -- Remove based on position.
	local name = getTileName(x, y)
	if name then
		posToName[x][y] = nil
		nameToPos[name] = nil
	end
end

(Untested off-the-top-of-my-head code. You would put it in a module for actual use.)

6 Likes

Thanks. That’s better than using raw numbers. I’ll give it a shot.

2 Likes