An Introduction to Squirrel: Difference between revisions

From SigMod
Jump to navigation Jump to search
No edit summary
No edit summary
 
(16 intermediate revisions by the same user not shown)
Line 1: Line 1:
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Overview] ==
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Overview] ==
This is a relatively brief beginner's guide to the Squirrel language (v3.2) aimed at people with no programming experience interested in trying their hand at [https://developer.valvesoftware.com/wiki/VScript VScript]. The only prerequisite knowledge you are expected to have is basic knowledge of the Source engine. While in theory this guide is not technically specific to any Source game in particular, it is for version 3.2 which is unique as TF2's version of Squirrel. L4D2, Portal, and other Source games run on older versions, so in practice this guide is directed at TF2 players. If you're following this guide for another Source game that's perfectly fine, just be aware that some features may not function, or they might behave differently in your game's version of Squirrel. You'll also need to store your .nut files in the appropriate place of whatever game you're using, rather than the tf/scripts/vscripts folder.
This is a relatively brief beginner's guide to the Squirrel language (v3.2) aimed at people with little to no programming experience. While this guide is intended to be used by people wanting to learn VScript for TF2, it doesn't cover anything regarding how to use VScript itself.


If you already have experience with C-like languages or programming in general I would recommend you visit the [http://squirrel-lang.org/squirreldoc/reference/index.html Squirrel Reference Manual] for a more expedient learning experience more aimed at developers.
If you already have experience with C-like languages or programming in general I would recommend you visit the [http://squirrel-lang.org/squirreldoc/reference/index.html Squirrel Reference Manual] for a more expedient learning experience aimed at developers.




== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Getting Started] ==
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Getting Started] ==
While it's possible to compile the Squirrel source code into binaries with which you can test your code outside of a Source game, simply launching a game and executing your script is much simpler and allows us to test game specific code, so that's what we'll do for the duration of the guide. For guides on VScript itself, the VDC has an excellent collection:
While it's possible to compile the Squirrel source code into binaries with which you can test your code outside of a Source game, simply launching a game and executing your script is much simpler and allows you to start tinkering with Squirrel quicker, so that's what we'll do for the duration of the guide. For guides on VScript itself, the VDC has an excellent collection:
* [https://developer.valvesoftware.com/wiki/VScript_Fundamentals VScript Fundamentals]
* [https://developer.valvesoftware.com/wiki/VScript_Fundamentals VScript Fundamentals]
* [https://developer.valvesoftware.com/wiki/Entity_Scripts Entity Scripts]
* [https://developer.valvesoftware.com/wiki/Entity_Scripts Entity Scripts]
Line 13: Line 13:
* [https://developer.valvesoftware.com/wiki/Team_Fortress_2/Scripting/VScript_Examples/en VScript Examples]
* [https://developer.valvesoftware.com/wiki/Team_Fortress_2/Scripting/VScript_Examples/en VScript Examples]


To get set up, navigate to your tf/ directory and add a scripts/vscripts folder if it doesn't already exist and create a file with any name with the extension .nut.
To get set up, navigate to your tf/ directory and add a scripts/vscripts folder if it doesn't already exist and create a file with any name with the extension .nut. (Mine will be named testing.nut)
You should have a location something like this: C:\Program Files (x86)\Steam\steamapps\common\Team Fortress 2\tf\scripts\vscripts\testing.nut
 
You should have a location something like this: <pre>C:\Program Files (x86)\Steam\steamapps\common\Team Fortress 2\tf\scripts\vscripts\testing.nut</pre>
 
Go ahead and launch your favorite Source game which supports VScript and load into any map on a local server, then bind any key to <code>"script_execute testing"</code>.
 
For example: <code>bind 5 "script_execute testing"</code>


Go ahead and launch your favorite Source game which supports VScript and load into any map on a local server, then bind any key to "script_execute testing".
For example: bind 5 "script_execute testing"


Tab out of your game, open your .nut file and type:
Tab out of your game, open your .nut file and type:
Line 25: Line 28:
</syntaxhighlight>
</syntaxhighlight>


Save the file then go back in game and press your bind, you should see your text in console!
<code>'''printl'''</code> is a function that prints to the developer console, we'll go over what functions are and the syntax for it later in the guide, for now just follow along. After saving the file and pressing our script button in-game, you'll see <code>Hello World!</code> in console.


You just made your first program in VScript! To get started with the guide, choose a chapter in the table below. Make sure to tinker with the examples in the guide to see first hand how Squirrel works!


Good luck!


== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Variables] ==
Every program in any programming language is comprised of a set of instructions called statements which manipulate data to produce a desired result. Programs store data into regions of memory as objects. Objects which we give an identifier (name) in our programs are called variables. You can think of statements as analogous to sentences in the language we speak to each other, each requires a specific set of items to be considered valid, and each is ended with a specific character. Sentences are mostly ended with periods, and statements always end with a semicolon.


<hr>


Identifiers have a few considerations to keep in mind:
* '''They are case-sensitive, wep is not the same as WEP'''
* '''The first character must be alphabetical (a-z) or an underscore, afterwards they can have any combination of (a-z), (0-9), or underscores'''
* '''Underscores are the only special character allowed, nothing else'''


 
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0;"
'''Valid Identifiers:'''
|-
* INDEX
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
* _weapon
{| class="wikitable mw-collapsible mw-collapsed" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
* weapon_sequence
! colspan="1" style="padding: 25px; background-color: #303030; color: white; border: 2px solid black" | Chapters
* player123
|-
 
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
'''Invalid Identifiers:'''
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
* starting-index
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Variables Variables]
* 1_ring_to_rule_them_all
|}
* BOLD&BRASH
|-
* $IMBATMAN
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
 
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
 
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Comments Comments]
In addition, identifiers cannot be the same name as a keyword, which are special identifiers reserved by the language for its use in the syntax. We'll go over most of these throughout the guide, so don't worry about memorizing them just yet.
|}
{| class="wikitable" style="width:75%; margin:auto; text-align:center font-weight:bold; font-size:125%"
|-
|+Keywords
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
|base
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
|break
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Data_Types Data Types]
|case
|}
|catch
|-
|class
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
|clone
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Operators Operators]
|}
|-
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Expressions Expressions]
|}
|-
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Intro_to_Functions Intro to Functions]
|}
|-
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Intro_to_Objects Into to Objects]
|}
|-
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Scopes Scopes]
|}
|-
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Control_Flow Control Flow]
|}
|-
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Exceptions Exceptions]
|}
|-
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Enums Enums]
|}
|-
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Arrays Arrays]
|}
|-
|-
|continue
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
|const
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
|default
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Tables Tables]
|delete
|}
|else
|enum
|-
|-
|extends
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
|for
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
|foreach
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Functions Functions]
|function
|}
|if
|in
|-
|-
|local
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
|null
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
|resume
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Generators Generators]
|return
|}
|switch
|this
|-
|-
|throw
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
|try
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
|typeof
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Delegation Delegation]
|while
|}
|yield
|constructor
|-
|-
|instanceof
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
|true
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
|false
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Weak_References Weak References]
|static
|}
|__LINE__
|__FILE__
|-
|-
|rawcall
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
|
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
|
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Classes Classes]
|
|
|
|}
|}
 
