Page 1 of 1

For loop not iterating through all objects of list?

Posted: Thu Dec 29, 2022 4:59 am
by cashmere

Code: Select all

load=function(self)
called=true        
guiobj={}
table.insert(guiobj,gui.addBtn(love.graphics.getWidth()/2.25,450,100,30,
function(self)
	if(not coin.flip)then
		coin.flip=true end
end,"call it",{1,0,1}))
table.insert(guiobj,gui.addToggle(200,200,50,50,called))
end,
update=function(self)
	for i=1,#guiobj do
		guiobj[i]:update()end
	end,
draw=function(self)
	for i=1,#guiobj do
		guiobj[i]:draw()end
end
For some reason, the code will only update the element most recently appended to guiobj. This remains consistent throughout various combinations of gui objects, so I believe I've isolated the problem to being in this segment, but if this seems all good I can send any and all bits of code that may be required. All of the objects have an update function so it should be fine there. I come from Pico-8 and while the implementation varies slightly, it should be quite similar, but I remain quite puzzled.
Thanks for any help!

Re: For loop not iterating through all objects of list?

Posted: Thu Dec 29, 2022 5:21 pm
by pgimeno
Hard to say. It could be the abuse of globals or an error in creating the objects or something else. Please post the complete project so we can take a look.

Re: For loop not iterating through all objects of list?

Posted: Thu Dec 29, 2022 5:45 pm
by ivan
Are you modifying the table during iteration?

Re: For loop not iterating through all objects of list?

Posted: Thu Dec 29, 2022 5:53 pm
by knorke
or deleting entries from the middle?
# stops counting as soon as it hits a gap in the indices.

Re: For loop not iterating through all objects of list?

Posted: Fri Dec 30, 2022 4:27 am
by cashmere
pgimeno wrote: Thu Dec 29, 2022 5:21 pm Hard to say. It could be the abuse of globals or an error in creating the objects or something else. Please post the complete project so we can take a look.
Hope this helps! (I'm going to look into abuse of globals that could be promising)

Re: For loop not iterating through all objects of list?

Posted: Fri Dec 30, 2022 6:12 pm
by pgimeno
I've checked and both update() functions are definitely called. Just insert print(self) at the beginning of the update function in gui.addBtn() and gui.addToggle() to see that.

However, you're redefining love.mousepressed inside both update functions. The Löve frame loop works roughly like this (some finer details are missing):
  1. read inputs
  2. call input events and any other events except love.update() and love.draw()
  3. call love.update(dt)
  4. clear the screen
  5. call love.draw()
  6. paint what was drawn
  7. start again
Guess what, love.mousepressed is one of these input events that are called in the second step. So what's happening is this:
  • When love.update is executed, you're redefining love.mousepressed twice, once for the button and once for the toggle.
  • The last value assigned (the one for the toggle) prevails.
  • Then everything is drawn etc. and the frame cycle begins again; the inputs are read.
  • Now love.mousepressed has a chance to run, but note that the event keeps the last value that it was set to, which is that of the toggle.
As a result, you only see one of the buttons take effect, because love.mousepressed is never set for the other one when the time to execute love.mousepressed comes.

A possible fix is to have a mousepressed method in the button and the toggle class; you don't really need an update function. Then have love.mousepressed (in main.lua) call the mousepressed events in all controls just like you do with update and draw.

Aside from that, there's a lot of abuse of globals; I didn't see any outstanding problems with it but it's a question of time that it will come bite you in the butt. In Lua, the rule of thumb is to keep everything local unless you really, really need it global, which is hardly ever.

Re: For loop not iterating through all objects of list?

Posted: Sat Dec 31, 2022 12:24 am
by cashmere
Thank you! The problem was the mousepressed thing, plus I localized a bunch of stuff so yeah looking way better now