5

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

This is my World file 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 is wrong? Anyway, what is the best way for make a class in Lua?

Jazzguy
  • 147
  • 1
  • 11

1 Answers1

5

In the world.lua file, put a return World at the end:

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

-- Add line below
return World
hjpotter92
  • 78,589
  • 36
  • 144
  • 183