Question

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.
Acnesium
Prole
Posts: 8
Joined: Sun Oct 16, 2011 12:17 pm

Question

Post 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.
User avatar
ivan
Party member
Posts: 1912
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Question

Post 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 )"
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Question

Post 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").
Acnesium
Prole
Posts: 8
Joined: Sun Oct 16, 2011 12:17 pm

Re: Question

Post by Acnesium »

Thanks, both of you!
User avatar
ivan
Party member
Posts: 1912
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Question

Post 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. :)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Question

Post 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'. ;)
Help us help you: attach a .love.
Acnesium
Prole
Posts: 8
Joined: Sun Oct 16, 2011 12:17 pm

Re: Question

Post 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
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Question

Post 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.
Help us help you: attach a .love.
Acnesium
Prole
Posts: 8
Joined: Sun Oct 16, 2011 12:17 pm

Re: Question

Post 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?
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Question

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

Who is online

Users browsing this forum: Google [Bot] and 1 guest