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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

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

Post 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 !
Last edited by Bigfoot71 on Thu Oct 13, 2022 8:23 am, edited 1 time in total.
My avatar code for the curious :D V1, V2, V3.
User avatar
SelfDotX
Prole
Posts: 25
Joined: Sun Oct 02, 2022 5:06 pm

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

Post 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:
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

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

Post 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).
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

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

Post 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)
My avatar code for the curious :D V1, V2, V3.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

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

Post 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
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

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

Post by Bigfoot71 »

Oh yes my apologies, I went too fast, I had forgotten a letter while rewriting, thank you very much ! ^^
My avatar code for the curious :D V1, V2, V3.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests