Hello people,
I've fell in LÖVE today and started creating a game.
It goes well so far, but I am new into the "game developement process" (I've been programming only websites and Project Euler problems until now - or hey, I remember I played with Game Maker for a little while) and I don't know how to split the main.lua into more files.
At the moment everything is in main.lua and I feel my code is already "bloated". I've downloaded some examples and projects from other people, but I don't understand the process well.
My game is in the attachment, so explore the source freely... What I want to achieve is splitting the main.lua into intro.lua, menu.lua, etc.
Also, does anyone here have better idea of how to make separate "rooms" (yeah, I have this terminology surely from the Game Maker ) and blend between them? I am doing it by the "play" variable, which points to functions. Not ideal, I guess :/
Thanks in advance, LÖVE you all
Martin
Splitting the main.lua
Splitting the main.lua
- Attachments
-
- pkmn.love
- (30.23 KiB) Downloaded 156 times
Re: Splitting the main.lua
Try: require("intro.lua"). After that call, everything in intro.lua will be available.
(Haven't looked at your code. )
(Haven't looked at your code. )
Re: Splitting the main.lua
Yeah, I knew THAT already, but how should I split it - WHAT should be in that intro.lua? (Regardless on what the intro does)
For example, should it have its own draw(), update() and load(), which would rewrite the original one? I guess this wouldn't work...
EDIT: As I read it now, it really sounds aggresive. My apologies, I didn't mean it to ;-)
For example, should it have its own draw(), update() and load(), which would rewrite the original one? I guess this wouldn't work...
EDIT: As I read it now, it really sounds aggresive. My apologies, I didn't mean it to ;-)
Last edited by MaJJ on Sat May 30, 2009 9:47 pm, edited 1 time in total.
Re: Splitting the main.lua
the way i usually avoid loves callbacks is to make a table
scene = {}
then load up intro.lua, in there put something like
function scene.draw()
intro stuff
end
back in the main.lua, in (lets say draw) i put
scene.draw()
now when i need to load up a new scene the scene table gets overwritten with the new stuff and love keeps going, just be sure to use the same function names in each script so that the callbacks always calls scene.draw() or scene.update(), and it will just run whatever you have in memory for those names
you can also put all the other data needed for that scene in to the table, so when its time for a new scene, level or whatever you only have to blank out 1 table
scene = {}
then load up intro.lua, in there put something like
function scene.draw()
intro stuff
end
back in the main.lua, in (lets say draw) i put
scene.draw()
now when i need to load up a new scene the scene table gets overwritten with the new stuff and love keeps going, just be sure to use the same function names in each script so that the callbacks always calls scene.draw() or scene.update(), and it will just run whatever you have in memory for those names
you can also put all the other data needed for that scene in to the table, so when its time for a new scene, level or whatever you only have to blank out 1 table
Last edited by osgeld on Sat May 30, 2009 9:30 pm, edited 1 time in total.
Re: Splitting the main.lua
My apologies. Not everyone knows that.
One way of doing it is using a stack of states:
You could also ... eh resume to the intro by doing a table.remove(states), although that makes more sense with a stack which looks like, for instance.
Hope I didn't offend you this time.
One way of doing it is using a stack of states:
Code: Select all
-- main.lua
intro = {
update = function(dt)
if intro.done then
-- Push main menu to the top when intro is done.
table.insert(states, mainmenu)
end
end,
draw = function()
-- Draw into.
end
}
mainmenu = {
update = function(dt)
-- Stuff
end,
draw = function()
-- Draw main menu.
end
}
states = {}
-- Push intro.
table.insert(states, intro)
function update(dt)
states[#states].update(dt)
end
function draw()
states[#states].draw()
end
Code: Select all
[4] quit game confirmation
[3] in-game menu
[2] main game
[1] titlescreen
Re: Splitting the main.lua
Sorry for being you - eh, I mean rude :-)
I'll try to do the things you suggested, thanks!
EDIT: It definitely worked. My code's clean again, you saved me
I'll try to do the things you suggested, thanks!
EDIT: It definitely worked. My code's clean again, you saved me
Who is online
Users browsing this forum: Google [Bot] and 2 guests