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?
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
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 : .
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.
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 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!