[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
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:Thanks Taehl :) I managed to do that last night and it all works fine :)
Oh, shit, I forgot to warn you against that method. It is evil, please don't use it. It runs "game.lua" every time you start a new game.

Code: Select all

require "game"
is much better. You will want game.lua to provide a function that sets things up in that case. You then call that function when the player starts a new game.
ntpspe wrote:@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.
Exactly. :nyu: If you want more buttons, it might be an idea to use tables to avoid code duplication.
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 »

Robin wrote:Oh, shit, I forgot to warn you against that method. It is evil, please don't use it. It runs "game.lua" every time you start a new game.
Don't worry, i'm using:

Code: Select all

        function love.load()
        newgame = love.filesystem.load("newgame.lua")
        end
        [ALL the rest of the code for buttons and that]
        function startanewgame()
        newgame()
        end
So that when the user clicks my newgame button, it runs "startanewgame" which then loads "newgame" from the newgame.lua which is working perfectly, and it loads the lua file at the start ready, but only executes it when called.
Robin wrote: Exactly. :nyu: If you want more buttons, it might be an idea to use tables to avoid code duplication.
Yeah, I was thinking tables would be handy, what do you mean by code duplication though?
Personally I'd prefer to manually code the co-ordinates of each button and the checks for them, it might not be efficient, but it works
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:Don't worry, i'm using:
That is less evil, I'll give you that. Still, evil, because it still runs "newgame.lua" every time you start a new game.
ntpspe wrote:what do you mean by code duplication though?
Instead of:

Code: Select all

--check for button 0
--check for button 1
--check for button 2
--check for button 3
--check for button 4
--check for button 5
--check for button 6
--check for button 7
--check for button 8
--check for button 9
--check for button 10
--check for button 11
--check for button 12
--check for button 13
--check for button 14
--check for button 15
--check for button 16
--check for button 17
--check for button 18
--check for button 19
--check for button 20
--check for button 21
--check for button 22
--check for button 23
--check for button 24
--check for button 25
--check for button 26
--check for button 27
--check for button 28
--check for button 29
--check for button 30
--check for button 31
--check for button 32
--check for button 33
--check for button 34
--check for button 35
--check for button 36
--check for button 37
--check for button 38
--check for button 39
--check for button 40
--check for button 41
--check for button 42
--check for button 43
--check for button 44
--check for button 45
--check for button 46
--check for button 47
--check for button 48
--check for button 49
--check for button 50
--check for button 51
--check for button 52
--check for button 53
--check for button 54
--check for button 55
--check for button 56
--check for button 57
--check for button 58
--check for button 59
--check for button 60
--check for button 61
--check for button 62
--check for button 63
--check for button 64
--check for button 65
--check for button 66
--check for button 67
--check for button 68
--check for button 69
--check for button 70
--check for button 71
--check for button 72
--check for button 73
--check for button 74
--check for button 75
--check for button 76
--check for button 77
--check for button 78
--check for button 79
--check for button 80
--check for button 81
--check for button 82
--check for button 83
--check for button 84
--check for button 85
--check for button 86
--check for button 87
--check for button 88
--check for button 89
--check for button 90
--check for button 91
--check for button 92
--check for button 93
--check for button 94
--check for button 95
--check for button 96
--check for button 97
--check for button 98
--check for button 99
--check for button 100
--check for button 101
--check for button 102
--check for button 103
--check for button 104
--check for button 105
--check for button 106
--check for button 107
--check for button 108
--check for button 109
--check for button 110
--check for button 111
--check for button 112
--check for button 113
--check for button 114
--check for button 115
--check for button 116
--check for button 117
--check for button 118
--check for button 119
--check for button 120
--check for button 121
--check for button 122
--check for button 123
--check for button 124
--check for button 125
--check for button 126
--check for button 127
--check for button 128
--check for button 129
--check for button 130
--check for button 131
--check for button 132
--check for button 133
--check for button 134
--check for button 135
--check for button 136
--check for button 137
--check for button 138
--check for button 139
--check for button 140
--check for button 141
--check for button 142
--check for button 143
--check for button 144
--check for button 145
--check for button 146
--check for button 147
--check for button 148
--check for button 149
--check for button 150
--check for button 151
--check for button 152
--check for button 153
--check for button 154
--check for button 155
--check for button 156
--check for button 157
--check for button 158
--check for button 159
--check for button 160
--check for button 161
--check for button 162
--check for button 163
--check for button 164
--check for button 165
--check for button 166
--check for button 167
--check for button 168
--check for button 169
--check for button 170
--check for button 171
--check for button 172
--check for button 173
--check for button 174
--check for button 175
--check for button 176
--check for button 177
--check for button 178
--check for button 179
--check for button 180
--check for button 181
--check for button 182
--check for button 183
--check for button 184
--check for button 185
--check for button 186
--check for button 187
--check for button 188
--check for button 189
--check for button 190
--check for button 191
--check for button 192
--check for button 193
--check for button 194
--check for button 195
--check for button 196
--check for button 197
--check for button 198
--check for button 199
--check for button 200
you can do:

