Optional parameters
7 posts • Page 1 of 1
Optional parameters
Is it possible to make optional parameters in a script? I recently made my IsMouseInBox script and I have so that you can give it the color if true and the color if false. How can I make it so that I don't have to give a color and it will give it a default if none is given?
That's what I have now. Is it possible to make CIT and CIF optional? So that the user only needs to give P1 and P2 a value if they don't want to give a color? I hope you understand what I mean.
- Code: Select all
Scene.my.IsMouseInBox=(P1, P2, CIT, CIF) =>{App.mousePos(0) >= P1(0) && App.mousePos(1) <= P1(1) && App.mousePos(0) <= P2(0) && App.mousePos(1) >= P2(1) ? {CIT} : {CIF}}
That's what I have now. Is it possible to make CIT and CIF optional? So that the user only needs to give P1 and P2 a value if they don't want to give a color? I hope you understand what I mean.
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: Optional parameters
hmm, no replies until now. i find this question interesting.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Optional parameters
I think that this might not be possible but I'll just wait for replies.
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: Optional parameters
i don't know if it's possible and i'm the opposite of an expert but maybe you could do something with void?
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Optional parameters
That's a possibility. I haven't tried it yet. I did try and check if it was equal to "" by using math.toString. It didn't work. Thank you for a new option.
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: Optional parameters
standardtoaster wrote:Is it possible to make optional parameters in a script? I recently made my IsMouseInBox script and I have so that you can give it the color if true and the color if false. How can I make it so that I don't have to give a color and it will give it a default if none is given?
- Code: Select all
Scene.my.IsMouseInBox=(P1, P2, CIT, CIF) =>{App.mousePos(0) >= P1(0) && App.mousePos(1) <= P1(1) && App.mousePos(0) <= P2(0) && App.mousePos(1) >= P2(1) ? {CIT} : {CIF}}
That's what I have now. Is it possible to make CIT and CIF optional? So that the user only needs to give P1 and P2 a value if they don't want to give a color? I hope you understand what I mean.
This may be possible using arrays. Like:
- Code: Select all
Scene.my.IsMouseInBox=(P1P2CITCIF)=>{CIT := [0,0,1,1];CIF := [1,0,0,1];P1 := p1p2citcif(0);P2 := p1p2citcif(1);Math.toString((p1p2citcif ++ [["Pi"]])(2)(0)) == "Pi" ? {} : {CIT = p1p2citcif(2);CIF = p1p2citcif(3)};App.mousePos(0) >= P1(0) && App.mousePos(1) <= P1(1) && App.mousePos(0) <= P2(0) && App.mousePos(1) >= P2(1) ? {CIT} : {CIF}}
The input is like:
- Code: Select all
Scene.my.IsMouseInBox([[-10,10],[10,-10]])
or
- Code: Select all
Scene.my.IsMouseInBox([[-10,10],[10,-10],[1,1,1,1],[0,0,0,1]])
It works by checking the size of the array, then determining how many arguments have been sent. If you want to change the default colour change:
- Code: Select all
CIT := [0,0,1,1];CIF := [1,0,0,1];
to
- Code: Select all
CIT := <X>;CIF := <Y>;
Where <X>, the default colour if it is true and
where <Y>, the default colour if it is false.
Another way is to make a function which takes all your arguments, and delivers it to different functions based on the input, which is just as easy.
Hopefully this helped .
- gradyfitz
- Posts: 174
- Joined: Tue Sep 01, 2009 8:33 am
- Location: Victoria, Australia
7 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 3 guests