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.
Question
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Question
Yes, you can do this using "require ( filename )" or "dofile ( filename )"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?
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Question
Don't listen to that, it's wrong.ivan wrote: Yes, you can do this using "require ( filename )" or "dofile ( filename )"
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
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.
"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.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Question
Note LÖVE changes the require() mechanics a bit, because of the whole .love file/write directory thingy.ivan wrote:stuff
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.
Re: Question
Hello,
I got into a problem when trying out the quad images. It just doesn't draw the image. What is wrong?
main.lua
pacman.lua
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
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
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Question
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.
Re: Question
Thanks it worked!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.
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?
- Jasoco
- Inner party member
- Posts: 3727
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Question
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.
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 5 guests