Log In  


Cart #56636 | 2018-09-14 | Code ▽ | Embed ▽ | No License

So this is a followup to a thread I made a few days ago about having trouble parsing a table that has multiples of the same piece.
In this example I am trying to select either piece (which will be indicated by an arrow) but as you will see it only works for one.



Your problem is that every time you check or set p.selected you're referencing a global table called p which is only declared in the create_piece function, so it will always be the last piece you created.

If you only want to have one piece selected at a time you don't need to have an overlapped variable or a selected boolean for each piece, you could just have a global that references the currently selected piece.

Here's a short example of how you might do that for your select_piece function:

function try_select()
	for p in all(pieces) do	
		if hand.x >= p.x and hand.x <= p.x + 8 and hand.y >= p.y and hand.y <= p.y + 8 then
			selected = p
			return true
		end
	end
end

function select_piece()
	if btnp(5) then
		selected = nil
	end

	if btnp(4) then
		if not try_select() and selected then
			create_destination_box(hand.x - 6, hand.y - 2, 8)
		end
	end
end

You won't be able to cut and paste that directly into your code and have it magically work, but hopefully it'll give you an idea for how you could fix the issue your having.


Definitely helped me over the hump, thanks a ton.



[Please log in to post a comment]