Page 1 of 1

(Solved) HUMP Class - attempt to index local '***' (a nil value)

Posted: Wed Oct 12, 2022 6:34 pm
by Bigfoot71
Hello everyone !

So I find myself facing a problem that I can't explain to myself, I started using HUMP to do my Classes and I get an error that I can't understand...

Here is the file that contains the class in question:

Code: Select all

Player = Class {

    init = function(self, img, pos)

        self.img = img
        self.pos = pos

        self.w, self.h = img:getDimensions()
        self.ox, self.oy = self.w / 2, self.h / 2

    end;

    draw = function(self)
        lg.draw(
            self.img,
            self.pos.x,
            self.pos.y,
            nil,
            nil,
            nil,
            self.ox,
            self.oy
        )
    end;

}

return Player
For line self.w, self.h = img:getDimensions() I get:

Code: Select all

Player.lua:8: attempt to index local 'img' (a nil value)
whereas img is indeed passed as a parameter because without that my image is displayed.

I "instantiate" it like this in another file:

Code: Select all

local path = ...

local Player = require(path.."/Player")

local player = Player()

local player_tex = lg.newImage(
    "assets/images/player.png"
)

-- State definition --

local game = {}

function game:enter()
    player:init(player_tex, Vec(display.size.w/2, display.size.h/2))
end

function game:update(dt)
    --player:update(dt)
end

function game:draw()
    player:draw()
end

return game
I also tried to define the "methods" outside the table as a parameter of Class but the result is exactly the same...

EDIT: I temporarily solved the problem by doing:

Code: Select all

if img ~= nil then
    self.w, self.h = img:getDimensions()
    self.ox, self.oy = self.w / 2, self.h / 2
end
But it's not very clean or very practical and I would like to know if you have a better solution

Thanks in advance !

Re: HUMP Class - attempt to index local '***' (a nil value)

Posted: Wed Oct 12, 2022 9:10 pm
by SelfDotX
I could be wrong (im new too) but I don't think you call init directly. If I had to guess it should look like:

Code: Select all

local player = Player(img, pos)
or

Code: Select all

local player = Player.new(img, pos)
Just a guess :awesome:

Re: HUMP Class - attempt to index local '***' (a nil value)

Posted: Thu Oct 13, 2022 6:24 am
by ReFreezed
SelfDotX is correct. Class libraries, including the one in HUMP, tend to support an 'init' method that gets called automatically when the object is first created (i.e. by the Player() call here).

Re: HUMP Class - attempt to index local '***' (a nil value)

Posted: Thu Oct 13, 2022 7:29 am
by Bigfoot71
I removed local player = Player() and changed player:init(x, x) to player = Player(x, x) but I always have the same error when I want to retrieve the dimensions of img (like what the parameter img would be nil whereas without the img:getDimensions() the image is displayed fine)

Re: HUMP Class - attempt to index local '***' (a nil value)

Posted: Thu Oct 13, 2022 7:51 am
by ReFreezed
Did you maybe misspell the image variable? The fixed code should be something like this:

Code: Select all

local player
local player_tex = lg.newImage("assets/images/player.png")
function game:enter()
    player = Player(player_tex, Vec(display.size.w/2, display.size.h/2))
end

Re: HUMP Class - attempt to index local '***' (a nil value)

Posted: Thu Oct 13, 2022 8:22 am
by Bigfoot71
Oh yes my apologies, I went too fast, I had forgotten a letter while rewriting, thank you very much ! ^^