Page 1 of 1
Passing Arguments to Functions between Scripts; Please Help
Posted: Sat Oct 13, 2012 5:51 am
by Lark
I could use some help with function calls. From SCRIPT-A, I want to locate SCRIPT-B and execute one of its functions while passing it an argument. When I do, I get a reference to a table and not the argument I passed. Are the arguments stored in a table? If so, how do I index the table? I've searched the docs and lots of posts, but I can't find what I need. For example:
SCRIPT-A:Code: Select all
remoteScript = findEntity("SCRIPT-B")
remoteScript:myRoutine(2)
SCRIPT-B:Code: Select all
function myRoutine(n)
print(n)
end
I would expect that "2" should print, but instead I get "table: 0x077105a8". What am I missing?
Thank you!
-Lark
Re: Passing Arguments to Functions between Scripts; Please H
Posted: Sat Oct 13, 2012 5:55 am
by Lmaoboat
I think all you have to do is SCRIPT-B.myRoutine(2)
Re: Passing Arguments to Functions between Scripts; Please H
Posted: Sat Oct 13, 2012 6:02 am
by Lark
Lmaoboat wrote:I think all you have to do is SCRIPT-B.myRoutine(2)
Rescued again! Thank you Lmaoboat! I've been stuck on this for hours and finally gave up and asked for help. Now I can finish my next big script and post it soon. I've already thought of a better way to rewrite it, but heck, I need to finish this version first. More soon!
Thank you!
-Lark
Re: Passing Arguments to Functions between Scripts; Please H
Posted: Sat Oct 13, 2012 6:05 am
by Lmaoboat
I believe you can also do the same thing to refer to other script's tables and variables.
Re: Passing Arguments to Functions between Scripts; Please H
Posted: Sat Oct 13, 2012 11:37 am
by Komag
This is something I don't know about, but looks important and useful, SUPERTHREADED!
Re: Passing Arguments to Functions between Scripts; Please H
Posted: Sat Oct 13, 2012 12:01 pm
by BeNNyBiLL
The colon (':') operator is used to pass in the self parameter with your other function parameters i.e:
remoteScript:myRoutine(2)
is the same as;
remoteScript.myRoutine(remoteScript, 2)
This is why you were getting a table with your result, as instead of getting a compilation error, lua just drops the last parameter as the function doesn't support more than 2 parameters. Deeper explanation found here
http://www.lua.org/pil/16.html (Also that whole book is really useful for beginners, I myself only started reading it yesterday as I have not used LUA before and have found it extremely helpful!)
Re: Passing Arguments to Functions between Scripts; Please H
Posted: Sun Oct 14, 2012 8:08 am
by Lark
BeNNyBiLL wrote:This is why you were getting a table with your result, as instead of getting a compilation error, lua just drops the last parameter as the function doesn't support more than 2 parameters. Deeper explanation found here
http://www.lua.org/pil/16.html
Fantastic! This makes perfect sense now, thank you. The table passed is the object invoking the function, so you could have three buttons, for example, invoke the same function in a script and the script could determine which of the three was pressed by looking at table.id. As it turns out, I need this. I've been disappointed that connections can't pass arguments to functions, but now there's no need - the arguments would be static anyway and just identifying the button (or other object) is good enough!
Thank you very much!
-Lark
Re: Passing Arguments to Functions between Scripts; Please H
Posted: Sun Oct 14, 2012 5:31 pm
by Cyrgaan.Hun
Hey, fellow Dungeon Makers!
I've tested this, and can easily pass more than 2 parameters just keep it in mind, that if you call a function, the first parameter will ALWAYS be the sender object, so the correct syntax:
("demo" is the name of the script_entity)
Code: Select all
function printThisOut(sender, pMessage1, pMessage2, pMessage3, pMessage4)
print(pMessage1, pMessage2, pMessage3, pMessage4)
end
and you have to call it simply not mentioning the sender:
Code: Select all
demo:printThisOut("One", "Two", "Three", "Four")
It will work properly!