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.