Mama scripting guideYou can write scripts within your program using Mama programming language, which has a structure and syntax quite similar to Python, but with support for 3D object manipulation. There are two ways to enter scripts into programs: using the script tile, or using a free scripting window (available only when enableFreeScripting configuration parameter is set to true). Both modes let you enter whatever code you like, where the basic difference is in the execution time:
Contents
BasicsMama lets you print anything you like to the output console using the "print" command:print "hello Mama!" # prints "hello Mama!"Text written after the '#' symbol to the end of line is considered a comment. You may as well define a variable holding the above string, and then print it: define s="hello Mama!" print s # prints "hello Mama!"Note: a variable must be defined before it is used using the define keyword. If you use and undefined variable, you'll get a syntax error: s="hello Mama!" print sOutput: NameException: Name 's' not defined Error fix options: 1. Verify correct spell of the name 2. Verify that the name was defined before usage MathThe next program shows example of math operations in Mama:define a=12, b=5, c c = a+b print a,b,c c = a*b print a,b,c c = a/b print a,b,cOuput is: 12 5 17 12 5 60 12 5 2.4We can write the last program in a shorter form and with an explanatory info: define a=12, b=5 print "a=",a,"b=",b,"a+b=",a+b print "a=",a,"b=",b,"a*b=",a*b print "a=",a,"b=",b,"a/b=",a/bOutput is: a= 12 b= 5 a+b= 17 a= 12 b= 5 a*b= 60 a= 12 b= 5 a/b= 2.4 ListsYou can have lists of any type - numbers, strings, objects etc. Moreover, a list may contain elements of different types.You define a list of items using the parenthesis operators ("(" and ")") and access its elements using the subscript operator ("[]"): define letters=("a", "b", "c", "d", "e", "f") print letters[4] #prints: e print letters[1,2,3] #prints: (b, c, d) print letters[1..4] #prints: (b, c, d, e) print letters.length() # prints: 6 letters.add("p") # add 'p' to the end of the list letters.remove("c") # removes 'c' from the list print letters # prints: (a, b, d, e, f, p) letters.reverse() # הופכת את סדר האיברים בסדרה print letters # prints: (p, f, e, d, b, a) letters.sort() # sorts the list print letters # prints: (a, b, d, e, f, p)Lists of numbers can be created using the range operator (".."): print 2..5 # creates the list (2,3,4,5) and prints it print 12..20 @ 3 # creates a sublist of (12,13,14,15,16,17,18,19, 20) containing the elements (12, 15, 18), then prints itLists can be assigned to variables: define a,b,c a,b,c=10,-2,"hello" print a,b,c # prints: 10 -2 hello Tables/MapsTables are used to map keys to objects. For example:define animal_heights = ["dog":50, "horse":170, "cat":30, "mouse":6] animal_heights["rabbit"] = 20 print animal_heights # prints: [cat: 30, dog: 50, horse: 170, mouse: 6, rabbit: 20] assert animal_heights.length()==5 # create a copy define t = animal_heights.copy() print t # prints: [cat: 30, dog: 50, horse: 170, mouse: 6, rabbit: 20] # save to file and restore from file t.write("animals.dat") t.clear() t.read("animals.dat") print t # prints: [cat: 30, dog: 50, horse: 170, mouse: 6, rabbit: 20] Control FlowIf/ElseConditional statements are written in Mama using the keywords if, else and elif.define a=random(),b=random() print "a=",a,"b=",b if(a < b) print "a<b" elif(a>b) print "a>b" else print "a==b" LoopsMama has 3 kinds of loops: for, while and repeat.The for loop can be used to loop over elements in a list: define letters=("a", "b", "c", "d", "e", "f") for letter in letters print letter, # prints: a, b, c, d, e, f
define a=5 while(a>0) print a, # prints: 5 4 3 2 1 a--The repeat loop lets you write simple loops which run N times: repeat 3 print "hello" # prints 3 times "hello" FunctionsFunctions let you write modular programs, by decomposing missions into parametrized reusable pieces. Following is a sample function for calculating a factorial of a given number:function factorial(n) if n==0 return 1 define result=1 for i in 1..n result=result*i return result print factorial(5) # prints: 120 print factorial(10) # prints: 3628800 print factorial(170) # prints: 7.257415615307994E306 print factorial(200) # prints: infinityFunctions can define optional parameters by setting default values, for example: function print_car_details(name,id,speed=100) print name,id,speed print_car_details("Mustang","red"); print_car_details("Ferrari","black", 200); Classes and objectsClasses are defined using the class keyword, and class methods are defined using the method keyword. A car class:class Car define name, speed, color # member variables method init(n, c, s=100) # constructor: third argument is optional - it has a default value name=n color=c speed=s print "Car object created!" method print_data() print "name="+name+", speed="+speed + ",color="+color # create objects define mustang=Car("Mustang", "red") mustang.print_data() define ferrari=Car("Ferrari", "black", 200) ferrari.print_data()Prints: Car object created! name=Mustang, speed=100,color=red Car object created! name=Ferrari, speed=200,color=black Inheritance and PolymorphismA class can inherit another class using the inherits keyword:class Car define name, speed, color # member variables method init(n, c, s=100) # constructor name=n color=c speed=s print "Car object created!" method print_data() print "name="+name+", speed="+speed + ",color="+color class Tank inherits Car define weight method init(n, c, s=100, w=5000) Car.init(this, n, c, s) # constructor: call base class constructor weight=w method print_data() base.print_data() print "weight="+weight # create objects define mustang=Car("Mustang", "red") mustang.print_data() define ferrari=Car("Ferrari", "black", 200) ferrari.print_data() define tank=Tank("Tank", "green", 50, 10000) tank.print_data()Prints: Car object created! name=Mustang, speed=100,color=red Car object created! name=Ferrari, speed=200,color=black Car object created! name=Tank, speed=50,color=green weight=10000 Scripting: manipulating 3D Objects in the sceneFrom within Mama scripts you can manipulate 3D objects directly by calling their standard and custom methods. For instance, create an empty world, then add from the gallery the object Characters/kids/Jock. Then, drag a script tile and write the following:Jock.walk(3) Jock.say("hello") Jock.walkOffScreen()Run the program - Jock will do exactly what you asked him to! Drawing ApplicationMama does not limit you to the IDE 3D window - you can actually do whatever you like using Mama's own GUI library. First version: a simple drawing application - lets you draw on a board using the mouse.
class DrawingApp inherits Window # handle mouse events method mouse_pressed(x, y) # standard mouse pressed method turtle.position(x, y) # turtle represents the current pen location method mouse_dragged(x, y) # standard mouse dragged method turtle.to(x, y) # draw line from current position to the given one define app=DrawingApp() # start the application - create new Application objectNotes:
|