Reputation: 569
My project structure is following:
--Shooter
----sprites
------background.png
------player.png
------zombie.png
----units
------player.lua
------zombie.lua
----main.lua
----load.lua
----update.lua
----draw.lua
And my main.lua
will have the following:
local Bullet = require("units.bullet")
local Player = require("units.player")
local Zombie = require("units.zombie")
require("load")
require("update")
require("draw")
require("functions")
love.window.setTitle("Shooter")
function love.load()
Load()
end
function love.update(dt)
Update(dt)
end
function love.draw()
Draw()
end
For example, bullet.lua
:
local Bullet = {}
bulletSprite = love.graphics.newImage('sprites/bullet.png')
function Bullet.create()
local newBullet = {
pos = {},
speed = 10,
sprite = bulletSprite,
direction = 0,
dead = false
}
return setmetatable(newBullet, {__index = Bullet})
end
function Bullet:setPos(x, y)
self.pos.x = x
self.pos.y = y
end
function Bullet:setDirection(angle)
self.direction = angle
end
function Bullet:move(dt)
local distance = self.speed * dt * 60
self.pos.x = self.pos.x + math.cos(self.direction) * distance
self.pos.y = self.pos.y + math.sin(self.direction) * distance
end
function spawnBullet(bullets, player)
local newBullet = Bullet.create()
newBullet:setPos(player.pos.x, player.pos.y)
newBullet.direction = player.angle
table.insert(bullets, newBullet)
end
The problem is that none of the files in units
folder will load properly. From the error log I can see that it tries to search for bullet.lua
in the root directory and then various love2d
and lua
libraries.
I have tried various things like require("./units/bullet")
or replace /
with .
but so far, no luck.
Extracting those files from the units
folder into root folder will work. Loading images from sprites
folder works, however (for example love.graphics.draw(sprites.background, 0, 0)
).
Any help?
Upvotes: 1
Views: 348
Reputation: 569
I found out the problem was that while my main.lua
had correct require
s, my update
file didn't.
Upvotes: 1
Reputation: 964
Two things come to mind.
return Bullet
, return Player
and so on... into each module you have createdUpvotes: 0