Page 1 of 1

bad argument #2 to 'newSource' (string expected, got no value)

Posted: Mon May 18, 2020 5:37 pm
by lushfoliage
Hello, I've been taking Harvard's free game design course so I'm not particularly well-versed with lua or Love2D. When creating a global table for all of my sounds, I received this error:

bad argument #2 to 'newSource' (string expected, get no value)
I went as far as to copy and paste the code found in the course's GitHub link for the main.lua file, but I received the same error. When I hover over the gSounds in my code, each of the keys(?) shows a ['name']: any, excluding ['paddle-hit']: table, which sometimes is a ['paddle-hit']: boolean instead.
I'm only in breakout-0
https://github.com/games50/breakout/tre ... /breakout0

gSounds table:

Code: Select all

    gSounds = {
        ['paddle-hit'] = love.audio.newSource('sounds/paddle_hit.wav'),
        ['score'] = love.audio.newSource('sounds/score.wav'),
        ['wall-hit'] = love.audio.newSource('sounds/wall_hit.wav'),
        ['confirm'] = love.audio.newSource('sounds/confirm.wav'),
        ['select'] = love.audio.newSource('sounds/select.wav'),
        ['no-select'] = love.audio.newSource('sounds/no_select.wav'),
        ['brick-hit-1'] = love.audio.newSource('sounds/brick-hit-1.wav'),
        ['brick-hit-2'] = love.audio.newSource('sounds/brick-hit-2.wav'),
        ['hurt'] = love.audio.newSource('sounds/hurt.wav'),
        ['victory'] = love.audio.newSource('sounds/victory.wav'),
        ['recover'] = love.audio.newSource('sounds/recover.wav'),
        ['high-score'] = love.audio.newSource('sounds/high_score.wav'),
        ['pause'] = love.audio.newSource('sounds/pause.wav'),

        ['music'] = love.audio.newSource('sounds/music.wav')
    }
StartState where it's used

Code: Select all

local highlighted = 1

function StartState:update(dt)
    -- toggle highlighted option if we press an arrow key up or down
    if love.keyboard.wasPressed('up') or love.keyboard.wasPressed('down') then
        highlighted = highlighted == 1 and 2 or 1
        gSounds['paddle-hit']:play()
    end

    -- we no longer have this globally, so include here
    if love.keyboard.wasPressed('escape') then
        love.event.quit()
    end
end
and the sound file is just a paddle_hit.wav in a sounds folder. Any idea what I've messed up?

Re: bad argument #2 to 'newSource' (string expected, got no value)

Posted: Mon May 18, 2020 5:43 pm
by MrFariator
In the past, love.audio.newSource allowed the second parameter, type, to be optionally omitted, and default to "stream". Nowdays you have to specify the type of the source, so all you need to do is

Code: Select all

gSounds = {
  ['paddle-hit'] = love.audio.newSource('sounds/paddle_hit.wav', 'static')
  -- or
  ['paddle-hit'] = love.audio.newSource('sounds/paddle_hit.wav', 'stream')
  -- ... the rest
 }
For sound effects, it's probably preferable to use 'static'.

Re: bad argument #2 to 'newSource' (string expected, got no value)

Posted: Mon May 18, 2020 5:46 pm
by lushfoliage
thank you!

Re: bad argument #2 to 'newSource' (string expected, got no value)

Posted: Tue May 19, 2020 1:03 am
by zorg
Also, because you will most probably run into it, 11.3 uses the 0-1 color range now instead of what the 0.10.something version most CS50 courses use, which used the 0-255 range... contemplation regarding a pinned post stating all this to people coming from the harvard CS50 course rising, although ultimately it might be pointless.