|-
<div style="margin-top: 50px;">To define a variable with x as it's identifier, we use the <code>'''local'''</code> keyword:</div>
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
 
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
<syntaxhighlight lang="c" line="1" start="1" style="font-weight:bold;">
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Inheritance Inheritance]
local x;
|}
</syntaxhighlight>
|-
 
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
We created our variable in this statement but we haven't given it a value, so currently the value for x is <code>'''null'''</code>, which is a special value representing the lack of a value. To give our variable an actual value we have two options; we can set it at the time of definition (which is called initialization), or we can assign it a value with the assignment (<code>'''='''</code>) operator in a separate statement, or both!
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
 
! colspan="1" style="border: 1px solid black; padding-right:64px; background-color: #606060; color: #111111; height: 35px;" | [https://sigwiki.potato.tf/index.php/Squirrel_Metamethods Metamethods]
 
<syntaxhighlight lang="c" line="1" start="1" style="font-weight:bold;>
local x;
local y = 10;
 
x = 5;
y = 0;
</syntaxhighlight>
 
Here we define x and initialize y to 10, and then set x to 5 and y to 0 afterwards.
 
 
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Comments] ==
A comment is a human readable note which was inserted by a programmer to denote the what, how, or why behind code. Comments are ignored by the compiler and only serve to make the code easier to understand or use for programmers. Comments come in two flavors: single line and multi line comments. Single line comments go until they hit a new line character (\n). Multi line comments have a start and end token, anything between them is commented out.
 
 
To insert a single line comment, prefix it with <code>'''//'''</code> or <code>'''#'''</code>.
 
<syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;>
// I'm a comment that takes the whole line
local darth_variable = 404 # I'm an inline comment
</syntaxhighlight>
 
To insert a multi line comment, use <code>'''/*'''</code> to start and <code>'''*/'''</code> to end.
 
<syntaxhighlight lang="c" line="1" start="1" style="font-weight:bold;>
/*I
      am
          a
      multi
  line
comment
*/
local foo = 2;
</syntaxhighlight>
 
You can "nest" single line comments, but you can't do this with multi line comments
 
<syntaxhighlight lang="c" line="1" start="1" style="font-weight:bold;>
//////////////////////// okay
/* // okay */
/* /* not okay */ */
</syntaxhighlight>
 
The compiler will end the multi line comment at the first end token (*/) it sees, so the second one is outside of any comments and results in a compile error.
 
 
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Data Types] ==
Data stored in variables can have a number of different types which determines what type of object it is and what you can do with it. Below is the full list of data types in Squirrel, however we will only go over the primitive ones in this section. The other data types will receive their own sections later in the guide.
 
'''Data Types:'''
* Null
* Integer
* Bool
* Float
* String
* Table
* Array
* Function
* Generator
* Class
* Instance
* Thread
* Userdata
 
 
To get a variable's data type as a string (see below on strings), you can use the <code>'''typeof'''</code> operator.
<syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;>
local t = typeof 5; // "integer"
</syntaxhighlight>
 
 
<hr>
 
 
<span style="font-size:120%">'''Null'''</span>
 
Has one value: <code>'''null'''</code>, it represents the non-existence or lack of a value.
 
<syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;>
local x; // null
 
local y = 5;
// ...
y = null;
</syntaxhighlight>
 
 
<hr>
 
 
<span style="font-size:120%">'''Integer'''</span>
 
Represents a 32 bit whole number, meaning it can store any value from -2,147,483,648 to 2,147,483,647. You can specify the integer base as [https://en.wikipedia.org/wiki/Hexadecimal hexadecimal] by prefixing with <code>'''0x'''</code> or [https://en.wikipedia.org/wiki/Octal octal] with <code>'''0'''</code>. You can also specify [https://www.asciitable.com/ ASCII] char codes between single quotes '''<code>'</code>''' which will be stored as their integer value.
 
<syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;>
local x = 12345;    // Decimal
local y = 0xFFFFFF; // Hexadecimal begins with 0x
local z = 050;      // Octal begins with 0
local c = 'a';     // Char code stored as 97
</syntaxhighlight>
 
These are all integer '''literals''', which are hard coded pieces of data within a program for a specific data type. Other types have literals as well which you'll see in the examples below.
 
 
<hr>
 
 
<span style="font-size:120%">'''Bool'''</span>
 
Has two values: <code>'''true'''</code> and  <code>'''false'''</code>.
 
<syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;>
local is_stuck    = false;
local should_round = true;
 
</syntaxhighlight>
 
 
<hr>
 
 
<span style="font-size:120%">'''Float'''</span>
 
Represents a 32 bit floating point number, meaning it can store any decimal value from 1.18e-38 to 3.4e38. Due to how floating point numbers are stored in memory, they have a limited amount of accuracy for a certain number of significant digits, for a 32 bit float it's usually around 6 or 7 digits. Any additional digits for the float value may result in rounding errors and precision loss. Keep this in mind when you're working with floats.
 
<syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;>
local float1 = 1.23456;  // 1.23456 (Regular float literal)
local float2 = 1.2e34;  // 1.2e+34  (Scientific notation float literal)
 
// Precision loss
local float3 = 1.234567; // 1.23457
local float4 = 1234.567; // 1234.57
local float5 = 123456.7; // 123457
local float6 = 12345678999.0; // 1.23457e+10
</syntaxhighlight>
 
You cannot have a leading floating point for float literals, they must start with at least one digit.
 
<syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;>
// Invalid
local float1 = .123;
local float2 = .12E5;
 
// Valid
local float3 = 0.123;
local float4 = 0.12E5;
</syntaxhighlight>
 
 
<hr>
 
 
<span style="font-size:120%">'''String'''</span>
 
