Several Tilemaps in gameobject

I’m currently working on a Sokoban game. My game currently has a menu with buttons for each level. when i click the button it loads a proxy collection to load my “Level” gameobject , and when it returns “proxy_loaded” i send back a message what level to initialize.

my initialize function runs through the tilemap and creates the player and boxes gameobjects in tables and stores the walls and targets in tables according to the tiles.

this is the setup for the menu gui script:

local levels = { "Level01", "Level02"} -- id of level button
local levelSelected = nil

function on_message(self, message_id, message, sender)
	if message_id == hash("proxy_loaded") then
		-- New world is loaded. Init and enable it.
		msg.post(sender, "init")
		msg.post(sender, "enable")
		msg.post("main:/Level", "initialize", {level = levelSelected})
	end
end

function on_input(self, action_id, action)
	if action_id == hash("touch") and action.pressed then
		local button = nil
		for i, level in ipairs(levels) do
			button = gui.get_node(level)
			if gui.pick_node(button, action.x, action.y) then 
				levelSelected = level
				msg.post("#menu", "disable")
				msg.post("proxy#gameproxy", "load")
				return
			end
		end
	end
end

heres the Level script:

function on_message(self, message_id, message, sender)
	--load the level passed by proxy loaded message
	if message_id == hash("initialize") then
		initLevel(message.level)
	end
end

function initLevel(level)

	local tileMap = "Level#" .. level
	
	for i = 1, 10 do
		tiles[i] = {}
		for j = 1, 10 do
			-- create box objects
			if tilemap.get_tile(tileMap,"boxes",i,j) == ind_box_place then
				tilemap.set_tile(tileMap,"boxes",i,j,0) -- clear tile
				tilemap.set_tile(tileMap,"floor",i,j,ind_floor_spr) -- set tile to floor

				local p = vmath.vector3( (i - 1) * TILE_SIZE, (j - 1) * TILE_SIZE, 1)
				local component = "factory#boxfactory"
				local box = factory.create(component, p)
				table.insert(boxes, box)
			end	

			-- create player object
			if tilemap.get_tile(tileMap,"player",i,j) == ind_player_place then
				tilemap.set_tile(tileMap,"player",i,j,0) -- clear tile
				tilemap.set_tile(tileMap,"floor",i,j,ind_floor_spr) -- set tile to floot

				local p = vmath.vector3( (i - 1) * TILE_SIZE, (j - 1) * TILE_SIZE, 1)
				local component = "factory#playerfactory"
				local player = factory.create(component, p)
				-- table.insert(players, player)
				players = player
			end	

			-- get walls and targets of game
			if tilemap.get_tile(tileMap,"walls",i,j) == ind_wall then
				tiles[i][j] = "wall"
			elseif tilemap.get_tile(tileMap,"floor",i,j) == ind_target then
				tiles[i][j] = "target"
			else
				tiles[i][j] = "nope"
			end	
		end
	end

	boxesLeft = table.getn(boxes)
	label.set_text("Level#BoxesLeft", "Boxes Left: " .. boxesLeft)
end

the problem i am having is how how to add the tilmaps dynamicly?
i built everything this way so that i dont have to create a proxy for each level and separate game object and will only have to choose which tilemap to init the level? when i add another tilemap to the gameobject it just covers the first tilemap?

thanks!

If you want to hide or show components you can turn them on and off with “enable” and “disable” messages.

If you want to spawn and destroy different tilemaps dynamically, you’ll need to put them on a game object and use a factory, just like how you’re spawning your player and your box.

One way or the other you’ll have to have create data for each level. You can have a collection for each one and use a proxy or a collectionFactory for each, you can have game object files for each and a factory for each, or you can store them as text data (in a lua module or maybe JSON) and loop through that to set each tile.

2 Likes

Thanks! My main purpose was to have everything programmable so i wouldn’t have to add components by hand for every level.

I ended up using Tiled to export the data to a lua module and had to load it dynamically.

Here’s how I did it if any one looks for this in the future:

local tileMap = "Level#tm_level"
local path = '/main/level/levels/' .. level .. '.lua'
local code_str = sys.load_resource(path)
local lua_func = assert(loadstring(code_str))		
local levelData = lua_func()

reference for this solution:
dynamic load lua module

1 Like