Page 1 of 1

Attempt to perform arithmetic on "color" a nil value.

Posted: Fri May 29, 2020 6:02 pm
by lushfoliage
Have a Brick class that declares self.color = 1 in its init function, and however when I call self.color in the Brick's render function, I get the error that color is a nil value. The code works perfectly fine if I replace self.color in Brick:render() with a 1, and also if I put self.color = 1 in the beginning of the render function. I'd like to know what's up, and I believe I read somewhere that self variables were automatically global?

Code: Select all

function Brick:init(x, y)
    -- used for coloring and score calculation
    self.tier = 0
    self.color = 1

    self.x = x
    self.y = y
    self.width = 32
    self.height = 16

    -- used to determine whether this brick should be rendered
    self.inPlay = true
end

function Brick:render()
    if self.inPlay then
        love.graphics.draw(gTextures['main'],
            -- multiply color by 4 (-1) to get our color offset, then add tier to that
            -- to draw the correct tier and color brick onto the screen
            gFrames['bricks'][1 + ((self.color - 1) * 4) + self.tier],
            self.x, self.y)
    end
end
Thanks

Re: Attempt to perform arithmetic on "color" a nil value.

Posted: Fri May 29, 2020 6:24 pm
by hoistbypetard
Check to see if you're accidentally calling brick.render() instead of brick:render(). self isn't passed as an implicit first parameter with . the same way it is with : .

Re: Attempt to perform arithmetic on "color" a nil value.

Posted: Fri May 29, 2020 7:00 pm
by lushfoliage
Ahaha...turns out in some functions I had brick:render() instead of Brick:render(), however the bricks are all invisible now...I'll figure this out this whole coding business sooner or later.

Re: Attempt to perform arithmetic on "color" a nil value.

Posted: Fri May 29, 2020 7:32 pm
by pgimeno
lushfoliage wrote: Fri May 29, 2020 7:00 pm Ahaha...turns out in some functions I had brick:render() instead of Brick:render(),
That should be the correct thing to have. You don't call an instance method on a class (Brick), you call it on the instances (brick). That's the whole point of the self parameter and the colon syntax.

Re: Attempt to perform arithmetic on "color" a nil value.

Posted: Fri May 29, 2020 7:51 pm
by lushfoliage
pgimeno wrote: Fri May 29, 2020 7:32 pm
lushfoliage wrote: Fri May 29, 2020 7:00 pm Ahaha...turns out in some functions I had brick:render() instead of Brick:render(),
That should be the correct thing to have. You don't call an instance method on a class (Brick), you call it on the instances (brick). That's the whole point of the self parameter and the colon syntax.
Mhmm this is definitely correct. Not sure what I did but I went through all of my code and realized this must've been true since I haven't run into this problem before. Somehow I've got the bricks rendering all good though, I probably made a typo somewhere and didn't realize. Thank you for the help!