The absolute beginners guide to Squirrel

EK.IceFlake

  • Hero Member
  • "We are the champions my fellahs" - Sufyan/VK.SuFy
  • Posts: 1,761
The absolute beginners guide to Squirrel
« on May 20th, 2016, 05:53 PM »Last edited on June 22nd, 2017, 03:51 PM
(click to show/hide)
Yeah, just translating this to Squirrel

Part I
Concepts of Squirrel

Welcome to the absolute beginner's guide to Squirrel! Before you can get into the nooks and crannies of Squirrel, first we have to learn about the concepts of Squirrel and programming in general.

Introduction
Squirrel is an embeddable programming language and has a C-like syntax. Squirrel was designed as an embeddable programming language, and is now used by projects like VC:MP. Squirrel scripts are compiled at runtime, but can be precompiled to prevent copying. Squirrel scripts run in a virtual machine. Squirrel scripts have a structured flow, which means instructions are run one after another, but control statements can change what instructions come next.

Basic program structure
A very basic "Hello world" script would look like this.
Code: [Select]
HelloWorld();
function HelloWorld()
{
    print("Hello world");
}
The function call, "HelloWorld();", executes the function "HelloWorld" without any parameters.
The keyword 'function' specifies that a function is being made.
The brackets, { and }, are enclosures of instructions. They link instructions together and without them you can only have one function call (which is done here, but the brackets are included for their explanation)

Notice how HelloWorld(); and print("Hello world"); end with semicolons, but the rest of the script doesn't. That is because semicolons define where commands end, whereas control flow contains commands.

Variables
Variables are placeholders for information that a script can use, and can be changed at any time by the script. Variables are given names so we can not only get the variable's value, but so that we can also change it later if we need to. Variables are assigned types, but the types can change as Squirrel is a dynamically typed language. Some common primitive types are:

integer
string
float

Types don't need to (and can't) be defined upon creation, as every time a new value is assigned of a new type, the type automatically changes. A local variable declaration consists of the word local, followed by the name of the variable, and a semicolon. For example:
Code: [Select]
local nPlayerID
Variables can be assigned a value upon creation by having an equals sign followed by a value before the semicolon. For example:
Code: [Select]
local nPlayerID = 4; //Creates a variable nPlayerID with a default value of 4
local sPlayerName = "ext-d.CrystalBlue"; //Creates a variable sPlayerName with a default value of "ext-d.CrystalBlue"
Notice the quotation marks between ext-d.CrystalBlue. That is because all strings (character array, or simply text) are marked with quotation marks in Squirrel.

Boolean
Boolean algebra is when a mathematical expression is evaluated as either true or false. Therefore, a boolean value can only have one of two values: true or false. However, it evaluates entire expressions. Let's take a few expressions into consideration.

If I were to say 3 < (is less than) 6, obviously the statement is true.
If I were to say I am 5' 9" tall, this statement is also true.

However, if I were to say I am 5' 9" tall AND I am President of the United States, boolean logic states that the ENTIRE expression is false. While it may be true that I am 5' 9", I am not the President. Therefore, the entire expression is false.

A similar sentence: I am 5' 9" tall OR I am President of the United States.
Since only one part of the sentence has to be true, the entire sentence is considered true.

Note
0 is considered false, and 1 is considered true.

Boolean operators
There are three boolean operators: AND, OR, and NOT. In Squirrel:

AND is written as &&
OR is written as ||
NOT is written as !

Boolean operators are evaluated like so. For AND, the combination of two "True" values results in "True". Any other combinations returns False. For example:
True AND True is true.
True AND False is false.
False AND True is false.
False AND False is false.

For OR, as long as one value is true, the entire statement is true.

True OR True is true.
True OR False is true.
False OR False is false.
False OR True is true.

The NOT operator negates the value of an expression.
NOT True is false.
NOT False is true.

Comparison Operators

Boolean expressions most often involve comparison operators to see if they are true or false. These operators are equal to, greater than (or equal to), less than (or equal to), and not equal. In Squirrel:

Equal to is written as ==
Less than is written as <
Greater than is written as >
Less than or equal to is written as <=
Greater than or equal to is written as >=
Inequality is written as !=

Comparison operators form expressions that can be evaluated as true or false. For example, Number_of_Students > 30. If there are more than 30 students in the class, the entire expression is true. Otherwise, it is false.

Combining Boolean and Comparison Operators
We've looked at how boolean and comparison operators work to form expressions. Now, we'll look at how we can combine them to form more complex expressions.

If we wanted to know if there were more than 30 students in a class and if there were more than 30 seats, we could express this as:
Code: [Select]
Number_of_Students > 30 && Number_of_Seats > 30

Squirrel has to determine the boolean (true/false) value of each expression, then use those values to evaluate the expression as a whole. Let's say we declare two new variables:
Code: [Select]
local Number_of_Students = 35;
local Number_of_Seats = 38;

Squirrel will break this down into 35 > 30 && 38 > 30, which then evaluates to TRUE && TRUE. Therefore, the entire expression is true. The following example uses the following assumptions:

All employees work in a department.
All employees in department 5 have salaries greater than $25,000.
Alice is an employee in department 5.

Consider the truth of these few expressions:

Code: [Select]
Alice_salary < 25000 && Alice_department == 5
Alice_salary < 25000 is false. Alice_department == 5 is true. false && true is false.

Code: [Select]
!Alice_salary < 25000
Alice_salary < 25000 is false. Not false is true.

Thijn

  • VC:MP Beta Tester
  • Posts: 1,689

EK.IceFlake

  • Hero Member
  • "We are the champions my fellahs" - Sufyan/VK.SuFy
  • Posts: 1,761
Re: The absolute beginners guide to Squirrel
« Reply #2, on May 20th, 2016, 05:59 PM »
Quote from Thijn on May 20th, 2016, 05:57 PM
Part 1 to Absolute beginners guide to making a topic.

- Close your BBC tags.
- Profit.
I closed them and it told me mismatch. So I removed the closing tags and some stupid shit happened and millions of automatic brackets came. Decided to ignore them.

KAKAN

  • Wiki Contributor
  • Posts: 3,346
Re: The absolute beginners guide to Squirrel
« Reply #3, on May 21st, 2016, 10:34 AM »
Quote
Welcome to the absolute beginner's guide to Squirrel! Before you can get into the nooks and crannies of Pawn,
:D
Edit it pls. Either way, nice topic :D
You can contact me using Discord, at Developers.CPP( check off-board to find the invite link )
Github: https://github.com/theKAKAN

EK.IceFlake

  • Hero Member
  • "We are the champions my fellahs" - Sufyan/VK.SuFy
  • Posts: 1,761