Populate objects on a board - timer

Hi,

Completely new to game dev and defold. I use the Linker sample game as a basis.
What I try to achieve is to populate the board (starting with one object and adding another object every 2 seconds). Currently, thefunction update send a message and the function on_message trigger the function build_board. It works.

But I really have any clue how to do for the next time the function build_board will be called, it will create an object at the position [1,0].

  local function build_board()
	local board = {}
	math.randomseed(os.time())
	local pos = vmath.vector3()
	local x = 0
	local y = 0
	pos.x = edge + blocksize / 2 + blocksize * x
	board[x] = {}
	pos.y = top_edge - blocksize / 2 - blocksize * y
	local color = colors[math.random(#colors)]	-- Pick a random color
	local id = factory.create("#airplane_factory", pos, null, { color = color })
	board[x][y] = { id = id, x = x, y = y, color = color, type = type }
	print(id)
	return board
end

	
function update(self, dt)
	self.timer = self.timer + dt
	if self.timer >=2 then
		local added = 0
		msg.post("#", "reached2")
		self.timer = 0
	end
end

Thank you,
Vincent

I don’t see all of the relevant code, like the on_message function for instance, but here are some thoughts:

  • You should probably use timer.delay() instead of adding to a timer in the update function. You should as much as possible avoid running code every update. It’s better to use the native timer since it’s done in the engine in a much more performant way.
  • It almost looks as if you rebuilding the board every time build_board() is called. You should probably store the board on the self variable, as well as the current x & y position that you are supposed to populate.

Thank you for your help! I started over with the emthree Basic Project. I managed to make good progress but I’m stuck again :zipper_mouth_face:. What I try to do now is to modify the function M.collapse and the local function check_collapse to have another path during the collapse (the collapse happen when I delete an object).

My question is : Is it better to modify the actual code to arrive at a kind of mix between M.SLIDE_LEFT and M.SLIDE_UP with if conditions or is there a better way to do it ?

Actual code

function M.collapse(board, callback)
	assert(board, "You must provide a board")
	assert(callback, "You must provide a callback")
	local duration = board.config.collapse_duration

	local direction = DIRECTIONS[board.config.slide_direction]

	local collapsed = false
	local blocks = board.slots
	local empty_x = nil
	local empty_y = nil
	local block_size = board.block_size

	local function check_collapse(x, y)
		local block = blocks[x][y]
		if block then
			if empty_x and empty_y then
				--
				-- Move to empty slot
				--
				blocks[empty_x][empty_y] = block
				blocks[x][y] = nil
				block.x = empty_x
				block.y = empty_y
				--
				-- Calc new position and animate
				---
				local to = vmath.vector3((block_size / 2) + (block_size * empty_x), (block_size / 2) + (block_size * empty_y), 0)
				go.animate(block.id, "position", go.PLAYBACK_ONCE_FORWARD, to, board.config.slide_easing, duration)
				collapsed = true

				empty_x = empty_x - direction.x
				empty_y = empty_y - direction.y
			
			end
		-- first empty slot
		elseif not empty_x then
			empty_x = x
			empty_y = y
		end
	end

	if board.config.slide_direction == M.SLIDE_DOWN then
		for x = 0,board.width - 1 do
			empty_x = nil
			empty_y = nil
			for y = 0,board.height - 1 do
				check_collapse(x, y)
			end
		end
	elseif board.config.slide_direction == M.SLIDE_UP then
		for x = 0,board.width - 1 do
			empty_x = nil
			empty_y = nil
			for y = board.height-1,0,-1 do
				check_collapse(x, y)
			end
		end
	elseif board.config.slide_direction == M.SLIDE_LEFT then
		for y = 0,board.height - 1 do
			empty_x = nil
			empty_y = nil
			for x = 0, board.width-1 do
				check_collapse(x, y)
			end
		end
	elseif board.config.slide_direction == M.SLIDE_RIGHT then
		for y = 0,board.height - 1 do
			empty_x = nil
			empty_y = nil
			for x = board.width-1,0,-1 do
				check_collapse(x, y)
			end
		end
	end
	
	if collapsed then
		delay(duration, callback)
	else
		callback()
	end
end

Thank you!