The absolute beginners guide to Squirrel

Started by EK.IceFlake, May 20, 2016, 04:53 PM

Previous topic - Next topic

EK.IceFlake

[spoiler]Yeah, just translating this to Squirrel[/spoiler]

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.
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:
local nPlayerIDVariables can be assigned a value upon creation by having an equals sign followed by a value before the semicolon. For example:
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:
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:
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:

Alice_salary < 25000 && Alice_department == 5Alice_salary < 25000 is false. Alice_department == 5 is true. false && true is false.

!Alice_salary < 25000Alice_salary < 25000 is false. Not false is true.

Thijn

Part 1 to Absolute beginners guide to making a topic.

- Close your BBC tags.
- Profit.

EK.IceFlake

Quote from: Thijn on May 20, 2016, 04:57 PMPart 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

QuoteWelcome 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
oh no

EK.IceFlake

pl0x remove your posts don't spam or I have to quit. pm me for errors. fixed. thanks for reporting.