Error regarding basic main menu and tables SOLVED

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.
Sosomojo
Prole
Posts: 6
Joined: Wed Nov 14, 2012 5:09 pm

Error regarding basic main menu and tables SOLVED

Post by Sosomojo »

Hello. New here. Got into starting to learn about love a few weeks ago. I'm no code monkey by any means, my previous experience being mostly in modding and the making of 2d graphics (which I have over the years gotten quite good at). So, I'm no coder by any previous experience. This means that when I try to learn things I tend to mess up and then not know what the problem is, so I might be here asking stupid questions a lot as I'm trying to learn the basics from the ground up. I learn mostly by doing, so I have followed some basic tutorials and succeeded with some basic things, but now I'm actually trying to do some things that I might in some form actually end up using for something, not just "red rectangle can move around". This means I've started building a main menu, which will feature some buttons for things that might at some point be scripted assuming I do not ragequit along the way. As such, I have attempted to use some basic table management in order to make some form of main menu. In part, this is in order to save codespace, and in part it is to familiarise myself with the use of tables in luascript.

The love file I have here produces a syntax error. I do not know what it wants me to do to solve it, and therefore I'm asking for help now. The main menu is image-based, featuring a few buttons and a background texture. The love file features code that is supposed to make the buttons glow when the mouse is hovering over them, which is what is problematic. If I comment out all the code for drawing the buttons, the script runs and the background texture renders along with the cursor. That stuff works. The only thing that is scripted for at this point is the main menu. The image folder in the love also contains some relics that are not used.

The error I get is:
syntax error: menu.lua:60 ')' expected near main_menu_buttons

What follows is the functions dealing with said table to give some context:

Code: Select all

function main_menu_buttons_load(number, width, height)
	local CentralX = find_central_x()
	local HalfWidth = width/2
	for var = 1, number, 1 do
		local var2 = var - 1
		main_menu_buttons[var].X = CentralX - HalfWidth
		main_menu_buttons[var].Y = 128 + var2 * 128
		main_menu_buttons[var].RelaxedImage = love.graphics.newImage("images/mainmenubutton"..var..".png")
		main_menu_buttons[var].HoverImage = love.graphics.newImage("images/mainmenubutton"..var.."hover.png")
		main_menu_buttons[var].Width = width
		main_menu_buttons[var].Height = height
		main_menu_buttons[var].Hover = false
	end
end

Code: Select all

function main_menu_buttons_update(number)
	local MouseX = love.mouse.getX()
	local MouseY = love.mouse.getY()
	local var = 1
	for var = 1, number, 1 do
		if MouseX > main_menu_buttons[var].X 
		and MouseX < main_menu_buttons[var].X + main_menu_buttons[var].Width
		and MouseY > main_menu_buttons[var].Y 
		and MouseY < main_menu_buttons[var].Y  + main_menu_buttons[var].Height
		then
			main_menu_buttons[var].Hover = true
		else
			main_menu_buttons[var].Hover = false
		end
	end
		
end
And here is the block that the error traces back to, starting at line 56.

Code: Select all

function main_menu_buttons_draw(number)
	local var = 1
	for var = 1, number, 1 do
		if main_menu_buttons[var].Hover == true then
			love.graphics.draw(main_menu_buttons[var].HoverImage, main_menu_buttons[var].X main_menu_buttons[var].Y)
		else
			love.graphics.draw(main_menu_buttons[var].RelaxedImage, main_menu_buttons[var].X main_menu_buttons[var].Y)
		end
	end
end
The code below this is unrelated and works.
Attachments
project.love
(115.94 KiB) Downloaded 115 times
Last edited by Sosomojo on Thu Nov 15, 2012 3:24 pm, edited 1 time in total.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Error regarding basic main menu and the management of ta

Post by Nixola »

Code: Select all

function main_menu_buttons_draw(number)
   local var = 1
   for var = 1, number, 1 do
      if main_menu_buttons[var].Hover == true then
         love.graphics.draw(main_menu_buttons[var].HoverImage, main_menu_buttons[var].X main_menu_buttons[var].Y)
      else
         love.graphics.draw(main_menu_buttons[var].RelaxedImage, main_menu_buttons[var].X main_menu_buttons[var].Y)
      end
   end
end

You have to separate the X and the Y with a comma, like every other argument, so it should be this:

Code: Select all

function main_menu_buttons_draw(number)
   local var = 1
   for var = 1, number, 1 do
      if main_menu_buttons[var].Hover == true then
         love.graphics.draw(main_menu_buttons[var].HoverImage, main_menu_buttons[var].X, main_menu_buttons[var].Y) --notice the comma
      else
         love.graphics.draw(main_menu_buttons[var].RelaxedImage, main_menu_buttons[var].X, main_menu_buttons[var].Y) --notice it
      end
   end
end

P.S: you don't need to declare or create the variable used in a 'for' loop, it's created automatically
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Sosomojo
Prole
Posts: 6
Joined: Wed Nov 14, 2012 5:09 pm

Re: Error regarding basic main menu and the management of ta

Post by Sosomojo »

Thanks for the quick reply. Still get another error though, this time under the load function, at line 13, or the line where the x value is added to the table in the for statement. There I attempt to index a nil value. Will try out some things to see if I can find out what the problem is. Any help is appreciated though, as I'm trying to actually learn how to do this from the ground up.
spir
Citizen
Posts: 76
Joined: Wed Oct 17, 2012 1:12 pm

Re: Error regarding basic main menu and the management of ta

Post by spir »

