Page 1 of 1

Error main.lua:29: attempt to index global 'mole' (a nil value) Traceback [love "callbacks.lua"]:228: in function 'h

Posted: Thu Mar 09, 2023 8:59 pm
by nibodhs
Hello there, I was following Challacade's tutorial and tried editing the target with an image. I have the cursor completed yet there is an error message:

Code: Select all

function love.load()
    love.mouse.setVisible(false) 
    mouseImg = love.graphics.newImage("mouse.png") 

    moleImg = love.graphics.newImage("mole.png")

    gameFont = love.graphics.newFont("karma future.ttf", 45)

    score = 0
 end

function love.update(dt)
    
end


function love.draw()
    love.graphics.setFont(gameFont) 

    mole = love.graphics.draw(moleImg, 100 ,500)

    x, y = love.mouse.getPosition() 
    love.graphics.draw(mouseImg, x, y) 

    love.graphics.print("Score: "..score)
end

function love.mousepressed(x, y, button, istouch)
    size = mole.getWidth() * mole.getHeight()
    distance = distanceBetween(x, y, mole.getX(), mole.getY())
    if button == 1 then
        if distance < a then
            score = score + 1
        end
    end
end

function distanceBetween(x1, x2, y1, y2)
    return math.sqrt((y2 - y1)^2 + (x2 - x1)^2)
end
Please tell me if anything is wrong as I am quite new to Love.

Re: Error main.lua:29: attempt to index global 'mole' (a nil value) Traceback [love "callbacks.lua"]:228: in functio

Posted: Fri Mar 10, 2023 5:20 am
by Andlac028
It is :getHeight, not .getHeight (same for width)

: is used when you want to get something about the object you created, . when you just want to get functions from table (like love.graphics.print)

Re: Error main.lua:29: attempt to index global 'mole' (a nil value) Traceback [love "callbacks.lua"]:228: in functio

Posted: Fri Mar 10, 2023 7:18 am
by pgimeno
Also, love.graphics.draw doesn't return anything, that's why you get a nil value.

Also, getX() is not a method of any image. You have to keep track of where you draw it yourself, and use that.

Re: Error main.lua:29: attempt to index global 'mole' (a nil value) Traceback [love "callbacks.lua"]:228: in functio

Posted: Fri Mar 10, 2023 6:13 pm
by Bigfoot71
Small example that can help to understand the use of the symbol ":"

Code: Select all

tbl = { text = "Hello" }

function tbl:print()
    print(self.text)     -- "self" being the table "tbl"
end

tbl:print()        -- return "Hello"
tbl.print(tbl)     -- return "Hello"
tbl.print()        -- error: `self` is a nil value (undefined)
So exactly as Andlac said it is used to obtain a value related to the table, I repeat it with an example because it may not be obvious enough when you start ^^

Re: Error main.lua:29: attempt to index global 'mole' (a nil value) Traceback [love "callbacks.lua"]:228: in functio

Posted: Fri Mar 10, 2023 7:29 pm
by Azzla
As you don't have any update logic yet, mole will always be wherever you spawn it, in this case 100, 500. Since you are new to love, one simple but effective suggestion I have is that hard-coded integers are usually not going to be helpful down the road.

Consider something like the following:

Code: Select all

--keep track of all the properties you need in one table
local mole = {}
mole.x = 100
mole.y = 500
mole.sprite = love.graphics.newImage('mole.png')
mole.width = mole.sprite:getWidth()
mole.height = mole.sprite:getHeight()

function love.draw()
	love.graphics.draw(mole.sprite, mole.x, mole.y)
end

function love.mousepressed(x, y, button)
	if not button == 1 then return end --guard clause comes first
	
	distance = distanceBetween(x, y, mole.x, mole.y)
	if distance < threshold then --'threshold' could be whatever you decide is right
		score = score + 1
	end
end
Now you can update the mole.x/mole.y in love.update(dt), and the draw function will automatically reflect those changes. Also note that in callback functions like love.mousepressed its best practice to check exit conditions before anything else (like distanceBetween) so as not to waste function calls. Good luck! :)