Page 1 of 1

Arg passed is nil

Posted: Sun Sep 17, 2017 4:45 pm
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.

Re: Arg passed is nil

Posted: Sun Sep 17, 2017 5:14 pm
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.

Re: Arg passed is nil

Posted: Tue Sep 19, 2017 12:42 pm
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)

Re: Arg passed is nil

Posted: Tue Sep 19, 2017 1:36 pm
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.

Re: Arg passed is nil

Posted: Tue Sep 19, 2017 5:00 pm
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.

Re: Arg passed is nil

Posted: Tue Sep 19, 2017 5:51 pm
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.

Re: Arg passed is nil

Posted: Tue Sep 19, 2017 7:27 pm
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:

Re: Arg passed is nil

Posted: Tue Sep 19, 2017 8:57 pm
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.