Code: Select all

for i, button in ipairs(button_list) do
  -- check for button i
end
Much better, isn't it?
ntpspe wrote:Personally I'd prefer to manually code the co-ordinates of each button and the checks for them, it might not be efficient, but it works
This might seem easier now, but it will bite you in the end.
Help us help you: attach a .love.
User avatar
ntpspe
Prole
Posts: 31
Joined: Sun Aug 07, 2011 6:19 pm

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

Post by ntpspe »

Robin wrote:That is less evil, I'll give you that. Still, evil, because it still runs "newgame.lua" every time you start a new game.
Yeah, but isn't that why we load all the images in [love.load()] and then draw them elsewhere, rather than loading them at every function that needs them? I thought the same principle would apply for loading all the rest of the files and setting default variables.

And nice 200 line example! Haha yeah... it might end up biting me later ¬_¬ I'll work on it :D
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post by Robin »

ntpspe wrote:Yeah, but isn't that why we load all the images in [love.load()] and then draw them elsewhere, rather than loading them at every function that needs them?
If you call newImage, newFont or things like that in newgame.lua, they will be loaded every time you start a new game, instead of just once.
Help us help you: attach a .love.
User avatar
ntpspe
Prole
Posts: 31
Joined: Sun Aug 07, 2011 6:19 pm

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

Post by ntpspe »

Ah I see. But I want 'newgame.lua' to be loaded every time the game is loaded don't I?

Also, on a side note, whenever I use [love.graphics.newFont] (and by whenever I mean it was placed in love.load() to load the font I wanted at the start of the game, but I also tried it elsewhere) The fps drops to 3 and my ram starts climbing slowly, to the point where the system crashes. (memoryleak?)
And it's not because I'm loading a font, even just changing the size of the default font caused the same problem :/

Code: Select all

        love.load()
        font = love.graphics.newFont(20)
        love.graphics.setFont(font)
        end
        
        love.draw()
        love.graphics.print("Some text", 100, 100)
        end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post by Robin »

ntpspe wrote:Ah I see. But I want 'newgame.lua' to be loaded every time the game is loaded don't I?
Hm, we might be thinking about two different things.
  • User starts game
  • Game loads
  • Menu
  • User selects "new game"
  • New game starts
  • User plays game
  • Back to menu after game over or user quitting
  • User selects "new game"
  • New game starts
  • User plays game
  • Back to menu after game over or user quitting
  • User selects "new game"
  • New game starts
  • ...
The method you're using does all those things (loading images, things like that) at the bullet points that say "New game starts". What I'm saying is that you only need to do that at the point that says "Game loads".
ntpspe wrote:Also, on a side note, whenever I use [love.graphics.newFont] (and by whenever I mean it was placed in love.load() to load the font I wanted at the start of the game, but I also tried it elsewhere) The fps drops to 3 and my ram starts climbing slowly, to the point where the system crashes.
I've never seen that before. Could you post details about your system (operating system, graphics card, things like that), so the devs might be able to do something about that?

What happens if you run games other people made (there are quite a lot little games and demos on the forums)?
Help us help you: attach a .love.
User avatar
ntpspe
Prole
Posts: 31
Joined: Sun Aug 07, 2011 6:19 pm

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

Post by ntpspe »

Oh I understand now :)

As for the problem, I think I accidentally made some kind of loop that was repeatedly setting the font to size 20, which must have been using up the system resources or something, I'm not sure, but I ended up replacing my text with images anyway, and just using the default font for anything else.
(It was late at night so probably made a mistake haha. There's a screenshot up on ntpspe.wordpress.com with the system monitor showing the graph peaking at 80% ram usage)
Post Reply

Who is online

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