Basics of the microScript language

As a reminder, microStudio is a free integrated video game development environment.

In this article, we will look at the microScript language.

So, to begin with, microScript is a local language… Currently, it is only used within microStudio… A program written in the microScript language is transpiled into javascript. microScript is a fairly easy-to-learn language, similar in syntax to the Lua language.

Here are some general principles of the microScript language that we will talk about in more detail in this article:

  • Variables are global by default. To define a local variable, use the “local” keyword.

  • Line breaks do not really matter; they are treated as spaces.

  • No value nullnil or undefined… Any undefined or null variable defaults to 0.

  • There is no boolean type. 0 is false, and anything that is not 0 is true.

  • There is no runtime error or exception. Any variable that is not defined returns 0.

  • Calling a value as a function that is not a function returns the value itself.

“Hello, world!” in microScript language:

print("Hello, world!")

Writing one-line comments with //:

print("Hello, world!") //здесь можно написать комментарий

Anything before the next line break is ignored when the program runs. Multi-line comments are not available in microScript.

Variables

Any variable that has not yet been used can be assumed to exist and have a value of 0. To start using a variable, it must be assigned an equal value:

x = 1

All variables in microScript are considered global unless they are explicitly declared as local.

A local variable can be defined using the “local” keyword:

myFunction = function()
	local i = 0
end

When a type is specified as local to a variable, then its scope is limited to functions within their scope.

Variable names must start with either a Latin letter or an underscore _followed by latin letters, numbers or underscores:

_x1 = 2
var_a = 3

Uppercase and lowercase letters are different because microScript, like Lua, is case sensitive.

There is a list of reserved words that cannot be used as a variable identifier: and, break, by, class, continue, else, elsif, end, for, function, if, in, local, new, not, object, or, return, then, to, while

Value types

microScript recognizes five types of values:

Numbers

Values ​​of the “number” type in the microscript can be integers or decimal numbers. For instance:

pi = 3.1415
x = 1
half = 1/2

Strings

Strings are texts or fragments of texts. They must be quoted, double or single. Example:

name = "Вася"
print('Привет' + name) //конкатенация сток с помощью оператора
											 //сложения "+"               

In the console: Привет Вася

Lists

Any number of required values ​​of any of the five types can be placed in the list:

empty_list = [] //пустой список
prime_numbers =[2,3,5,5,7,11,13,17,19] //список чисел
mixed_list =[1,"кот",[1,2,3],draw()] //смешанный список

You can access the elements of a list by their index, that is, their position in the list, starting at the index 0:

list = ["кот", "собака", "мышь"]
list[0] //"кот"
list[1] //"собака"
list[2] //"мышь"

You can loop through the list using a for loop:

for element in list
	print(element) //на консоль выведутся все элементы списка начиная с первого
end

Objects

An object in a microscript is a form of an associative list. An object has one or more “fields” that have a key and a value. The key is a character string, the value can be any microscript value. An object definition starts with the “object” keyword and ends with the “end” keyword. Several fields can be defined in between. Example:

my_object = object
  x = 10
  y = 20
  name = "object 1"
end

You can access the fields of an object using the operator .:

print(my_object.x)

In the console: 10

So the above example could be written like this:

my_object.x = 0
my_object.y = 0
my_object.name = "object 1"

You can add a new field for the object my_object:

my_object.vx = 5

Also, the fields of an object can be accessed using parentheses []:

my_object["x"] = 0
my_object["y"] = 0
my_object["name"] = "object 1"

Functions

A function is defined with the “function” keyword and ends with the “end” keyword:

nextNumber = function(x)
	x+1
end

Function call:

nextNumber(10)
print(nextNumber(10))

In the console:11
When a value is called as a function that is not a function, it simply returns its value. Example:

x = 1
print(x())

In the console:1
The above code returns “1” without an error. This way, it is even possible to call a function that is not yet defined (then it returns 0) without throwing an error. This allows you to start structuring your program very early on with sub-functions that you will work on later. For instance:

draw = function()
  drawSky()
  drawClouds()
  drawTrees()
  drawEnemies()
  drawHero()
end

Classes

In microscript, classes and objects are very similar concepts and can be used almost interchangeably.

Class creation begins with the “class” keyword and ends with the “end” keyword. Example:

Enemy = class
  constructor = function(position)
  	this.position = position
  end
  hp = 10
  velocity = 1
  move = function()
  	position += velocity
  end
  hit = function(damage)
  	hp -= damage
  end
end 

The first property that we have defined in the above class is the “constructor” function. This function is called when the class object is instantiated. It will set the position property (position) object. this refers to the instance of the object on which the function will be called, thus setting this.position means the object is setting a property position for yourself.

Instantiating objects from a class:

enemy_1 = new Enemy

Operator new used to create a new instance of an object derived from a class.

Let’s create another object:

enemy_2 = new Enemy

Both objects enemy_1 and enemy_2 have the same fields velosity equal to 1:

enemy_1.velosity //равно 1
enemy_2.velosity //равно 1

You can change the value velosity for enemy_1 For example:

enemy_1.velosity = 2

Variables hp and velocity are also available to us directly from the class:

Enemy.hp
Enemy.velosity

Conditional operator

In a microscript, the conditions are written as follows:

if age<18 then
	print("ребенок")
else
	print("взрослый")
end

“if” means “if”; “then” means “then”; “else” means “otherwise”; end means end.
In the above example, if the value of the age variable is less than 18, then the statement print("ребенок") will be executed, otherwise the instruction will be executed print("взрослый")

Several hypotheses can be tested using the “elsif” keyword (short for “else if”).

if age<10 then
	print("ребенок")
elsif age<18 then
	print("подросток")
elsif age<30 then
	print("молодой человек")
else
	print("очень взрослый")
end 

For loop

Cycle for is widely known in programming, just like the conditional operator if, however, I will briefly describe its use in microscript.

This loop allows for the same processing of all elements of a list or a series of values:

for i=1 to 10
	print(i)
end

In the above example, we will output to the console every number from 1 to 10.

We can do the same, only with a step of 2:

for i=0 to 10 by 2
	print(i)
end

In the above example, we will display numbers from 0 to 10 to the console with a step of 2 (by 2).

While loop

This cycle allows operations to be repeated until a satisfactory result is obtained:

x = 1
while x*x<100
   print(x*x)
   x = x+1
end

The above example outputs a square x, then the value is increased x (one is added to x) if the square x less than 100.

Abort or continue the cycle

We can interrupt the loop ahead of schedule using the operator break:

while true
   x = x+1
   if x>= 100 then break end //если x больше либо равен 100, цикл будет прерван
end

You can skip the rest of the loop and go to the next iteration of the loop using the operator continue… Example:

for i=0 to 10000
	if i%10 == 0 then continue end //это позволит пропустить обработку чисел,
                                 //кратных 10
	doSomeProcessing(i)
end

Arithmetic Operators

Operator

Description

+

Addition

Subtraction

*

Multiplication

/

Division

%

Modulo division (remainder of division)

^

Exponentiation

Binary comparison operators

Operator

Description

==

a == b is true only if a equals b

! =

a != b is true only if a differs from b

<

a < b is true only if a strictly less b

>

a > b is true only if a strictly more b

<=

a <= b is true only if a less or equal b

> =

a >= b is true only if a more or equal b

Boolean operators

Operator

Description

and

logical AND: a and b is only true if a and b are true

or

logical OR: a or b is only true if a true or b verily

not

logical NO: a not true if a false, and false if a verily

Boolean values

There is no boolean type in microScript. 0 is considered false and any other value is true. Comparison operators return 1 (true) or 0 (false). For convenience, microScript also allows the following predefined variables to be used:

Variable

Meaning

true

one

false

0