Logic only affected last index in table

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
AlexCalv
Prole
Posts: 49
Joined: Fri Aug 08, 2014 7:12 pm
Contact:

Logic only affected last index in table

Post by AlexCalv »

Have this button table here and I need to solve a bug where you can click through the button to whatever is behind it. I've tried to solve this by adding a variable 'LET_BUTTON_SELECTED' that will be true if any button is highlighted. However, this variable only turns true if the last created and enabled button is highlighted. This demonstrates what I'm talking about:
https://love2d.org/imgmirrur/Q059AYe.mp4

And here's the code that I have so far. This code all works fine with highlighting and such other than this one issue I'm having.

Code: Select all

button = {}
function button.spawn(quad, action, activeState, x, y, w, h)
	table.insert(button, {id = #button + 1, type = "button", action = action, activeState = activeState, enabled = enabled, quad = quad, quad_overlay = nil, x = x, y = y, width = w or 194, height = h or 49, highlight = false})
	--Center our button according to our width, height
	button[#button].x, button[#button].y = button[#button].x - (button[#button].width / 2), button[#button].y - (button[#button].height / 2)
end

function button.update(dt)
	for i = 1, #button do
		button.detectVisibility(button[i])

		if button[i].enabled then
			button.highlight(button[i])
		end
	end
end

function button.detectVisibility(me)
	--Checks to make sure buttons are only usable/rendered when they need to be.
	if (me.activeState == "pauseButton" and LET_GAME_PAUSED and LET_PANEL_FOCUS == false) or (me.activeState == LET_PANEL_OPEN and LET_GAME_PAUSED) or (me.activeState == LET_CUR_GAME_STATE and not LET_GAME_PAUSED) then
		me.enabled = true
	else
		me.enabled = false
		--if a button is selected and then it becomes disabled, this 
		--ensures that it is unselected
		me.highlight = false
	end
end

function button.highlight(me)
	if mouseX >= me.x and
	mouseX <= me.x + me.width and
	mouseY >= me.y and
	mouseY <= me.y + me.height then
		me.highlight = true
	else
		me.highlight = false
	end

	--this is the var being printed to console
	LET_BUTTON_SELECTED = me.highlight
end
MrFariator
Party member
Posts: 548
Joined: Wed Oct 05, 2016 11:53 am

Re: Logic only affected last index in table

Post by MrFariator »

This is because you're assigning a value to LET_BUTTON_SELECTED every time the button.highlight() function is called. As such, you're overwriting it over and over again, until you reach the last element in your buttons table. This ensures that only the last button actually matters.

I think in your code's case you could solve this simply by (1) adding a little if-statement around the assignment, or alternatively (2) return from the function if the button is not highlighted.

Code: Select all

function button.highlight(me)
  if mouseX >= me.x and
  mouseX <= me.x + me.width and
  mouseY >= me.y and
  mouseY <= me.y + me.height then
    me.highlight = true
  else
    me.highlight = false
    -- (2) put a return statement here
  end

  --this is the var being printed to console
  if me.highlight and not LET_BUTTON_SELECTED then -- (1) could be simplified but just illustrating the conditions
    LET_BUTTON_SELECTED = me.highlight
  end
end
And then you set LET_BUTTON_SELECTED to false before any button processing is done within the current frame.

Realistically though, I'd much prefer not using globals, and instead returning values from the button handling functions - and acting correspondingly on those, but that's a discussion for another time.
Post Reply

Who is online

Users browsing this forum: Azzla, Google [Bot] and 2 guests