[SOLVED]main menu questions (buttons, loading other)...

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.
User avatar
ntpspe
Prole
Posts: 31
Joined: Sun Aug 07, 2011 6:19 pm

[SOLVED]main menu questions (buttons, loading other)...

Post by ntpspe »

Hey guys,

So I've made a simple menu, just with a logo and background music, along with a nice 'debug' part in the top left telling me the FPS, dt and which keys/mousebuttons are being pressed.

1.So my first question, i'm using " fps = love.timer.getFPS( )" to get the fps, then printing it in the " love.graphics.print(fps .." FPS", 0, 30, 0)" way.

But... it says 0 all the time. Can somebody just explain why? Is it because i'm on the menu which is static (nothings moving).

2. Can I have someone post a reply that's a simple "How to make buttons" please? As in explain every part of it, i've seen another forum post about simple buttons, and I do understand, but i'd rather learn in terms similar to the wiki if possible :P Not "Download this and copy it in" or "just do this".

3. When the user clicks 'New Game' is there some way for it to move from main.lua to... game.lua or something? So that I can have the main.lua as the menu and for global variables, and then the main game will be in another .lua file?

Thanks in advance

-NT
Last edited by ntpspe on Mon Aug 08, 2011 2:00 pm, edited 1 time in total.
User avatar
ntpspe
Prole
Posts: 31
Joined: Sun Aug 07, 2011 6:19 pm