Strings are a sequence of characters with any length between quotation marks <code>"</code>. Strings are immutable, meaning they cannot be modified and you must create a new string if you need to change the old one. Regular strings must begin and end on the same line, they cannot contain any new line characters (\n).
 
<syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;>
// Strings ...
local str1 = "'''@!(&(*@!%&!(%!%(%&FJSGADFJKL@fadjsh141a=-"; // String
local str2 = "123"; // String with number characters, not an integer
local str3 = "a";  // String
local str4 = "";    // Empty string
 
// ... compared to char codes
local int1 = 'a';  // NOT a string
local int2 = '';    // Compile error
local int3 = ''';  // Compile error
local int4 = '\'';  // 39, Valid char code for ' (explained below)
 
// Need to make a new string if we want to modify
local name = "bar";
name = "barber";
</syntaxhighlight>
 
 
Strings (and char literals) may contain [https://en.cppreference.com/w/c/language/escape escape sequences], which special are characters that denote that something special should happen. Even though they have multiple characters in their definition, they count as one character when actually in use.
Here are the ones you're most likely to use:
 
* <code>'''\t'''</code> - Display a tab
* <code>'''\n'''</code> - Move to a new line
* <code>'''\\'''</code> - Insert \ char
* <code>'''\"'''</code> - Insert " char
* <code>'''\''''</code> - Insert ' char
 
<syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;>
local text  = "Hello there! Please enter your class:\t";    // Tab
local text2 = "What do you mean you don't \"have one\"?\n"; // " char and new line
</syntaxhighlight>
 
 
There may come a time when you need a string that does not interpret escape sequences or that can span multiple lines, which is where verbatim strings come in handy. Verbatim strings begin with <code>'''@"'''</code> and end with a regular quote <code>'''"'''</code>.
 
<syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;>
local mystring = @"eenie
meenie
minie
moe";
</syntaxhighlight>
 
You may also need to add quote characters within the verbatim string, to do so you double up each quotation <code>'''""'''</code>. The double quote will be replaced with a single quote by the compiler.
 
<syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;>
local mystring = @"eenie
""meenie""
minie
moe";
</syntaxhighlight>
 
 
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Operators] ==
Now that you know how to create data and store it into variables, we can now go over how to modify and operate on that data. Operators are used for this purpose, they take one or more operands and produce a result value. Operators which take one operand are called unary operators, two operands are binary operators, and three are ternary operators.
 
 
<hr>
 
 
<span style="font-size:120%">'''Arithmetic'''</span>
 
