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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
nibodhs
Prole
Posts: 1
Joined: Thu Mar 09, 2023 8:53 pm

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

Post 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.
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

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

Post 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)
Last edited by Andlac028 on Fri Mar 10, 2023 1:04 pm, edited 1 time in total.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

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

Post 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 ^^
My avatar code for the curious :D V1, V2, V3.
User avatar
Azzla
Prole
Posts: 43
Joined: Sun Mar 29, 2020 2:23 am

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

Post 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! :)
libraries: Stalkpile
electronic music stuff: https://soundcloud.com/azzlamusic
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest