An Introduction to Squirrel: Difference between revisions

From SigMod
Jump to navigation Jump to search
No edit summary
No edit summary
Tag: Manual revert
Line 111: Line 111:
single
single
//
//
\#
#
multi
multi
/* */
/* */

Revision as of 05:07, 26 October 2023

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 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 Squirrel Reference Manual for a more expedient learning experience more aimed at developers.


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:

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

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!"); then save the file. Go back in game and press your bind, you should see your text in console!

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.

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

Valid Identifiers:

  • INDEX
  • _weapon
  • weapon_sequence
  • player123

Invalid Identifiers:

  • starting-index
  • 1_ring_to_rule_them_all
  • BOLD&BRASH
  • $IMBATMAN

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.

Keywords
base break case catch class clone
continue const default delete else enum
extends for foreach function if in
local null resume return switch this
throw try typeof while yield constructor
instanceof true false static __LINE__ __FILE__
rawcall

To define a variable with x as it's identifier, we use the local keyword:

local x;

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 (=) operator in a separate statement, or both!

local x; local y = 10;

x = 5; y = 0;

Here we define x and initialize y to 10, and then set x to 5 and y to 0 afterwards.

Comments

single //

multi /* */

Data Types

integer, float, string, null, table, array, function, generator, class, instance, bool, thread and userdata.


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 ::= '@"'[.]* '"'

Expressions

Intro to Functions

Intro to Objects

Scope

Control Flow

if/else if/else for foreach while do while switch break continue

Exceptions

try catch throw

Enums

Arrays

Tables

Functions

Generators

Delegation

Weak References

Classes

Inheritance

Metamethods