Optional parameters

About advanced scenes, and the Thyme scripting language used in Algodoo.

Optional parameters

Postby standardtoaster » Mon Oct 19, 2009 5:45 am

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.
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Optional parameters

Postby KarateBrot » Sat Oct 24, 2009 9:20 am

hmm, no replies until now. :? i find this question interesting.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Optional parameters

Postby standardtoaster » Tue Oct 27, 2009 1:44 am

I think that this might not be possible but I'll just wait for replies.
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Optional parameters

Postby KarateBrot » Tue Oct 27, 2009 4:24 am

i don't know if it's possible and i'm the opposite of an expert but maybe you could do something with void?
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Optional parameters

Postby standardtoaster » Tue Oct 27, 2009 5:51 am

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.
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Optional parameters

Postby gradyfitz » Tue Oct 27, 2009 9:06 am

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 :D.
Mechanisms: 18 Mechanisms.
Thyme: Tutorial - Variables/Commands List.
Thymechanic
gradyfitz
 
Posts: 174
Joined: Tue Sep 01, 2009 8:33 am
Location: Victoria, Australia

Re: Optional parameters

Postby KarateBrot » Tue Oct 27, 2009 12:59 pm

Wow. :shock:
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 3 guests

cron