Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are Lua Function Parameters handed over?

I'm using lua for a project and now I want to use some functions of another class in another file. But it seems not working right for me. The first parameter is not getting handed over but I dont know why.

File index.lsp:

dbc = dofile("/home/mako/www/.lua/DBC.lua")
dbc.connectDevice(name, id, ptid)

DBC.lua:

function DBC:connectDevice(name, id, ptid)
    trace(name)
    trace(id)
    trace(ptid)
end

on the trace I get printed:

1
72893789(ptid)
nil

instead of

ESP
1
72893789

aswell when I change the order of the parameters it is like an offset as seen next

index.lsp:

dbc.connectDevice(id, name, ptid)

DBC.lua

function DBC:connectDevice(id, name, ptid)
    trace(name)
    trace(id)
    trace(ptid)
end

on the trace I get printed:

ESP
72893789(ptid)
nil

instead of

1
ESP
72893789

Could somebody maybe explain it to me what I have to do or am I making any mistake?

like image 309
prix Avatar asked Nov 29 '25 15:11

prix


1 Answers

Remember that in Lua, the syntax function DBC:connectDevice(name, id, ptid) ... end is just shorthand for the following code:

function DBC.connectDevice(self, name, id, ptid)
  ...
end

Note the implicit self parameter as the first function argument. This means that when you call the function, you must pass the object as the first argument, like this:

DBC.connectDevice(DBC, name, id, ptid)

Or, you can use the colon syntax again, to pass the object implicitly:

DBC:connectDevice(name, id, ptid)
like image 73
Paul Belanger Avatar answered Dec 02 '25 03:12

Paul Belanger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!