SketchyPhysicsWiki
Register
Advertisement

About

Ruby?!

No, we're not giving you jewels! Ruby is an easy-to-learn programming language that can be used to create custom control scripts for Sketchyphysics. From basic formulas to a scripted differential, this lets you do what you want with the controllable joints, motors, emitters and magnets at your disposal. If you've used Ruby before, scroll down for some commands specific to SP.

Ruby Tutorials by experience

NOTE: Because of various specific commands, like the creation and retrieval of variables, these tutorials are not appropriate for the teaching of 'normal' Ruby script.

  • Beginner, always worth a look
  • Intermediate, when you're comfortable with the basics
  • Advanced, for more experienced users who want to do amazing things with Ruby Script!

Ruby operators

* multiplication
/ division
^ power
+ plus
- minus
< Less than
> Greater than
== Equal to


Code

Ready-to-use scripts

General code

Random values

rand

Gives a random decimal value between 0 and 1.


rand(10)

Gives a random whole number between 0 and the specified number.

SP Specific code

Variables

setVar("var",1)

getVar("var")

getSetVar("var,1")


Joystick input

joy("leftx")

joy("lefty")

joy("rightx")

joy("righty")


joyRY (Analogue triggers)


Keyboard input

key("q,w,e,r,t,y,space,shift,tab,etc")


Joypad button input

joyButton("a,b,x,y,rb,lb")


Control panel sliders

slider("your_slider")


Oscillator

oscillator("50")

The number dicates the number of frames it takes to move back and forth.


SP3 X commands

lookAt("object")

push(1) (Acts something like a thruster)

playsound("sound.wav") plays a .wav file put in the sketchyphysics3 folder's subfolder: sounds.


Note:

  • w,a,s,d are also inupts for leftx and lefty, and the arrow keys for rightx and righty.
  • Buttons and keys use joystick emulation; up is 0 and down is 1.

Functions

if function

  • 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 for and. Most users don't use this but some do so it's important you know about this and the semicolon.


  • ;end; —ends the function.
Advertisement