Page 1 of 1

Splitting the main.lua

Posted: Sat May 30, 2009 9:09 pm
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

Re: Splitting the main.lua

Posted: Sat May 30, 2009 9:13 pm
by rude
Try: require("intro.lua"). After that call, everything in intro.lua will be available.

(Haven't looked at your code. :monocle:)

Re: Splitting the main.lua

Posted: Sat May 30, 2009 9:16 pm
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 ;-)

Re: Splitting the main.lua

Posted: Sat May 30, 2009 9:23 pm
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

Re: Splitting the main.lua

Posted: Sat May 30, 2009 9:29 pm
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.

Re: Splitting the main.lua

Posted: Sat May 30, 2009 9:34 pm
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 ^^