Scripting #1 Variables part 1

C Scripting questions and answers
Post Reply
User avatar
Global Moderator
Global Moderator
Posts: 4574
Joined: Sun Sep 05, 2010 6:14 am

Scripting #1 Variables part 1

Post by ArtF »

Scripting 101


 I figure pretty much everyone at this point knows what a variable is, but
I think the topic has to be started as a discussion point at any rate, and for
anyone truly starting fresh in scripting.

  A variable is just that, a thing that varies. It can be a type of number
( integer or float in the case of Monkey Script), boolean , string, table, pointer
or even a function. In reality almost everything in Monkey is a variable.

  In many languages, you have to "type" a variable, meaning to declare to the
system what kind of variable it is. Youll often see this in lines such as

int g = 9;  // this declares an integer of value 9...

 Monkey script is self-typing, so you needn't declare a type, the system will figure
out from the type of thing your putting into the variable exactly what type of variable
it is. So in Monkey the above line is just  

g = 9; // sets g to the integer 9

 You end every command in GameMonkey Script with a ";" just as in most languages
and "//" denotes a comment.

 Monkey is also polymorphic, meaning it can swap types of a variable as you go.
for example..

a = 9;
a = "Hello";
a = 9.2;

  The lines above are legal, first "a" becomes a variable name holding the integer number 9,
it then becomes a string, and finally, a float. It can occur that you wonder how
to ensure an integer isnt really a float , after all, a 5 looks like a 5.0
  When you need to ensure a variable is used as a float ( or an integer ) a sub command
exists to help.

 a = 5;
 b = a.ToFloat();
 c = a.ToInt();

 In the above lines, the variable b will always be a float, while c will always
be an integer. Its not often that will make a difference, but its worth mentioning.

  The variables you use will sometimes be just for your function  (which we call local
variables) or at other times for other functions to use. To allow a variable to stay around
for other scripts when your gone, you can declare it as global.

ex: global g = 9;

 This variable once declared global is usable through out the system , even in your Gcode
scripts. So , of course, g would be a very bad choice for a variable name. In fact
if Auggie ever gathers any following, it could be hard to think of variable names that
wont conflict with others. Id recommend you use variable names linked to whatever library
or function your writing to minimise any confusion. A variable name like

PolarDRO1 = 1;

is much more easily understood by a reader, and far less likely to be duplicated by someone
else.  Booleans are the true/false answers in script, so you can use a variable such as

a = true; or
b = false;

  In reality, booleans are just an integer value of 1 or 0, and can be used in programs and
scripts as numbers. In fact most variables can. so one could print a result of math on a boolean
such as

a = true; b= false;
print( a * 4 + "," + b * 4);

4 , 0

   This shows the "true" is really just a 1, b is zero, and also displays the way strings can be
added to numbers to create printable strings of both.

a = 13;
b = "Im in the " + a + "th level of heaven.";

 would put into  b  a full sentence with formatting done for you.This is simpler than in
most C languages and makes for faster scripting.

 Tables are also a variable type in Game Monkey Script, and declarations such as

mytable = table( 1,2,3," what? text?", 5.6);

will create an indexable table of entries, in which each entry may be of any type, even a function.
Arrays of variables can be created in a table.

print( mytable[0] );

print( mytable[3] );

what? text?

print( mytable)


 You can access table elements with indexes, and the table itself can be seen to be a pointer in
the print above.
You can make tables of any variables, so a table of tables can be made..

mytable1 = table( 1,2,3," what? text?", 5.6);
mytable2 = table( 7, 8, 9 );
mytable3 = table( mytable1, mytable2);
print( mytable3[0][3] );  //print the 3rd entry of the first table


Tables may also be indexed by name. Consider this table

mytable = table( axis = "X", feedrate = 2000, accel = 55555 );

print( mytable["axis"]);


print( mytable["feedrate"] );


mytable["axis"] = 6.7 //change the value and type of the entry
print mytable["axis"]


mytable["anewentry"] = 3.14;  //add a  new value to the table..

     Next Topic will show how even functions are variables and can be put in tables
or manipulated much like any other variable... Enough for today I think. Feel free to post
comments or questions on this thread.

Last edited by ArtF on Fri Jan 08, 2016 9:21 am, edited 1 time in total.
Old Timer
Posts: 362
Joined: Wed Sep 10, 2014 1:35 pm

Re: Scripting #1 Variables part 1

Post by DanL »

thank art that make sense, I still get confused when it comes things like k = 1 when and how to use that sort of thing, your explanation is very noob friendly what is hard work.

if you keep doing topics formated like this it is going to make it easier.

I know I have a lot of M codes to change from arduino C type script to monkey, the one for my 3D printer, they have a lot of calls in them
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest