For loop not iterating through all objects of list?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
cashmere
Prole
Posts: 5
Joined: Thu Dec 29, 2022 4:45 am

For loop not iterating through all objects of list?

Post 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!
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

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

Post by ivan »

Are you modifying the table during iteration?
User avatar
knorke
Party member
Posts: 274
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

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

Post by knorke »

or deleting entries from the middle?
# stops counting as soon as it hits a gap in the indices.
cashmere
Prole
Posts: 5
Joined: Thu Dec 29, 2022 4:45 am

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

Post 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)
Attachments
app.zip
(2.28 KiB) Downloaded 50 times
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
cashmere
Prole
Posts: 5
Joined: Thu Dec 29, 2022 4:45 am

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

Post by cashmere »

Thank you! The problem was the mousepressed thing, plus I localized a bunch of stuff so yeah looking way better now
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests