Skip to content

GlovePIE Scripts and Syntax

Ravbug edited this page Jan 23, 2019 · 8 revisions

GlovePIE Scripts

Here is the syntax for GlovePIE scripts:

Commands and other Statements

Comments

Comments are text which does nothing. They only help the human who is trying to read your script (usually you). The computer ignores them.

Comments can have the following syntax:

// This is a comment

/* This is a multi-line comment  
   It goes over multiple lines  
   like this  */    

’ This is a BASIC style comment REM This is also a BASIC style comment

% This is a MATLAB style comment

Assignment statements

The most common kind of statement in GlovePIE is the assignment statement.

It has one of the following forward syntaxes:

LValue = expression [ ; ]

LValue := expression [ ; ]

or the following backwards syntax:

expression => LValue [ ; ]

These statements all set the LValue to the value of the expression. If it is inside an IF statement then it will always set the value. If it is not inside an IF statement then it will ONLY set the LValue when the expression changes. This means you can set a variable, such as a key, several times:

Ctrl = var.a Ctrl = var.b

Which has a similar effect to: Ctrl = var.a or var.b

The expressions can be any complicated mathematics that you want, or just another value.

Mathematical assignment statements

If you want to add something to a value, you have always been able to say: LValue = LValue + expression

But now you can write the same thing a shorter way, like in C and Java:

LValue += expression

Both ways change LValue by adding expression onto it. There are also other ways to increase a value, see the “Increment statements” section below.

You can also do subtraction, multiplication, or division by using -=, *= or /=

IF statements

Sometimes you will want to only do a set of commands when some condition is true.

You can do that with IF statements.

If statements can have one of the following syntaxes:

if condition then statement [ ; ]

if ( condition ) [then] statement [ ; ]

if condition [then] [begin] [ { ]   
  statement [ ; ]    
  statement [ ; ]    
  ...  `  
[ } ] [end [if]] [ ; ]  
if condition [then] [begin] [ { ]    
  statement [ ; ]    
  statement [ ; ]    
  ...   
[ } ] [end] else [begin] [ { ]  
  statement [ ; ]    
  statement [ ; ]    
  ...    
[ } ] [end [if]] [ ; ]   
if condition [then] [begin] [ { ]    
  statement [ ; ]   
  statement [ ; ]  
  ...    
[ } ] [end] (else if|elseif) condition2 [then] [begin] [ { ]    
  statement [ ; ]   
  statement [ ; ]    
  ...    
[ } ] [end] (else if|elseif) condition3 [then] [begin] [ { ]    
  statement [ ; ]    
  statement [ ; ]    
  ...    
[ } ] [end] else [begin] [ { ]   
  statement [ ; ]    
  statement [ ; ]    
   ...    
[ } ] [end [if]] [ ; ]  

WHILE loops

While loops are NOT very useful in PIE because PIE is not a linear language. A PIE script continuously loops through the entire script, even while IF statements are running in the background. If you think you need a while loop then you are probably looking at the problem the wrong way.

Nevertheless, PIE does support while loops in the unlikely event that you do need them.

The entire while loop will be executed in one go. You can't use it to wait for some condition triggered elsewhere (yet).

The syntax is one of the following:

while condition do statement [ ; ]

while ( condition ) [do] statement [ ; ]

while condition [do] [begin] [ { ]   
  statement [ ; ]    
  statement [ ; ]   
  ...    
[ } ] [end [while]] [ ; ]    

If you make a mistake and create an infinite loop, then it will give up after a fifth of a second and speak "infinite loop".

FOR loops

For loops aren't as useful in PIE as they are in other languages, because PIE is not a linear language. A PIE script continuously loops through the entire script, even while IF statements are running in the background. If you think you need a for loop then you may be looking at the problem the wrong way.

The entire for loop will be executed in one go. You can't use it to wait for some condition triggered elsewhere (yet).

The syntax is one of the following:

for variable (=|:=) InitialValue (to|downto) FinalValue [step amount] do statement [ ; ]

for variable (=|:=) InitialValue (to|downto) FinalValue [step amount] [do] [begin] [ { ]    
  statement [ ; ]    
  statement [ ; ]   
  ...    
[ } ] [end [for]]    

for ( initialization ; condition ; increment ) [do] statement [ ; ]

for ( initialization ; condition ; increment ) [do] [begin] [ { ]
statement [ ; ]
statement [ ; ]
... [ } ] [end [for]]

If you make a mistake and create an infinite loop, then it will give up after a fifth of a second and speak "infinite loop".

Wait command

Wait commands are for use in macros. Everything inside an IF statement is considered a macro. A wait command will pause only the macro it is inside of, while the rest of the script will keep going in the background. If you have nested if statements inside each other, then it will only pause the innermost if statement. So don’t use it inside an if statement which is already inside an if statement itself.

The syntax is either:

wait duration [ ; ]

wait( duration ) [ ; ]

You should normally specify the units for the duration. Valid units are: milliseconds (ms), seconds (s), minutes, hours, days.

eg.
wait 100 ms wait 1 second wait(500 milliseconds);

Increment statements

You can add one to something using one of these syntaxes:

var.x++
++var.x
Inc(var.x)
These are commands, not functions like in C. So you can’t set something else to equal var.x++.

You can subtract one from something like this:

var.x--
--var.x
Dec(var.x)

SHR and SHL statements

You can shift a value’s bits to the left or right with the SHR and SHL commands: eg.

shr var.x, 1

Say command

You can use the Say command to make GlovePIE speak:

Say “hello world”

or

Say(“hello world”)

Other Commands

The following other commands also exist:

ExitProgram, ExitPIE, Execute(filename), Chain(filename), Display(text), DebugPrint(text) AddCode(text), ControlPanel, ControlPanelKeyboard, ControlPanelJoystick, ControlPanelMouse, ControlPanelP5, ControlPanelPPJoy, ControlPanelSpeech, ControlPanelMidi, ControlPanelDisplay,

PlaySound(filename), Beep([freq, duration]), BeepAsterisk, BeepExclamation, BeepHand, BeepQuestion, BeepDefault,

FlashPieWindow, HidePie, ShowPie, MinimizePie, MaximizePie, RestorePie, UnMinimizePie, UnMaximizePie,

Press(x), Release(x), Toggle(x),

Type(text), TypeUnicode(text),

SendOsc(ip, port, address, [p1, p2, p3…]), BroadcastOsc(port, address, [p1, p2, p3…]) WiimotePoke([wiimote number], address, value) WiimoteSend(wiimote number, report number, [p1, p2, p3…])

Functions

Here are some of the functions that exist in GlovePIE

Pixel colour Functions

ScreenPixel(x, y): colour of screen pixel at (x, y) as an integer, in hexadecimal it looks like 0xRRGGBB. This is the opposite of the way the windows DLLs do it, but it is the way the internet and DirectX do it. To display it:

Debug = “0x”+ IntToHex(ScreenPixel(0, 0), 6)

You can also treat it like a vector:

[var.r, var.g, var.b] = ScreenPixel(0, 0)

debug = var.r+’, ‘+var.g+’, ‘+var.b

That will set var.r, var.g, and var.b to the values between 0 and 1 corresponding to the amount of red, green, and blue in the colour. The (0, 0) coordinates could be any pixel coordinates.

Trigonometry Functions

All angles are measured in degrees by default.
The following trigonometry functions are implemented:

Standard trig functions:sin, cos, tan, sec, cosec, cotan
Hyperbolic trig functions: SinH, CosH, TanH, SecH, CosecH, CotH

Inverse trig functions: aSin, aCos, aTan, aSec, aCosec, aCotan
Inverse Hyperbolic trig functions: aSinH, aCosH, aTanH, aSecH, aCosecH, aCotanH

2D inverse tan function: atan2

Rounding Functions

These functions preserve the units of their parameters.

ceil Rounds towards infinity floor: Rounds towards negative infinity trunc int: Rounds towards zero round: Rounds towards nearest integer. .5 rounds to nearest even number (Bankers' Rounding)

frac: Returns signed fractional component. eg Frac(-1.32) = -0.32

RoundTo(x, digits): If digits is negative, rounds to that many decimal places using Banker's Rounding If digits is positive, rounds to that power of ten using Banker's Rounding.

SimpleRoundTo(x [, digits]): Same as RoundTo except 0.5 always rounds up. Unfortunately -1.5 rounds up to 1. digits defaults to -2 (meaning 2 decimal places).

Sign Functions

Sign: returns the sign of a number. 1 if it is positive, 0 if it is zero, -1 if it is negative
Abs: returns the modulus or absolute value. Removes the sign of a number. Preserves units.

Exponential and Square Root Functions

Raising things to the power of something:

sqr(x): caculates x^2
sqrt(x): calculates the square root of x. x^(1/2)
power(x,y): calculates x^y
intPower(x,y): calculates x^y where x and y are integers (the result is not an integer if y is negative)
exp(x): calculates e^x. e is 2.71828. The derivative of e^x is e^x.
Ldexp(s,p): calculates s * (2^p)

Poly(x, a0, [a1, [a2, [a3, [a4, [a5, [a6]]]]]]): returns a0 + a1x + a2(x^2) + a3*(x^3) + a4*(x^4) + ...

Logarithms (undoing raising something to some power):

Log10(x): returns the number you have to raise 10 to the power of, in order to get x. eg. Log10(1000) = 3
Log2(x): returns the number you have to raise 2 to the power of, in order to get x. eg. Log2(256) = 8
LogN(N, x): returns the number you have to raise N to the power of, in order to get x. eg. LogN(10, 1000) = 3
Ln(x): returns the number you have to raise e (2.71828) to the power of, in order to get x
LnXP1(x): the same as above, but for x+1 instead of x

Comparison functions

IsZero(x): returns true if x is zero
IsInfinite(x): returns true if x is infinite
IsNaN(x): returns true if x is not a number

SameValue(a, b [, epsilon]): returns true if a and b are the same, or differ by no more than epsilon
InSet(x,a,b,c,d,e,f,g,...): returns true if x matches one of the values following it.

max(a,b): returns the maximum of two values. Preserves units.
min(a,b): returns the minimum of two values. Preserves units.

Range functions

EnsureRange(x, a, b): Returns the closest value to x which is within the range [a, b]. Preserves units.
InRange(x, a, b): Returns true if x is within the range [a, b].

MapRange(x, a, b, c, d): Returns value x converted from the range [a, b] to the range [c, d]. Values outside the original range will map to the appropriate values outside the new range.

EnsureMapRange(x, a, b, c, d): The same as MapRange except values outside the range are mapped to the closest values inside the range.

NEW! DeadZone(x, a): Returns value x between -1 and 1, but with a deadzone around zero, so that values within the range [-a, a] of the zero point become 0, and other values between -1 and 1 are scaled to compensate. You should only use this function for values roughly between -1 and 1, like joystick values.

Random functions

Random: Returns a random fractional number between 0 and 1.
Random(n): Returns a random whole number between 0 and n-1.
RandomRange(a,b): Returns a random whole number between a and b.
RandG(mean, StandDev): Returns a random number from gaussian distribution around mean. Preserves units.

Ordinal functions

odd(n): Returns true if n is odd
pred(n): Returns n-1
succ(n): Returns n+1

Date/Time functions

TimeStamp or GetTimeStamp: A precise timestamp measured in seconds, from an arbitrary starting point. Much more accurate than the other time functions (which are measured in days).
Now: Current time and date (in days since December 30, 1899)
Time: Current time (in fractions of a day)
Date: Current date (in days since December 30, 1899)

Tomorrow: Tomorrow's date (in days since December 30, 1899)
Yesterday: Yesterday's date (in days since December 30, 1899)

CurrentYear: Current year of the Gregorian calendar (in years).

DateOf(x): Returns the date part of the date and time in x (in days since December 30, 1899)
TimeOf(x): Returns the time part of the date and time in x (in fractions of a day)

Dayofthemonth(x), dayoftheweek(x), dayoftheyear(x), dayofweek(x), Daysbetween(x, y), Daysinamonth(x, y), daysinayear(x), daysinmonth(x), daysinyear(x), DaySpan(x, y), HourOfTheDay(x), HourOfTheMonth(x), HourOfTheWeek(x), HourOfTheYear(x), HoursBetween(x, y), HourSpan(x, y), IncDay(x, [y])

Temporal functions

Delta(x)

How much x has changed since the previous GlovePIE frame. It will be negative if x has decreased.

Smooth(x, [ExtraFrames, [DeadbandDistance]])

Smooths out the value of x by averaging with the previous ExtraFrames frames. If it hasn’t changed by more than DeadbandDistance it doesn’t count as changed at all. By default DeadbandDistance is 0.

Kalman(x, noise1, noise2)

Smooths using a Kalman filter, in theory. It has a tendency to diverge if you get the noise values wrong. Don’t ask me what the correct noise values are.

Pressed(x), Clicked(x), SingleClicked(x), DoubleClicked(x)

Returns true or false.

HeldDown(x, MinTime)

Returns true if x has been held down for at least MinTime.

KeepDown(x, MinTime)

Keeps the result being true after x has stopped being true, until MinTime is up. Operators

There are lots of different operators you can use in GlovePIE. All of them allow you to mix different units together and rely on GlovePIE to automatically convert the units.

a + b
a plus b

If a and b are numbers they will be added together. If a and b are strings of text then they will be concatenated together. If a and b are true/false values then they must both be true for it to be true (the same as “and”). If a is a vector, but b isn’t, it will extend the length of the vector without changing the direction (not implemented yet).

a and b
a but b

Will only be true if a is true and b is also true (anything greater than 0.5 or less than -0.5 is considered true). This only does logical and. If you want to AND the binary bits together, use & instead.

a & b

If a and b are numbers, then it will only include in the answer the binary bits that are 1 in both a and b for that position. If a and b are not whole numbers, they will be rounded off first. If a and b are strings, they will be concatenated.

a - b a minus b

If a and b are numbers they will be subtracted. If a and b are true/false values then they must both be true for it to be true (the same as “and”). If a is a vector, but b isn’t, it will decrease the length of the vector without changing the direction (not implemented yet).

-b

Will swap the sign of b.

not b

Will be true if b is false, and vice-versa

a x b
a * b
a times b
a multiplied by b
a cross b

Multiplies a and b together. If a and b are vectors it will do cross multiplication.

a . b
a dot b

If a and b are vectors it will do dot multiplication. Otherwise it does normal multiplication.

a / b
a divided by b

If a and b are numbers they will be divided properly, not like in C where you always get a whole number. 1 / 2 will give you 0.5. If you want integer division, you need to use div instead. If a and b are true/false values, then it will be true if either of them are true (the same as “or”)

a div b

Will give you the whole-number answer of a / b minus the remainder. IMPORTANT: a and b do not have to be integers. They can have decimals. 7.5 div 3.5 gives you the answer 2.

a mod b
a % b

These both give you the remainder of a divided by b. a, b, and the answer may all contain decimals. They don’t have to be whole numbers. For example, 7.5 mod 3.5 gives you 0.5.

a or b
either a or b

This is true if either a or b is true, or both. This only does logical or. If you want to manipulate the binary bits, use | instead.

neither a nor b

This is true if both a and b are false.

a | b

This includes in the answer the binary bits that are 1 in a or b or both.

a xor b This is true if a or b is true, but not both.

a ^ b
a ** b
ab NEW!
ab1+b2 NEW!
a^b NEW!

This is a raised to the power of b, ie. ab. You can write the number using unicode superscript characters if you prefer.

a ^^ b

This is a tetration b. Don’t ask.

b!

This is b factorial. b + (b-1) + (b-2) + … + 1

b!!

This is b double-factorial. IT IS NOT (b!)!

a shl b
a << b

This is the binary bits of a shifted to the left b bits. It is the same as a * (2^b).

a shr b
a >> b

This is the binary bits of a shifted to the right b bits. It is the same as a div (2^b).

|b|

This is the modulus (otherwise known as absolute value) of b, if b is a number, or the length of b if b is a vector, or the derivative of b if b is a matrix.

a%

This is a divided by 100.

a% of b
a % of b

This is a divided by 100 then multiplied by b.

a = b
a == b

True if a and b are the same (once converted to the same type and units).

a != b
a <> b
a isn’t b
a is not b

True if a and b are different (once converted to the same type and units).

a ~= b

True if a and b are approximately equal. If they are numbers, they must be within PIE.Epsilon of each other, when converted to the units of b. If they are strings then they must be the same except for case and leading and trailing spaces.

a !~= b
a ~!= b

True if a and b are not approximately equal.

a > b

True if a is greater than b. False if a is equal to b.

a < b

True if a is less than b. False if a is equal to b.

a >= b

True if a is greater than or equal to b

a <= b

True if a is less than or equal to b

b <= a <= c
c >= a >= b

True if a is between b and c inclusive.

b < a < c
c > a > b

True if a is between b and c, but is not b or c.

b <= a < c
c > a >= b

True if a is between b and c, but is not c.

b < a <= c
c >= a > b

True if a is between b and c, but is not b.

Clone this wiki locally