[Solved] Factory created objects in the wrong position

Hello,

I am creating each letterbox as in the examples below through a factory, via factory.create.

It happens that sometimes (not always), objects are created in the wrong positions.

I activated the space key to redraw the objects with exactly the same parameters and positions and the second time this is done everything is in the correct position.

Example drawn in the wrong positions:

After deleting and redrawing with very same parameters:

Code to create each letterbox:

local function create_letterbox(letter, pos, scale)
	local component = "/leterbox_factory#letterbg_factory"
	local c = factory.create(component, pos)
	go.set_scale(scale, c)
	-- go.set_position(pos, c) -- tried this with same result
	msg.post(c, hashes.SET_LETTER, { letter = letter })
	return c 
end
function draw_board()

	for _, word_map in pairs(board_def.words_map) do
		for _, letter_map in ipairs(word_map) do

			local obj = letter_map.obj

			if board_def.objects[obj] == "" then
local pos = vmath.vector3(sizes.left, sizes.top, 0)
				local letterobj = create_letterbox(letter_map.letter, pos, sizes.letterbox_scale)
				letter_map.pos = {x = pos.x, y = pos.y}
				board_def.objects[obj] = letterobj;
			end	
		end	
	end	

code do clear all letterbox

local function clear_board()
	print("clear_board")
	for obj, object in pairs(board_def.objects) do
		go.delete(object)
		board_def.objects[obj] = ""
	end
end

Code to perform the tests.
Touch the screen calculates the positions and generates a new scheme.

Space deletes and recreates with the same parameters already calculated.

function on_input(self, action_id, action)
	if action_id == hashes.TOUCH and action.pressed then
		clear_board()
		create_board() -- Create call draw_board()
	elseif action_id == hashes.SPACE and action.pressed then
		clear_board()
		draw_board()
	end
end

I esspecially focus on this line. How do you define sizes variable and which code affects to it?

Sorry, I sent the wrong version of this function.

I tried to edit the post but I didn’t find this option.

Follow the correct draw_board function above.

The positions are already pre-calculated.

When draw the second time, use the same positions.
And this second time everything is in the correct place.

So you should focus on where you calculate the letermap.pos_x, pos_y.

1 Like

Correct draw_board function

function draw_board()

	for _, word_map in pairs(board_def.words_map) do
		for _, letter_map in ipairs(word_map) do

			local obj = letter_map.obj

			if board_def.objects[obj] == "" then	
				local pos = vmath.vector3(letter_map.pos_x, letter_map.pos_y, 0)
				local letterobj = create_letterbox(letter_map.letter, pos, sizes.letterbox_scale)
				board_def.objects[obj] = letterobj;
			end	
		end	
	end	
end	

The second time it is drawn, it uses the same parameters and is not calculated again.

If there was a problem in calculating the positions, wouldn’t it always have to be placed in the wrong place?

Do you use any animations on those objects?
You could print out the letter with its position to console for debug

I just found the problem.

I saw a message stating that the maximum number of collisions (64) had been reached, and the messages were no longer being processed.

I eliminated the object collision and the error no longer occurred.

Before I changed go.delete(object, true) to delete the letterbox recursively but it didn’t work.
Wouldn’t it have to exclude the collisions that are inside it?

Anyway, I don’t need these collisions at the moment.

I was running via vs code (defold kit), and I don’t remember seeing this message about collisions. I saw it when I run through the defold editor.

2 Likes