Sorry
Sorry

Reputation: 569

Loading modules from folders

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

Answers (2)

Sorry
Sorry

Reputation: 569

I found out the problem was that while my main.lua had correct requires, my update file didn't.

Upvotes: 1

wsha
wsha

Reputation: 964

Two things come to mind.

  1. Make Sure LUA_PATH is set to your project directory
  2. add return Bullet, return Player and so on... into each module you have created

Upvotes: 0

Related Questions