Page 1 of 1

Can a script_entity call another? And how?

Posted: Wed Apr 15, 2015 4:24 pm
by mr0wizard
I have several "script_entity"s each called by a different "castle_wall_lever" and I would like to call another script_entity to act like a global function and maybe pass into this global function/"script_entity" a variable to be used to allow it to set things differently depending upon what is passed in. I obviously could have the same code in each of the "castle_wall_lever"s associated "script_entity"s but that would be harder to maintain. If this is possible can some one show me some sample code?

Re: Can a script_entity call another? And how?

Posted: Wed Apr 15, 2015 7:23 pm
by Isaac
The short example is this:

Code: Select all

myScript.script:myFunction()
From within a script, you can call another script's functions, by using the script_entity id, and its script component.
ie. myScript.script followed by either a period, or a colon, and then the function name.

Using the period calls the function with any arguments you put in the parenthesis.
Using the colon calls the function with the calling script [table] as the first argument, followed by any arguments you put in the parenthesis.
IE. Sending myScript.script:myFunction(my_argument) will be received as if sent like this...

Code: Select all

myScript.script.myFunction(self, my_argument)
*(Note use of the period instead of the colon)

A longer example is this:
Place it in a script object on the map, name it: myScript
and preview the output.

Code: Select all

--script named myScript
x = 1
print( x, self.go.id)
function myFunction(caller)
	x = x + 1
	print(x, caller.go.id)
end

myScript.script:myFunction()

myScript.script.myFunction(self)
* The variable caller is simply the first passed argument that the function gets, and is then visible in the function.

Re: Can a script_entity call another? And how?

Posted: Wed Apr 15, 2015 7:59 pm
by minmay
Isaac wrote:Using the colon calls the function with the calling script [table] as the first argument, followed by any arguments you put in the parenthesis.
IE. Sending myScript.script:myFunction(my_argument) will be received as if sent like this...

Code: Select all

myScript.script.myFunction(self, my_argument)
*(Note use of the period instead of the colon)
Err no it won't, it will be received as if sent like this:

Code: Select all

myScript.script.myFunction(myScript.script, my_argument)

Re: Can a script_entity call another? And how?

Posted: Wed Apr 15, 2015 8:45 pm
by Isaac
minmay wrote:
Isaac wrote:Using the colon calls the function with the calling script [table] as the first argument, followed by any arguments you put in the parenthesis.
IE. Sending myScript.script:myFunction(my_argument) will be received as if sent like this...

Code: Select all

myScript.script.myFunction(self, my_argument)
*(Note use of the period instead of the colon)
Err no it won't, it will be received as if sent like this:

Code: Select all

myScript.script.myFunction(myScript.script, my_argument)
I was waiting for that you know. ;)

Now, which side are you considering it from? The sender or the receiver?

If function received myscript.script as caller... Wouldn't caller.go.id be seen as caller.script.go.id?

(It's an honest question, as I've said before, I'm still a novice at scripting.)

**I did just try myScript.script.myFunction(self.script) and was surprised that it worked as well, but that seemed odd at first. Are there any practical ramifications from this? If self == myScript.script... then what exactly is the difference (or error), in stating it would be received as
myScript.script.myFunction(self, my_argument) ? (Also an honest question.)

Re: Can a script_entity call another? And how?

Posted: Wed Apr 15, 2015 9:02 pm
by mr0wizard
Thanks for the help. Also I can see where getting the calling functions ID into the called function my be of use in some cases. :D

Re: Can a script_entity call another? And how?

Posted: Wed Apr 15, 2015 9:15 pm
by minmay
Isaac wrote:
minmay wrote:
Isaac wrote:Using the colon calls the function with the calling script [table] as the first argument, followed by any arguments you put in the parenthesis.
IE. Sending myScript.script:myFunction(my_argument) will be received as if sent like this...

Code: Select all

myScript.script.myFunction(self, my_argument)
*(Note use of the period instead of the colon)
Err no it won't, it will be received as if sent like this:

Code: Select all

myScript.script.myFunction(myScript.script, my_argument)
I was waiting for that you know. ;)

Now, which side are you considering it from? The sender or the receiver?

If function received myscript.script as caller... Wouldn't caller.go.id be seen as caller.script.go.id?

(It's an honest question, as I've said before, I'm still a novice at scripting.)

**I did just try myScript.script.myFunction(self.script) and was surprised that it worked as well, but that seemed odd at first. Are there any practical ramifications from this? If self == myScript.script... then what exactly is the difference (or error), in stating it would be received as
myScript.script.myFunction(self, my_argument) ? (Also an honest question.)
"table:function(arg1)" is syntactic sugar for "table.function(table,arg1)". That's all it is. There's no other difference. You are overcomplicating things.

Re: Can a script_entity call another? And how?

Posted: Wed Apr 15, 2015 9:19 pm
by Isaac
minmay wrote:"table:function(arg1)" is syntactic sugar for "table.function(table,arg1)". That's all it is. There's no other difference. You are overcomplicating things.
8-)

In my first post, I was trying to simplify them.