... la vita e estrany ...
spir
Citizen
Posts: 76
Joined: Wed Oct 17, 2012 1:12 pm

Re: Error regarding basic main menu and the management of ta

Post by spir »

Nixola wrote: P.S: you don't need to declare or create the variable used in a 'for' loop, it's created automatically
You also don't need "== true" in

Code: Select all

if main_menu_buttons[var].Hover == true then
Understand why? (If not, ask).

Denis
... la vita e estrany ...
Sosomojo
Prole
Posts: 6
Joined: Wed Nov 14, 2012 5:09 pm

Re: Error regarding basic main menu and the management of ta

Post by Sosomojo »

No, I'm afraid I'm not quite familiar with the way lua handles a lot of things. So I don't really understand why I don't need it.

My programming knowledge is based on having courses in java in high school (and that was quite a few years ago) and modding Neverwinter Nights a few years back (using a language based on C).

So now I'm dusting off the old keyboard and I'm having a good time learning how to code with something that feels fresher somehow, but I'm not used to it's quirks.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Error regarding basic main menu and the management of ta

Post by Nixola »

Code: Select all

if (condition) then --[[some code that will be executed if the condition is not false or nil]] end
main_menu_buttons[var].Hover can either be equal to true or equal to false, so writing main_menu_buttons[var].Hover means writing true or false. When main_menu_buttons[var].Hover is true, the code "main_menu_buttons[var].Hover == true" is equal to true... Just as main_menu_buttons[var].Hover, that so doesn't have to be compared to true. When it's false, it's equal to false or nil (false, obviously) and so the code is not executed. If you didn't understand something here (I don't think I explained it well ^^') just ask
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
spir
Citizen
Posts: 76
Joined: Wed Oct 17, 2012 1:12 pm

Re: Error regarding basic main menu and the management of ta

Post by spir »

Nixola wrote:

Code: Select all

if (condition) then --[[some code that will be executed if the condition is not false or nil]] end
main_menu_buttons[var].Hover can either be equal to true or equal to false, so writing main_menu_buttons[var].Hover means writing true or false. When main_menu_buttons[var].Hover is true, the code "main_menu_buttons[var].Hover == true" is equal to true... Just as main_menu_buttons[var].Hover, that so doesn't have to be compared to true. When it's false, it's equal to false or nil (false, obviously) and so the code is not executed. If you didn't understand something here (I don't think I explained it well ^^') just ask
Couldn't have said better. However, let me try to add a little point. When you write this:
]

Code: Select all

if condition then action() end
you already ask Lua to check whether condiition is true or false. It needs to check it to know whether it should launch the associated action. Checking whether condition is true is just testing:

Code: Select all

condition == true
so that by adding an "== true" yourself you just let Lua checking

Code: Select all

(condition == true) == true
Get it? (If not, don't worry, you'll have an illumination some day.)

Nicely enough, this does not change the logic: if condition happens, then logically enough (condition == true) is also true and if it's false ...I'll let you finish ;). Otherwise, many programs would be wrong (because such expressions are quite common).

Denis
... la vita e estrany ...
Sosomojo
Prole
Posts: 6
Joined: Wed Nov 14, 2012 5:09 pm

Re: Error regarding basic main menu and the management of ta

Post by Sosomojo »

Ok guys. I've now rewritten most of the code. I'm still not sure what I'm doing wrong, but I keep getting syntax errors. This time it expects an ')' near the = on line 5.

Here is the new code I've written:

Code: Select all

main_menu_buttons = {}

--spawn function for buttons that are not completely image based. tx and ty are text coordinates while the x and the y are image coordinates.
function button_spawn(x, y, w, h, tx, ty, text)
	table.insert(main_menu_buttons = {x = x, y = y, w = w, h = h, tx = tx, ty = ty, text = text}) -- line 5
	print("Button data has been inserted into a table.")
end

--loads the main menu buttons.
function main_menu_buttons_load()
	button_spawn(288, 32, 256, 64, 300, 40, "NEW GAME")
	button_spawn(288, 128, 256, 64, 300, 138, "LOAD GAME")
	button_spawn(288, 224, 256, 64, 300, 234, "OPTIONS")
	button_spawn(288, 320, 256, 64, 300, 330, "CREDITS")
	button_spawn(288, 416, 256, 64, 300, 426, "MANUAL")
	button_spawn(288, 512, 256, 64, 300, 522, "QUIT")
	print "Main menu button data has been inserted into the correct table."
end

function main_menu_buttons_draw()
	for i,v in ipairs(main_menu_buttons) do
		local inside = insidebox(mouseX, mouseY, v.x, v.y, v.w, v.h)
		if inside then
			love.graphics.setColor(255, 255, 255)
			love.graphics.draw(main_menu_buttonhover, v.x, v.y)
		else
			love.graphics.setColor(255, 255, 255)
			love.graphics.draw(main_menu_button, v.x, v.y)
		end
		love.graphics.setColor(255, 255, 255)
		love.graphics.setFont(main_menu_font)
		love.graphics.print(v.text, v.tx, v.ty)
	end
end
Also the following function is used:

Code: Select all

function inside_box(px, py, x, y, wx, wy)
	if px > x and px < x + wx and py > y and py < y+wy then
		return true
	else
		return false
	end
end
Any help would be greatly appreciated. I'm not sure what I'm doing wrong here. Do you need a .love or is the error easy to spot for somebody more experienced than I?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Error regarding basic main menu and the management of ta

Post by Robin »

Code: Select all

table.insert(main_menu_buttons =
That = needs to be a comma.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 4 guests