Introduction to Scripting with LSN - Paramecium13/LSN GitHub Wiki

This tutorial is aimed at people with little programing experience. However, I recommend that you try some online tutorials for other languages to help learn about concepts common to all or many programming languages.


Glossary Definition: This is a glossary definition! It defines a common term. Some terms may have multiple glossary definitions in this tutorial, with the ones shown earlier being simpler and the ones shown later being more advanced.


This is an aside. It contains some information that is related but not always essential to what is being discussed. It is often written in a question and answer form.

#Part 0: Hello LSN! Let's get started by looking at some code.

Our First Code

fn MyFunction()
{
    say "Hello World!";
}

We're going to examine this code piece by piece. The word fn means that we're defining a function.


Function: Contains code that can be run.


MyFunction is the name of the function we're making. We can use this name elsewhere to refer to our function. A name cannot contain any spaces, start with a digit, or be the same as the name of another function. We need to write the () because it's a function. The { signifies the start of our function's code and the } signifies it's end.

The line say "Hello World!"; is a statement tells the game to show the message 'Hello World' to the player.


Statement: A piece of code that tells the game to do something. There are a lot of different types of statements.


The fact that this is a say statement (and not some other kind of statement) is indicated by the word Say. "Hello World" is the string that we want to be shown.


String: Some text. To write a string, enclose the text in double quotes.


The ; symbol signifies the end of the statement. It's useful because when you are writing a long statement, you can write it in multiple lines--LSN doesn't care about lines, you could even write your whole file in one line if you want to.

Now let's write some code to have a character greet the player by name.

Our Second Code

fn Greet(playerName : string, characterName : string)
{
    let greeting = "Hello " + playerName + "!";
    say greeting as characterName;
}

// This is a comment, it's ignored by LSN.
fn MyFunction()
{
    Greet("Paramecium13","King Rex");
}

Wow; there's a lot of new stuff in there. First, there's now some code between the parenthesis. This is where we list the function's parameters. This function has two parameters, playerName : string and characterName : string, which are separated by a comma (NOT a coma). The first part of the parameter is its name, which is how we refer to it in the function. Next comes a colon, which separates the name from the type.


Parameter: ...


Types

The last piece of the parameter definition is the name of its type. LSN is a statically typed language. That means that the reifier needs to know the type of every value, parameter, and anything else that has a type (a bunch of other things we'll discuss later). It needs this information so it can make sure we don't make certain kinds of mistakes, like trying to divide by a string.


Type: ...


Both of these parameters are of type string (The string we used in our first example was a string literal...).

Variables and Expressions

The line let greeting = "Hello " + playerName + "!"; is a statement that creates a variable named 'greeting'. A variable allows us to save a value that we can use later. Once we create a variable we cannot give it a different value. The word let indicates that this statement creates a variable.


Variable: ...


The part after the equals sign and before the semicolon is an expression that calculates what string to show. It does this by adding (i.e. joining together, a.k.a. concatenating) three strings using the plus sign. The first and last strings are string literals but the second is a parameter. When the code runs, that parameter will have a value which can be added to the other strings.


Expression: ...


Q: But wait! If it's a variable, why is its value always the same? Shouldn't it be called a constant?

A: While it looks like value is constant, it depends on the parameter 'playerName' and thus is not constant. However, if you write a variable whose value actually is constant (i.e. can be computed by the reifier), LSN will actually treat it as a constant and place its value wherever the variable is used.

Many types of expressions, including the add expressions we used above, contain other expressions. Note that in the line we're (still) discussing, the literal strings "Hello " and "!", and the parameter playerName are also expressions, the latter being a parameter expression. Parameter, literal, and variable expressions (as well as a few others we'll discuss later) are 'primitive' expressions, can be used by themselves or to compose other expressions.

The line say greeting as characterName; is another say statement. Instead of using a string literal like the previous one, it uses the variable 'greeting'.