'''Unary Operators'''
{| class="wikitable" style="width:75%; margin:auto; text-align:center font-weight:bold; font-size:110%"
|+
!Operator
!Symbol
!Form
!Operation
|- style="text-align:center"
|Unary minus
| -
| -x
|Negation of x
|}
|}
<syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;>
local x = 50;
x = -x; // -50
</syntaxhighlight>
'''Binary Operators'''
{| class="wikitable" style="width:75%; margin:auto; text-align:center font-weight:bold; font-size:110%"
|+
!Operator
!Symbol
!Form
!Operation
|- style="text-align:center"
|Addition
| +
| x + y
|x plus y
|- style="text-align:center"
|Subtraction
| -
| x - y
|x minus y
|- style="text-align:center"
|Multiplication
| *
| x * y
|x multiplied by y
|- style="text-align:center"
|Division
| /
| x / y
|x divided by y
|- style="text-align:center"
|Remainder
| %
| x % y
|the remainder from: x divided by y
|}
|}
<syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;>
local v = 100 + 100; // 200
local w = 0 - 2;    // -2
local x = 5.5 * 100; // 550.0
local y = 20 / 2;    // 10
local z = 23 % 2;    // 1
</syntaxhighlight>
'''Assignment Operators'''
{| class="wikitable" style="width:75%; margin:auto; text-align:center font-weight:bold; font-size:110%"
|+
!Operator
!Symbol
!Form
!Operation
|- style="text-align:center"
|Assignment
| =
| x = y
|assign value y to variable x
|- style="text-align:center"
|Addition Assignment
| +=
| x += y
|assign x + y to variable x
|- style="text-align:center"
|Subtraction Assignment
| -=
| x -= y
|assign x - y to variable x
|- style="text-align:center"
|Multiplication Assignment
| *=
| x *= y
|assign x * y to variable x
|- style="text-align:center"
|Division Assignment
| /=
| x /= y
|assign x / y to variable x
|- style="text-align:center"
|Remainder Assignment
| %=
| x %= y
|assign x % y to variable x
|}
|}
<syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;>
local x = 100;
x += 5;  // instead of x = x + 5
x -= 5;  // 100
x *= 100; // 10,000
x /= 2;  // 5,000
x %= 2;  // 0
</syntaxhighlight>
<hr>
The addition operators <code>'''+'''</code> and <code>'''+='''</code> are able to handle string operands. If one operand is a string, the compiler will try to convert the other operand to a string as well.
This same type conversion happens between floats and integers for any arithmetic operator (not just addition). If one operand is a float and the other an integer, the resulting value will be a float.
<syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;>
local str1 = "Hello,";
local str2 = " World!";
str1 += str2; // Hello World!
local float = 10 + 10.0 // 20.0
</syntaxhighlight>
! != || == && >= <= >
<=> + += - -= / /= *
*= % %= ++ -- <- = &
^ | ~ >> << >>>  ?: <=> in instanceof typeof ,
-, ~, !, typeof , ++, -- highest
/, *, % …
+, -
<<, >>, >>>
<, <=, >, >=, instanceof
==, !=, <=>
&
^
&&, in
+=, =, -=, /=, *=, %= …
, (comma operator) lowest
IntegerLiteral        ::=  [1-9][0-9]* | '0x' [0-9A-Fa-f]+ | ''' [.]+ ''' | 0[0-7]+
FloatLiteral          ::=  [0-9]+ '.' [0-9]+
FloatLiteral          ::=  [0-9]+ '.' 'e'|'E' '+'|'-' [0-9]+
StringLiteral        ::=  '"'[.]* '"'
VerbatimStringLiteral ::=  '@''"'[.]* '"'
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Expressions] ==
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Intro to Functions] ==
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Intro to Objects] ==
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Scope] ==
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Control Flow] ==
if/else if/else
for
foreach
while
do while
switch
break
continue
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Exceptions] ==
try catch
throw
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Enums] ==
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Arrays] ==
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Tables] ==
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Functions] ==
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Generators] ==
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Delegation] ==
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Weak References] ==
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Classes] ==
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Inheritance] ==
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Metamethods] ==

Latest revision as of 21:25, 22 January 2024

Overview[edit | edit source]

This is a relatively brief beginner's guide to the Squirrel language (v3.2) aimed at people with little to no programming experience. While this guide is intended to be used by people wanting to learn VScript for TF2, it doesn't cover anything regarding how to use VScript itself.

If you already have experience with C-like languages or programming in general I would recommend you visit the Squirrel Reference Manual for a more expedient learning experience aimed at developers.


Getting Started[edit | edit source]

While it's possible to compile the Squirrel source code into binaries with which you can test your code outside of a Source game, simply launching a game and executing your script is much simpler and allows you to start tinkering with Squirrel quicker, so that's what we'll do for the duration of the guide. For guides on VScript itself, the VDC has an excellent collection:

To get set up, navigate to your tf/ directory and add a scripts/vscripts folder if it doesn't already exist and create a file with any name with the extension .nut. (Mine will be named testing.nut)

You should have a location something like this:

C:\Program Files (x86)\Steam\steamapps\common\Team Fortress 2\tf\scripts\vscripts\testing.nut

Go ahead and launch your favorite Source game which supports VScript and load into any map on a local server, then bind any key to "script_execute testing".

For example: bind 5 "script_execute testing"


Tab out of your game, open your .nut file and type:

printl("Hello World!");

printl is a function that prints to the developer console, we'll go over what functions are and the syntax for it later in the guide, for now just follow along. After saving the file and pressing our script button in-game, you'll see Hello World! in console.

You just made your first program in VScript! To get started with the guide, choose a chapter in the table below. Make sure to tinker with the examples in the guide to see first hand how Squirrel works!

Good luck!




Chapters
Variables
Comments
Data Types
Operators
Expressions
Intro to Functions
Into to Objects
Scopes
Control Flow
Exceptions
Enums
Arrays
Tables
Functions
Generators
Delegation
Weak References
Classes
Inheritance
Metamethods