Lua error to create class

I am trying to create a class for my game and I got this error (shown in love2d): try to index upvalue 'World' (boolean)

This is my file in the world that I made:

local World = {}
World.__index = World

function World:new(meter, gravity)
    setmetatable({}, World)

    -- Set physics parameters
    love.physics.setMeter(meter)
    self.world = love.physics.newWorld(0, gravity*meter, true)

    -- Load background
    self.background = love.graphics.newImage("imgs/background.png")

    return self
end

function World:update(dt)

end

function World:draw()
    love.graphics.draw(self.background)
end

function World:destroy()
    -- Destroy the world
    self.world:destroy()
end

And here I call the world:

local World = require("world")

function love.load()
    -- Build the world
    world = World:new(32, 9.81)
end

What's wrong? Anyway, what's the best way to make a class in Lua?

+4
source share
1 answer

In the file, world.luaput return Worldat the end:

.
.
.
function World:destroy()
    -- Destroy the world
    self.world:destroy()
end

-- Add line below
return World
+4
source

Source: https://habr.com/ru/post/1628008/


All Articles