Arg passed is nil

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
ldmfield
Prole
Posts: 1
Joined: Sun Sep 17, 2017 4:34 pm

Arg passed is nil

Post by ldmfield »

Code: Select all

function love.keypressed(key,scancode,isrepeat)
    if isrepeat == false then
        print(key)
        input.HandleInput(key)
    end
end
I have the above code in my main.lua; at the top of main.lua I have required the input script.
When I press a button if its not a repeat then it prints the key to the console and then calls my HandleInput function. This works fine in main.lua. However in the input script:

Code: Select all

function input:HandleInput(key)     
    print(key)
    table.insert(input.keys,key)
end
This function prints the key argument as nil. I'm returning to love2D and lua after a very long time but I'm not quite sure why it would be getting a nil when I'm passing the key.
Hopefully someone can shed some light on this.
EDIT: As a consequence this means its also trying to insert nil into my table.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Arg passed is nil

Post by grump »

When you declare the function with a colon, you have to use colon syntax when calling the function:

Code: Select all

input:HandleInput(key)
If you use the dot syntax, the 'key' argument will be used as the 'self' table reference. Use the colon syntax with tables that will be instantiated, and the dot syntax for singular/static tables.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Arg passed is nil

Post by Beelz »

You don't have to, it's just syntactic sugar. You could've used dot notation with the first argument being itself:

Code: Select all

input.HandleInput(input, key)
Also inside the input function you're calling you can use the 'self' parameter because of the semicolon notation(or passing itself as 1st arg).

Code: Select all

table.insert(self.keys, key)

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Arg passed is nil

Post by grump »

Beelz wrote: Tue Sep 19, 2017 12:42 pm You don't have to, it's just syntactic sugar.
No, you don't have to. If you're insane or masochistic, you can do it the other way.
semicolon
No.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Arg passed is nil

Post by zorg »

Post demeanor matching the poster's nick. :3
Anyway, yeah, it's "colon' notation, and i wouldn't necessarily call people neither insane, nor masochistic to use either/or, just not mixed.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Arg passed is nil

Post by grump »

zorg wrote: Tue Sep 19, 2017 5:00 pm i wouldn't necessarily call people neither insane, nor masochistic to use either/or, just not mixed.
I'm gonna quote myself here:
When you declare the function with a colon, you have to use colon syntax when calling the function
In other words: When you define the function with a colon, but use dot notation when calling it, you're mixing notations. And that's bad, right? We're on the same page here.

If you insist on using only dot notation, and willingly type table identifiers twice all the time, then that's slightly less insane, but still pretty masochist. It's more code, thus more room for errors. That's bad.

Dot notation has its place, but if you can use colon instead to pass your instance reference, you should. Even if you're technically not required to.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Arg passed is nil

Post by Beelz »

grump wrote: Tue Sep 19, 2017 1:36 pm
Beelz wrote: Tue Sep 19, 2017 12:42 pmsemicolon
No.
Good catch... Been using Java a lot again lately. :death:

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Arg passed is nil

Post by grump »

Beelz wrote: Tue Sep 19, 2017 7:27 pm Been using Java a lot again lately. :death:
Oh, that's unfortunate. You have my deepest sympathy.

Not having to use semicolons is the greatest thing in Lua, but I always forget to put them when writing inline shader code for LÖVE. I'm not capable of writing shader code in Lua that compiles on the first attempt.
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests