Can a script_entity call another? And how?

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
mr0wizard
Posts: 4
Joined: Sun Apr 12, 2015 8:04 pm

Can a script_entity call another? And how?

Post 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?
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: Can a script_entity call another? And how?

Post 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.
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Can a script_entity call another? And how?

Post 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)
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.
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: Can a script_entity call another? And how?

Post 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.)
Last edited by Isaac on Wed Apr 15, 2015 9:04 pm, edited 1 time in total.
mr0wizard
Posts: 4
Joined: Sun Apr 12, 2015 8:04 pm

Re: Can a script_entity call another? And how?

Post 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
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Can a script_entity call another? And how?

Post 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.
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.
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: Can a script_entity call another? And how?

Post 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.
Post Reply