Splitting the main.lua

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
MaJJ
Prole
Posts: 7
Joined: Sat May 30, 2009 8:55 pm

Splitting the main.lua

Post by MaJJ »

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 :D ) 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
Attachments
pkmn.love
(30.23 KiB) Downloaded 155 times
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Splitting the main.lua

Post by rude »

Try: require("intro.lua"). After that call, everything in intro.lua will be available.

(Haven't looked at your code. :monocle:)
User avatar
MaJJ
Prole
Posts: 7
Joined: Sat May 30, 2009 8:55 pm

Re: Splitting the main.lua

Post by MaJJ »

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 ;-)
Last edited by MaJJ on Sat May 30, 2009 9:47 pm, edited 1 time in total.
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

Re: Splitting the main.lua

Post by osgeld »

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
Last edited by osgeld on Sat May 30, 2009 9:30 pm, edited 1 time in total.
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Splitting the main.lua

Post by rude »

My apologies. :rofl: Not everyone knows that.

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

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.

Code: Select all

[4] quit game confirmation
[3] in-game menu
[2] main game
[1] titlescreen
Hope I didn't offend you this time.
User avatar
MaJJ
Prole
Posts: 7
Joined: Sat May 30, 2009 8:55 pm

Re: Splitting the main.lua

Post by MaJJ »

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 ^^
Post Reply

Who is online

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