Page 1 of 2
[SOLVED]main menu questions (buttons, loading other)...
Posted: Sun Aug 07, 2011 8:55 pm
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
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
Re: main menu questions (buttons, loading other .lua files e
Posted: Sun Aug 07, 2011 8:57 pm
by ntpspe
PS: I think someone should do a button tutorial in the wiki
Re: main menu questions (buttons, loading other .lua files e
Posted: Sun Aug 07, 2011 9:28 pm
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
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.
Re: main menu questions (buttons, loading other .lua files e
Posted: Sun Aug 07, 2011 9:53 pm
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.
Re: main menu questions (buttons, loading other .lua files e
Posted: Sun Aug 07, 2011 10:14 pm
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).
Re: main menu questions (buttons, loading other .lua files e
Posted: Sun Aug 07, 2011 10:32 pm
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.
Re: main menu questions (buttons, loading other .lua files e
Posted: Mon Aug 08, 2011 6:33 am
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
Re: main menu questions (buttons, loading other .lua files e
Posted: Mon Aug 08, 2011 7:17 am
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.
Re: main menu questions (buttons, loading other .lua files e
Posted: Mon Aug 08, 2011 8:09 am
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.
Re: main menu questions (buttons, loading other .lua files e
Posted: Mon Aug 08, 2011 1:19 pm
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