Newbie help with ipairs

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
ThatMisterM
Prole
Posts: 1
Joined: Sat Apr 29, 2023 8:26 am

Newbie help with ipairs

Post by ThatMisterM »

Hi,

I'm very new to Love2d and am struggling with ipairs, hopefully someone can help! :ultrahappy:

I'm trying to iterate through a table of cards to check whether or not a card has the value "isClicked", set to true. Then if so, I want the x and y of the card to be the same as the mouse cursor.

Essentially, if I've clicked on the card, I want it to follow the mouse around.

I can do this without using a table using:

Code: Select all

function love.update(dt)
	mouseX, mouseY = love.mouse.getPosition()
	if card.isClicked == true then
		card.x = mouseX - card.width / 2
		card.y = mouseY - card.height / 2
	end
end
However when trying to do this in an ipairs iteration, like this:

Code: Select all

function love.update(dt)

	mouseX, mouseY = love.mouse.getPosition()
	for i,v in ipairs(cards) do
 		if v.isClicked == true then do
 			v.x = mouseX - v.width / 2
 			v.y = mouseY - v.height / 2
 		end
	end

end
I get the following error:

Error

Syntax error: main.lua:74: 'end' expected (to close 'function' at line 33) near '<eof>'



Traceback

[love "callbacks.lua"]:228: in function 'handler'
[C]: at 0x7ff952b231d0
[C]: in function 'require'
[C]: in function 'xpcall'
[C]: in function 'xpcall'


Am I not supposed to be doing "if ... then do" inside an ipairs iteration?
User avatar
BrotSagtMist
Party member
Posts: 655
Joined: Fri Aug 06, 2021 10:30 pm

Re: Newbie help with ipairs

Post by BrotSagtMist »

Your do has no end statement to close the block, how is that even worth a question seeing the error message tells you exactly that?
if >end
do > end
function > end
No exceptions.
obey
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: Newbie help with ipairs

Post by Andlac028 »

Just remove the do after then in if condition
User avatar
Bobble68
Party member
Posts: 162
Joined: Wed Nov 30, 2022 9:16 pm
Contact:

Re: Newbie help with ipairs

Post by Bobble68 »

It's an easy mistake to make if you aren't familiar with lua, and the error message isn't particularly helpful if you don't already know the solution. If statements always have a 'then' at the end of the conditions, and ipairs, pairs and while have a do at the end.

The error you're getting is actually complaining that you have an unended block, as Brot has pointed out in a very polite manner, though he doesn't seem to have read your code to see what the mistake is. I'm going to assume you're new to programming in general, so apologies if I'm over explaining things for you.

In programming there's a concept known as scope, which essentially describes how long variables are able to be used. All local variables go out of scope when you leave the block they were declared in - here's an example

Code: Select all

if true then -- start a block
  local x = 5
  print(x) --> 5
end          -- end a block

print(x)

--> nil
As you can see from there, as soon as you reach the 'end', x is no longer kept and trying to print it will give you nil. Notice how in this example, you get a different result:

Code: Select all

local x = 4
if true then
  x = 5
end

print(x)

--> 5
Why is this relevent? Because you can use 'do' to start a block too, without the need for an if statement:

Code: Select all

do  -- start a block
  local x = 5
end -- end a block

print(x)
--> nil
Unlike some languages like Python, Lua treats spaces and new lines to be the same thing, we just use new lines to make it easier for us to see. So if I were to reformat your code slighty, here's what the computer is seeing:

Code: Select all

function love.update(dt)

	mouseX, mouseY = love.mouse.getPosition()
	for i,v in ipairs(cards) do
 		if v.isClicked == true then 
 			do
 				v.x = mouseX - v.width / 2
 				v.y = mouseY - v.height / 2
 			end
		end
	end
	
--> HEY THERE'S AN END MISSING HERE!!!! ('end' expected (to close 'function' at line 33) near '<eof>')
I hope this clears things up! Good luck with your Love2D journey, don't be afraid to ask for help, the majority of people on this forum are pretty nice and helpful.


Edit - something I forgot to add is how global and local variables work. When you first declare a variable, you can either put 'local' in front, or leave it blank. If you leave it blank, it will be global, and can be accessed anywhere within your code regardless of the block. However, they are slightly slower to use, and this can get extremely messy if you overuse it. I fairly recently had an issue with having a global variable called 'mouse', as well as a local variable called 'mouse'. TLDR, try to avoid globals unless you have a good reason to use them.
Dragon
User avatar
BrotSagtMist
Party member
Posts: 655
Joined: Fri Aug 06, 2021 10:30 pm

Re: Newbie help with ipairs

Post by BrotSagtMist »

Am i reading sarcasm here? How rude. I think.
Bobble68 wrote: Sat Apr 29, 2023 8:43 pm However, they are slightly slower to use, and this can get extremely messy if you overuse it. I fairly recently had an issue with having a global variable called 'mouse', as well as a local variable called 'mouse'. TLDR, try to avoid globals unless you have a good reason to use them.
I am basically on the other edge here. I only use locals when necessary, creates less bugs and is less of a headache to transfer data.
And the being slower holds true for lua, but we use luajit and that cares less about this.
obey
User avatar
Bobble68
Party member
Posts: 162
Joined: Wed Nov 30, 2022 9:16 pm
Contact:

Re: Newbie help with ipairs

Post by Bobble68 »

BrotSagtMist wrote: Sat Apr 29, 2023 9:06 pm Am i reading sarcasm here? How rude. I think.
Bobble68 wrote: Sat Apr 29, 2023 8:43 pm However, they are slightly slower to use, and this can get extremely messy if you overuse it. I fairly recently had an issue with having a global variable called 'mouse', as well as a local variable called 'mouse'. TLDR, try to avoid globals unless you have a good reason to use them.
I am basically on the other edge here. I only use locals when necessary, creates less bugs and is less of a headache to transfer data.
And the being slower holds true for lua, but we use luajit and that cares less about this.
Ah true, but I'll take any speed I can get for free. I personally find it more helpful to compartmentalise things into little boxes I can forget about, which is much more helpful for a big project, otherwise I'm at risk of forgetting about using an already existing variable name. The number of bugs it causes is probably completely dependant on what your prefered programming style is. I prefer OOP for the same reason, though I know you don't like it so you probably have a different way of looking at code.
Dragon
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 6 guests