Re: main menu questions (buttons, loading other .lua files e

Post by ntpspe »

PS: I think someone should do a button tutorial in the wiki :P
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: main menu questions (buttons, loading other .lua files e

Post by thelinx »

First, don't double post.
ntpspe wrote: 1.So my first question, i'm using " fps = love.timer.getFPS( )" to get the fps, then printing it in the " love.graphics.print(fps .." FPS", 0, 30, 0)" way.

But... it says 0 all the time. Can somebody just explain why? Is it because i'm on the menu which is static (nothings moving).
Sounds like you're setting the fps variable in love.load() or in the global scope. Try putting that bit of code in love.update().
2. Can I have someone post a reply that's a simple "How to make buttons" please? As in explain every part of it, i've seen another forum post about simple buttons, and I do understand, but i'd rather learn in terms similar to the wiki if possible :P Not "Download this and copy it in" or "just do this".
It isn't as easy as "just do this" unless you want to download something and use in your project.
To do buttons without any third-party libraries, you'd have to manually check mouse positions and see if they are within a certain area of the screen, and then draw a corresponding image.
3. When the user clicks 'New Game' is there some way for it to move from main.lua to... game.lua or something? So that I can have the main.lua as the menu and for global variables, and then the main game will be in another .lua file?
Make your love.update and love.draw callbacks do different stuff depending on what "state" the game is in. Something like this:

Code: Select all

function love.load()
  state = "menu" -- start the game in the menu state
end

function new_game()
  -- do stuff
  state = "game"
end

function love.draw()
  if state == "menu" then
    -- draw menu
  elseif state == "game" then
    -- draw game
  end
end

-- et. c.
User avatar
ntpspe
Prole
Posts: 31
Joined: Sun Aug 07, 2011 6:19 pm

Re: main menu questions (buttons, loading other .lua files e

Post by ntpspe »

Sounds like you're setting the fps variable in love.load() or in the global scope. Try putting that bit of code in love.update().
Yeah I was doing, thanks, i'll move it now and give it a shot :)
It isn't as easy as "just do this" unless you want to download something and use in your project.
To do buttons without any third-party libraries, you'd have to manually check mouse positions and see if they are within a certain area of the screen, and then draw a corresponding image.
I don't think I worded it correctly, but yeah, basically instructions for how to manually check the mouse positions to see if they're in the 'button' area. I didn't mean like proper buttons :)
Make your love.update and love.draw callbacks do different stuff depending on what "state" the game is in. Something like this:
That makes sense, but I basically want my game split into different files so that if I want to edit a specific part later, i can just load up 'inventory.lua' for example.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: main menu questions (buttons, loading other .lua files e

Post by Robin »

From your posts, I can see that you need some understanding of Lua and programming in general before you do things like that. That's not a bad thing, but you need to realise that something like "basically instructions for how to manually check the mouse positions to see if they're in the 'button' area." requires nothing more than basic programming skills (or in fact basic math skills).
Help us help you: attach a .love.
User avatar
ntpspe
Prole
Posts: 31
Joined: Sun Aug 07, 2011 6:19 pm

Re: main menu questions (buttons, loading other .lua files e

Post by ntpspe »

I understand how to do it, just thought it'd help if there were a tutorial on it :)

And yeah, I'm still in the learning process, hence why I chose LOVE over SDL or Irrlicht or something ;) Everyone's gotta start somewhere, and I'm using LOVE as my learning project. I have coded C++ applications in the past :)

EDIT: And that's what forums are for are they not? Asking questions and learning things? And thelinx has been very helpful, moving the FPS and deltatime worked perfectly, so my debug menu is working now :)

On another note, whenever I use love.graphics.newFont, either in love.load or anywhere else, even if I just set the size, I get a massive memory leak, and my fps drops to 3. Without using love.graphics.newFont anywhere, I get 45fps minimum. Is this a bug? It was mentioned in the bug tracker before.
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: main menu questions (buttons, loading other .lua files e

Post by nevon »

ntpspe wrote:On another note, whenever I use love.graphics.newFont, either in love.load or anywhere else, even if I just set the size, I get a massive memory leak, and my fps drops to 3. Without using love.graphics.newFont anywhere, I get 45fps minimum. Is this a bug? It was mentioned in the bug tracker before.
love.graphics.newFont returns a new font object, so you should never do it outside of love.load. I would recommend doing something like this:

Code: Select all

function love.load()
    fonts = {
        small = love.graphics.newFont(11),
        medium = love.graphics.newFont(16),
        large = love.graphics.newFont(22)
    }
end

function love.draw()
    love.graphics.setFont(fonts.large)
    love.graphics.printf("Look, ma! No memory leaks!", 0, love.graphics.getHeight()/2, love.graphics.getWidth(), "center")
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: main menu questions (buttons, loading other .lua files e

Post by Robin »

ntpspe wrote:EDIT: And that's what forums are for are they not? Asking questions and learning things? And thelinx has been very helpful, moving the FPS and deltatime worked perfectly, so my debug menu is working now :)
Yes, you are right here.

The thing is, the forums can only help you if you have specific questions or problems. To check if the mouse is within a certain area, I could tell you to use love.mouse.getPosition, but you should be able to think of a way to compare that location to some other numbers. See Programming in Lua if you don't know the right syntax.
Help us help you: attach a .love.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: main menu questions (buttons, loading other .lua files e

Post by Taehl »

ntpspe wrote:3. When the user clicks 'New Game' is there some way for it to move from main.lua to... game.lua or something? So that I can have the main.lua as the menu and for global variables, and then the main game will be in another .lua file?
The fastest, easiest way that I can think of would be to make a new file called game.lua or something, which contains love.load, love.update, love.draw, and all the other functions you want. Then, to start your game, use the code

Code: Select all

love.filesystem.load("game.lua")() love.load()
Doing that will overwrite the love.update and such from your main menu and replace them with that of the game.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
ntpspe
Prole
Posts: 31
Joined: Sun Aug 07, 2011 6:19 pm

Re: main menu questions (buttons, loading other .lua files e

Post by ntpspe »

Thanks Taehl :) I managed to do that last night and it all works fine :)

@Robin yeah, Well I've managed to get button detection working by creating 4 variables, x1, x2, y1 and y2, which set the boundary for the 200x100 button.

So basically,

Code: Select all

function love.mousepressed(x ,y ,button)
	if button == "l" and x > spbuttonx1 and x < spbuttonx2 and y > spbuttony1 and y < spbuttony2 then
		buttontest = "You clicked the singleplayer button"
	singleplayer()
	end
end
Which works perfectly :)
Post Reply

Who is online

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