Page 1 of 1

Hello im new to love and having some issues with nil value error

Posted: Wed Jan 20, 2016 10:30 pm
by invisableevil
im trying to fallow this tut and everything is written the same but for some reason his will work in the video and mine ends up with nil value errors left and right

this is the code i am working with

Code: Select all

local Renderer = require "tools/renderer"

renderer = Renderer:create()

function createBox(x,y)
    local b ={}
    
    b.x = x or 0
    b.y = y or 0
    
    function b:load()
        renderer:addRenderer(self,1)
    end
    
    function b:draw()
        love.graphics.setColor(math.random(0,255),math.random(0,255),math.random(0,255))
        love.graphics.rectangle("fill",self.x,self.y,64,64)
    end
    
    return b
end

function love.load()
    rl = createBox(64,64)
    r2 = createBox(96,96)
    
    r1:load()
    r2:load()
end

function love.update(dt)

end

function love.draw()
    renderer:draw()
end
the error seems to be with the r1:load

Re: Hello im new to love and having some issues with nil value error

Posted: Thu Jan 21, 2016 2:50 am
by zorg
"Help us help you", post a .love file and/or post the complete error message you're getting; currently i can only say that the error might be in the renderer's lua file.

Then again, i just spotted a potential error. Okay, so a long shot, but either one or the other might be the right solution:

Code: Select all

function b:load()
        renderer.addRenderer(self,1)
    end
or

Code: Select all

function b:load()
        renderer:addRenderer(1)
    end
The difference is that the first passes b as the self parameter, while the other passes renderer as self.
Colon syntax can be misleading when defining functions; they are less so when calling them.

Re: Hello im new to love and having some issues with nil value error

Posted: Thu Jan 21, 2016 5:59 am
by invisableevil
"Help us help you", post a .love file and/or post the complete error message you're getting; currently i can only say that the error might be in the renderer's lua file.
this is the error

Code: Select all

error

main.lua:27: attempt to index global 'r1' (a nil value)

traceback

main.lua:27: in function 'load'
[C]: infunction 'xpcall'
i dont understand why its doing this

Re: Hello im new to love and having some issues with nil value error

Posted: Thu Jan 21, 2016 9:46 am
by arampl
A typo? You use rl then r1.

Re: Hello im new to love and having some issues with nil value error

Posted: Thu Jan 21, 2016 5:31 pm
by invisableevil
arampl wrote:A typo? You use rl then r1.
ignore my last reply u were very right thank you they just look the same in visual studio

Re: Hello im new to love and having some issues with nil value error

Posted: Thu Jan 21, 2016 5:57 pm
by Nixola
I only approved this message (not the one you say to ignore), but I mistakenly flagged it as "advertising a product" ^^'
Sorry about that and don't worry, I don't think there will be consequences for that.
Anyway, bartbes, is this a bug? He had two posts already, yet I had to approve his messages