Can a script_entity call another? And how?
Can a script_entity call another? And how?
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?
The short example is this:
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...
*(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.
* The variable caller is simply the first passed argument that the function gets, and is then visible in the function.
Code: Select all
myScript.script:myFunction()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)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)Re: Can a script_entity call another? And how?
Err no it won't, it will be received as if sent like this: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...*(Note use of the period instead of the colon)Code: Select all
myScript.script.myFunction(self, my_argument)
Code: Select all
myScript.script.myFunction(myScript.script, my_argument)Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
Re: Can a script_entity call another? And how?
I was waiting for that you know.minmay wrote:Err no it won't, it will be received as if sent like this: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...*(Note use of the period instead of the colon)Code: Select all
myScript.script.myFunction(self, my_argument)Code: Select all
myScript.script.myFunction(myScript.script, my_argument)
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.)
Last edited by Isaac on Wed Apr 15, 2015 9:04 pm, edited 1 time in total.
Re: Can a script_entity call another? And how?
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. 
Re: Can a script_entity call another? And how?
"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.Isaac wrote:I was waiting for that you know.minmay wrote:Err no it won't, it will be received as if sent like this: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...*(Note use of the period instead of the colon)Code: Select all
myScript.script.myFunction(self, my_argument)Code: Select all
myScript.script.myFunction(myScript.script, my_argument)
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.)
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
Re: Can a script_entity call another? And how?
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.
In my first post, I was trying to simplify them.