Jump to content

Storing all world tiles

Recommended Posts

I have this in my loop:

local tiles_refreshed
local function refresh_tiles()
	if tiles_refreshed then
	tiles = {}
	for x = 0, world_w do
		for y = 0, world_h do
			local tile = TheWorld.Map:GetTile(x, y)
			if tile ~= GROUND.IMPASSABLE and tile < GROUND.UNDERGROUND then
				if not tiles[tile] then
					tiles[tile] = {}
				table.insert(tiles[tile], { tile_x = x, tile_y = y })
	tiles_refreshed = true

I only call it when I need the tiles to save as much performance as I can, I also need to do this to get the number of each tile type in the world, and a random tile in the world of that type, so, for example, I can use this like so:

if tiles[GROUND.FOREST] > 10 then
  local idx = math.random(#tiles[GROUND.FOREST])
  local x, y = tiles[GROUND.FOREST][idx].tile_x, tiles[GROUND.FOREST][idx].tile_y

I need the number of tiles and a random tile for various reasons in my script.

So what I'm wondering is, if there's a way to optimize this, I was thinking to only call that at the start and then listen for a tile changed event (if there is one), is this possible?

Note: I am not fluent with DST API, Lua, DST Events (not creating them nor listening for them)

Link to comment
Share on other sites

The map has a SetTile(x, y, tile) function, but it's not part of the code we have access to. You can try extending it to push an event on all the players or whatever prefab you have that needs information about it, but I don't think it'll work.

I'm not sure how to solve this one.

Link to comment
Share on other sites

18 minutes ago, Zarklord said:

local _SetTile = Map.SetTile
function Map:SetTile(x, y, ground, ...)
	--do your proccessing to update your tile map here
	return _SetTile(self, x, y, ground, ...)


Yeah, that's what I suggested. Just wasn't sure we had access to extend it, since the original function isn't in the game files we have access to. Are you sure this works?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...