Page 1 of 2

Question

Posted: Thu Oct 20, 2011 5:02 pm
by Acnesium
Hello!

I am just new to löve 2d and I got a very simple question. I already tried to look it up but I not sure how to supscribe it.
I trying to make a game and make the source pretty clean, so is it possible to created more .lua files and make them load in main.lua?

example:

I got main.lua but I want to include enemies.lua in main.lua.

I hope it was clear enough,

Thanks.

Re: Question

Posted: Thu Oct 20, 2011 5:05 pm
by ivan
Acnesium wrote:I trying to make a game and make the source pretty clean, so is it possible to created more .lua files and make them load in main.lua?
Yes, you can do this using "require ( filename )" or "dofile ( filename )"

Re: Question

Posted: Thu Oct 20, 2011 5:07 pm
by bartbes
ivan wrote: Yes, you can do this using "require ( filename )" or "dofile ( filename )"
Don't listen to that, it's wrong.
dofile won't work with love, and require is not supposed to get the filename.

A quick introduction to require, given a string "X.Y.Z" it will look for X/Y/Z.lua and X/Y/Z/init.lua.
This means that if you want to load enemies.lua, you run require("enemies").

Re: Question

Posted: Thu Oct 20, 2011 5:14 pm
by Acnesium
Thanks, both of you!

Re: Question

Posted: Thu Oct 20, 2011 5:21 pm
by ivan
Hm, I haven't used Love2D in a while but why isn't dofile supported?
"require" can take in the filename without the extension.
Lua supports several search directories in which "require" looks into, so it depends on the configuration.
If your script file is NOT located in one of those search directories you have to specify the path to the file (without the extension) using "." instead of "/".
The Lua manual probably has a better description. :)

Re: Question

Posted: Thu Oct 20, 2011 5:40 pm
by Robin
ivan wrote:stuff
Note LÖVE changes the require() mechanics a bit, because of the whole .love file/write directory thingy.

And using actual paths is not very useful on LÖVE, because you'll be limiting your game to certain computers without a good reason.

Also, SELÖVE only lets you require things in the .love and the write directory. Just sayin'. ;)

Re: Question

Posted: Fri Oct 21, 2011 5:35 pm
by Acnesium
Hello,

I got into a problem when trying out the quad images. It just doesn't draw the image. What is wrong?

main.lua

Code: Select all

require('pacman')

function love.load()
	
end

function love.update(dt)

end

function love.draw()

end
pacman.lua

Code: Select all

function love.load()
	local	p_Atlas = love.graphics.newImage('pacman-256x256.png')
	local	p_Left = love.graphics.newQuad(0, 0, 32, 32, 256, 256)

	local	x = 200
	local	y = 200
	local	s = 100
	local	scale = 1
end

function love.update(dt)
	if love.keyboard.isDown('right') then
		x = x + (s * dt)
	elseif love.keyboard.isDown('left') then
		x = x - (s * dt)
	end
	
	if love.keyboard.isDown('down') then
      y = y + (s * dt)
   elseif love.keyboard.isDown('up') then
      y = y - (s * dt)
   end
end

function love.draw()
	love.graphics.drawq(p_Atlas, p_Left, x, y)
end

Re: Question

Posted: Fri Oct 21, 2011 5:39 pm
by Robin
You define love.load, love.update and love.draw in pacman.lua, but then you overwrite them in main.lua. If you remove the empty functions in main.lua, it should work.

Re: Question

Posted: Fri Oct 21, 2011 5:44 pm
by Acnesium
Robin wrote:You define love.load, love.update and love.draw in pacman.lua, but then you overwrite them in main.lua. If you remove the empty functions in main.lua, it should work.
Thanks it worked!

But what if I want to add another file to main.lua and add love.load; love.update and love.draw in that file will it overwrite too?

Re: Question

Posted: Fri Oct 21, 2011 5:46 pm
by Jasoco
Of course! Don't put multiple love.***() functions. You're going about it wrong. Instead, put the love.***() functions in main.lua and call the other individual update, draw, load functions with different names, from the originals.