Email:

Mama scripting guide

You 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:
  • tile scripts are executed during the program run, according to the program control flow - similar to normal code lines.
  • free scripts are executed at start of world run
Contents

Basics

Mama 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 s
 
Output:
 
NameException: Name 's' not defined
Error fix options:
	1. Verify correct spell of the name
	2. Verify that the name was defined before usage
 


Math

The 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,c
 
Ouput is:
 
12 5 17
12 5 60
12 5 2.4
 
We 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/b
 
Output is:
 
a= 12 b= 5 a+b= 17
a= 12 b= 5 a*b= 60
a= 12 b= 5 a/b= 2.4
 


Lists

You 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 it
 
Lists can be assigned to variables:
 
define a,b,c
a,b,c=10,-2,"hello"
print a,b,c				# prints: 10 -2 hello
 


Tables/Maps

Tables 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 Flow

If/Else

Conditional 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"
 

Loops

Mama 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
 
Note: the "," at the end of the print command avoids printing of a new line after the argument.
With while loop you iterate while the given condition is true:
 
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"
 


Functions

Functions 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: infinity
 
Functions 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 objects

Classes 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 Polymorphism

A 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 scene

From 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 Application

Mama 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.
  • We will define the main class as inheriting from Window, so that it will open in a separate window.
  • We will use the turtle global object - this object simplifies drawings and it provides convenient methods for repainting the drawing, for storing (serialization) and for restoring (deserialization). This are some of the turtle common methods:
    • position(x,y) - position the turtle at point (x,y)
    • to(x,y) - draw line from current position of the turtle to point (x,y), and change position to the new point
    • clear() - clears the drawing
    • undo()/redo() - undo/redo last change
    • save_content(filename) - saves the content of the drawing to the given filename
    • restore(filename) - restores the content of the drawing from the given filename
 
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 object
 
Notes:
  • In order to access the script window, after starting the application you will need to close the 3D window
  • to edit Mama scripts easily, set the configuration flag enableFreeScripting to true (see the documentation).