Attempt to perform arithmetic on "color" 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
lushfoliage
Prole
Posts: 5
Joined: Mon May 18, 2020 4:40 pm

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

Post 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
hoistbypetard
Prole
Posts: 26
Joined: Fri May 22, 2020 7:00 pm

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

Post 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 : .
lushfoliage
Prole
Posts: 5
Joined: Mon May 18, 2020 4:40 pm

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

Post 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.
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
lushfoliage
Prole
Posts: 5
Joined: Mon May 18, 2020 4:40 pm

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

Post 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!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 195 guests