SketchyPhysicsWiki
Advertisement

Tutorial[]

These scripts are used in a similar way to the beginner scripts, but their functions are a little more complicated; they are often more useful. After this tutorial you should be able to understand and write scripts that give advanced, dynamic behaviors to your SketchyPhysics models.


Local and global variables[]

SP global variables[]

They store information (Example: numbers), that you can then retrieve later on. They are actually SketchyPhysics functions that will always return a value where a simple variable might not, best used for joint controllers. To create one, use:


setVar("var",1)


This creates a global variable, called var, that holds a value of 1. To retrieve this variable (from any object or script field, anywhere in the model), use:


getVar("var")


Having already set var at 1, this would give us a value of 1 (big surprise). To set and retrieve a variable at the same time, use:


getSetVar("var",1)


Which will set var at 1 and give a value of 1 Even when a variable is not these will return a default value: 0.0, making them ideal for use in controllers.


Local variables[]

These only have a local reach(within the block they were created), best used for temporary calculations within a script, where using global variables might clash with other scripts, because of their straight forward use they are far quicker to write:

var=1

Simple local variables called var, set at 1. Retrieving local variables is easier than with global ones; they work something like letters in algebra, and can be slotted in almost anywhere:

var

Then you can combine them further:

pi=3.14159

r=300

result=(2*pi*r)+var


Global variables[]

Very similar use to local variables, but they are reached everywhere(even by SketchUp itself), however they do not hold a value before set so it's best NOT to use them in controllers.

$var=1

Very close to local variable use, only added the $ sign to tell it's now global, everything else is done the same way.

IMPORTANT: Because their reach extends to Sketchup these can not only clash with your variables but every other script/plugin, so be sure your names are unique.


Multiple variable script[]

Instead of using many different boxes for the same job (For example, switching landing gear up and down) it can all be combined into one script:


gear1 add to the servo it's connected to: getSetVar('gear1')

gear2 add to the servo it's connected to: getSetVar('gear2')

gear3 add to the servo it's connected to: getSetVar('gear3')


(This is what each gear is called for simplicity) In the 'onTouch' box add this script to one of the gears:

setVar('gear1',1)

That will then activate the first gear. Now combine the second part of the script to add the second gear:

setVar('gear1',1)+setVar('gear2',1)

(This is the script to set off two sets of landing gear)


Now just add the last gear and all 3 gears will come down when the 1 box is touched:

setVar('gear1',1)+setVar('gear2',1)+setVar('gear3',1)

Have a play around with it and try it.


IF functions[]

Probably the most useful function for SketchyPhysic's purposes. This extremely adaptable script function effectively asks a question, about what's happening with inputs, the frame number, and variables, and returns an 'answer,' which can itself be a variable, number, or even a self-contained formula with other inputs. It's written:


if (this is true?) then; (do this); else; (do this); end


Now, scripts aren't intellegent. You can't ask it whether you're fat, or if your house looks nice... no, because it'll give a big ol' syntax error. You have to ask something it will 'understand,' with numbers and variables and functions that are written properly. So, let's find out if 5 is bigger than 4:


if 5>4 then; 1; else; 0; end


Obviously 5 is greater than 4, so this will return a 1. Now, let's do something that could be useful! Say we want to move a joint to a random position... BUT, we want it to stay there. Try just using the rand command by itself. Not pretty, huh? The 'if' function can help! It's important to realize that the physics simulation isn't a continuous flow, it runs in frames; each frame is a still image, projected onto your screen. You see movement because the physics engine calculates where each object should be in the next frame, and moves it there. The script isn't exempt from this; to move an object that's connected to a script-controlled joint, the script must be run every frame, to find a single, numeric output which is used as the joint's controller. This means that, by just using the rand command, it is run every frame, resulting in a different value every time, and a wildly vibrating joint. So, let's get on with it:


if frame==0 then; setVar("foo",rand); else; getVar("foo"); end


Luckily, the simulation has a 'local' variable (It can be used by any script), updated every frame, called frame, that tells us which frame the simulation is currently at. Frame 0 is the first frame of the simulation, so using this 'if' function, you can create 'default' variables right at the beginning of each simulation. In this case, foo is set to a random value, but will stay that way for the remainder of the simulation (or at least until foo is changed). If you have SP3 X (Which is recommended for this tutorial, and above), you can put all your if functions into 'onTick,' on multiple lines:


if frame==0

setVar("foo",rand)

setVar("fee",10)

setVar("foe",0)

end


Notice that in the example above, I didn't use the ';'. That's because, on a single line, ';' creates a virtual line break, letting you perform a number of functions. With multiple line input opened up, however, they aren't strictly necessary. Also, notice how I didn't include 'then' or 'else'; 'then' is never needed, and basically just makes the script easier on the eye. If you only want the script to do something when the argument is true, don't include 'else'. Since 'onTick' doesn't directly control anything, it wasn't needed in the example. Now for some other useful add-ons:


if-and

This lets you only do something if two or more arguments are all true at the same time. Say you want to change a variable when two buttons are held down:


if joybutton("a")==1 and joybutton("x")==1

setVar("foo",1)

end


and can also be written &&:


if joybutton("a")==1 && joybutton("x")==1

setVar("foo",1)

end


And you can put in as many ands as you like!


if-or

This is for when you want two or more things to trigger the same output:


if joybutton("a")==1 or joybutton("x")==1

setVar("foo",1)

end


In the example, the A button and the X button both do the same thing. Neat, huh? You can also use as many ors as you like, and even use it in combination with and!


if-elsif

This will do something if the argument is true, but will make another argument if the first is false. In this example, if foo is bigger that 5, it is set back down to 1, but if it's lower than 1, it's set back up to 5:


if getVar("foo")>5

setVar("foo",1)

elsif getVar("foo")<1

setVar("foo",5)

end


W.I.P


... Here are some simple descriptions of a few 'if' commands.


  • if —means if. Very simple concept to grasp.


  • >=, <=, == —are bigger than, smaller than, and equal to.


  • then —when placed in a script, it is used to say that if what's before it is true, then do what's after it.

if getVar("foo")==1 then 0.5;end;


  • or —when placed in a script, it will do something if one, another, or both of multiple things happen.

if key("up")==1 or key("down")==1 then setVar("foo",1);else setVar("foo",0);end;


  • and —means that if the previous thing is true and the next, then do something, or do something and something else.

if key("up")+key("down")==1 and key("left")==0 then setVar("foo",1);elsif key("left")==1 then setVar("foo",0.5);else setVar("foo",0);end; if key("space")==1 then setVar("foo",1) and getVar("foo2");end;


  • ;else —means that if the first thing isn't done, do another thing.

if key("space")==1 then setVar("foo",1);else setVar("foo,0");end;


  • ;elsif —means else if, and is basically used to separate the function into 2 parts.

if getVar("foo")>=5 then setVar("go",1);elsif getVar("foo")<=4 then getSetVar("go",0);end;


  • ; —a semicolon can be used in the place of then at any time, or and on certain occasions.

if getVar("foo")==0;1;elsif getVar("foo")==0.5;setVar("foo2",1);setVar("foo3",0);end;

  • && —can be used instead of and.
  • ;end; —ends the function.
Advertisement