Editing An Introduction to Squirrel

Jump to navigation Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
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 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.
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.


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.
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.




== [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 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:
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:
* [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. (Mine will be named testing.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.
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 "script_execute testing".
For example: bind 5 "script_execute testing"


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>.
Tab out of your game, open your .nut file and type: printl("Hello World!"); then save the file. Go back in game and press your bind, you should see your text in console!


For example: <code>bind 5 "script_execute testing"</code>


== [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 a name (aka identifier) 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.


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


<syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;>
Identifiers have a few considerations to keep in mind:
printl("Hello World!");
* '''They are case-sensitive, wep is not the same as WEP'''
</syntaxhighlight>
* '''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'''
<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!


'''Valid Identifiers:'''
* INDEX
* _weapon
* weapon_sequence
* player123


<hr>
'''Invalid Identifiers:'''
* starting-index
* 1_ring_to_rule_them_all
* BOLD&BRASH
* $IMBATMAN




{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0;"
In addition, identifiers cannot be the same name as any keyword, which are reserved by the language for its use. We'll go over most of these throughout the guide, so don't worry about memorizing them.
{| class="wikitable" style="width:75%; margin:auto; text-align:center font-weight:bold; font-size:125%"
|+Keywords
|base
|break
|case
|catch
|class
|clone
|-
|-
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
|continue
{| class="wikitable mw-collapsible mw-collapsed" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
|const
! colspan="1" style="padding: 25px; background-color: #303030; color: white; border: 2px solid black" | Chapters
|default
|delete
|else
|enum
|-
|-
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
|extends
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
|for
! 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]
|foreach
|}
|function
|if
|in
|-
|-
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
|local
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
|null
! 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]
|resume
|}
|return
|switch
|this
|-
|-
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
|throw
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
|try
! 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]
|typeof
|}
|while
|yield
|constructor
|-
|-
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
|instanceof
{| class="wikitable" style="margin: 0 auto; width: 100%; padding: 0; border: none;"
|true
! 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]
|false
|}
|static
|__LINE__
|__FILE__
|-
|-
| colspan="1" style="padding: 0; margin: 0; width: 100%;" |
|rawcall
{| 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]
|}
|-
| 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_Tables Tables]
|}
|-
| 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_Functions 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_Generators Generators]
|}
|-
| 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_Delegation Delegation]
|}
|-
| 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_Weak_References Weak References]
|}
|-
| 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]
|}
|-
| 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_Inheritance Inheritance]
|}
|-
| 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_Metamethods Metamethods]
|}
|}
|}
|}
<div style="margin-top: 50px;">To define a variable with x as it's identifier, we use the <code>'''local'''</code> keyword:</div>
<syntaxhighlight lang="c" line="1" start="1" style="font-weight:bold;">
local x;
</syntaxhighlight>
We created our variable in this statement but we haven't given it a value, so currently the value for x is null by default (we'll go over what null is later in Data Types). To give our variable a 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!
<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 newline 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 = "bar";
</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] ==
<nowiki>
integer, float, string, null, table, array, function, generator, class, instance, bool, thread and userdata.
== [https://sigwiki.potato.tf/index.php?title=An_Introduction_to_Squirrel Literals and Operators] ==
! != || == && >= <= >
<=> + += - -= / /= *
*= % %= ++ -- <- = &
^ | ~ >> << >>>  ?: <=> 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 ::=  '@''"'[.]* '"'
</nowiki>
== [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] ==
Please note that all contributions to SigMod are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see SigMod:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)