Passing Arguments to Functions between Scripts; Please Help

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
Lark
Posts: 178
Joined: Wed Sep 19, 2012 4:23 pm
Location: Springfield, MO USA

Passing Arguments to Functions between Scripts; Please Help

Post 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
Lmaoboat
Posts: 359
Joined: Wed Apr 11, 2012 8:55 pm

Re: Passing Arguments to Functions between Scripts; Please H

Post by Lmaoboat »

I think all you have to do is SCRIPT-B.myRoutine(2)
User avatar
Lark
Posts: 178
Joined: Wed Sep 19, 2012 4:23 pm
Location: Springfield, MO USA

Re: Passing Arguments to Functions between Scripts; Please H

Post 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 :D
Lmaoboat
Posts: 359
Joined: Wed Apr 11, 2012 8:55 pm

Re: Passing Arguments to Functions between Scripts; Please H

Post by Lmaoboat »

I believe you can also do the same thing to refer to other script's tables and variables.
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Passing Arguments to Functions between Scripts; Please H

Post by Komag »

This is something I don't know about, but looks important and useful, SUPERTHREADED!
Finished Dungeons - complete mods to play
BeNNyBiLL
Posts: 40
Joined: Mon Oct 08, 2012 11:00 pm

Re: Passing Arguments to Functions between Scripts; Please H

Post 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!)
User avatar
Lark
Posts: 178
Joined: Wed Sep 19, 2012 4:23 pm
Location: Springfield, MO USA

Re: Passing Arguments to Functions between Scripts; Please H

Post 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
Cyrgaan.Hun
Posts: 1
Joined: Mon Oct 08, 2012 9:40 pm

Re: Passing Arguments to Functions between Scripts; Please H

Post 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! :D
Post Reply