luacs50love2d

Lua Love2D, CS50's Introduction to Game Development, knife library Timer.tween syntax


I'm working through the CS50 intro to game dev course and I'm having a syntax error I've been beating my head against.

I'm trying to write a function using the knife timer library that will be called every update to check the current alpha to see if it is at it max or min value and if so tween it to the opposite to create a pulsing effect. However I'm getting a syntax error that I can't solve. I've looked at the examples from the lecture, the repo provided for the project, and the knife timer library readme and been unable to find how my syntax varies from any of them.

Function below

function Tile:shimmer()
    if self.alpha == SHINYHIGHESTALPHA then
        Timer.tween(1, {
        [self] = {self.alpha = SHINYLOWESTALPHA}
        })
    elseif self.alpha == SHINYLOWESTALPHA then
        Timer.tween(1, {
        [self] = {self.alpha = SHINYHIGHESTALPHA}
        })
    end
end

and the error I get, line 41 is [self] = {self.alpha = SHINYLOWESTALPHA}

Error

Syntax error: src/Tile.lua:41: '}' expected near '='



Traceback

[love "callbacks.lua"]:228: in function 'handler'
[C]: at 0x7ffeef872fa0
[C]: in function 'require'
src/Dependencies.lua:35: in main chunk
[C]: in function 'require'
main.lua:34: in main chunk
[C]: in function 'require'
[C]: in function 'xpcall'
[C]: in function 'xpcall'


Solution

  • Try this instead:

    function Tile:shimmer()
        if self.alpha == SHINYHIGHESTALPHA then
            Timer.tween(1, {
            [self] = {alpha = SHINYLOWESTALPHA}
            })
        elseif self.alpha == SHINYLOWESTALPHA then
            Timer.tween(1, {
            [self] = {alpha = SHINYHIGHESTALPHA}
            })
        end
    end
    

    You're not supposed to put self. prefixes inside of table constructors.