Portal 2 Guide

Portal 2 Hammer Vscript Tutorial for Portal 2

Portal 2 Hammer Vscript Tutorial

Overview

This is guide for people new to Portal 2 Scripting!

Required Stuff

Notepadd++
[link]

Portal 2 Authoring Tools
Install through steam[http//steam]

Your first script!

This section will teach you how to setup scripts for Portal 2.

Language which we will be using is squirrel. Open your notepad++, create new file and save as .nut file anywhere inside Portal 2/portal2/scripts/vscripts/ folder.

If you won’t save it in propper location, Portal 2 wont read it!

Functions!

Now we are going to make our first function. Open your script file via notepad++.
Now type inside it.

function yourFunctionName() { //Code goes here }

yourFunctionName() is your function name, you can call it how you want for example: TreasureGhostLikeGrape. Remember to put () at end of function name, same as I did.

Sending text to console!

In this part I’m going to show you how to send text to console, to make that I’m going to use printl().
Open your script, and type
printl(“Your text goes here”) , also don’t forget to put ; at the end of line.

Using script in Hammer!

Script can be activated by any output and located in almost all entities but I’d recomend using logic_script entity for scripts.

Now open your logic_script, give it a name. Then add script to it. To do that go to its proporties, find entity script tab and press menage. Big script menager dialog will show up. Press + at bottom of it and open your script.

Last thing to do is output to launch it. Open your output, add onSmth -> logic_script -> RunScriptCode -> your function name

Checking results.

Now run your map with script, open console and type developer 2, this will let you see all input and outputs in console and top left corner of your screen.
Activate your logic_script and check your console!

Variables!

What is variable? And why I need it?

Variable is symbolic name associated with a value and whose associated value may be changed.

Kinds of variables
  • integer
  • float
  • string
  • vector
  • array
  • boolean
    Integer

    Integer is a variable that represents number.

    local integer = 3;

    Float

    Float is simmilar to integer, however it can save fractions.

    local float = 3.5;

    String

    String is a variable that stores text.

    local String = “Hello World!”;

    Vector

    Vector is a variable that saves location in 3d world (x,y,z).

    local vector; vector = Vector(1, 2, 3);

    Boolean

    Boolean is a variable that storage True/False.

    local boolean = true;

Creating and modifying variables!

This piece of tutorial will show you how to create and use varriables.

To create varriable, open your script, and type anywhere:

local yourVariableName = “Hello World”

Now inside your function create printl() function like this:

printl(yourVariableName)

Your code should look like this:

function yourFunctionName() { local myVariable = “guineaPig”; printl(myVariable); }
Checking results!

Now your text should appear in your console like this!

Actions on variables!

As I mentioned in previous part, variables can be modified, now I’m going to show you how.
Create 3 integer variables, and give them diffrent values.

local myValue1 = 21; local myValue2 = 43; //Now add them local myValue3 = myValue1 + myValue2; printl(myValue3);
Checking results!

As you see, this two variables were added to them selves. You can add, subtract, devide, multiply, intensify and square root them!

if() … else and while()!

if().. else

if() is small function, that runs only if the parameter meets, otherwise function will use “else” if avible.
Example:

function yourFunctionName() { local apple = true; //if starts here if(apple == false) { printl(“apple = false”); } else { printl(“apple = true”); } }

This is what you will get:

Now check what will happent when you will set apple as false.

while()

while() is a logic loop that will be occuring until parametr wont meets.
Example:

function yourFunctionName() { local xNumber = 0; local yNumber = 5; local zNumber = yNumber + 1; while(xNumber != zNumber) { printl(xNumber); xNumber += 1; } }

This function will write numbers from 0 to yNumber value.
Try changing yNumber value to diffrent values.

Script Functions!

You can find all Sript Functions here:
[link]

I changed script holding entity to cube, here is example of function usage:

function yourFunctionName() { local vector; vector = Vector(0,0,0); self.SetOrigin(Vector(vector)); //this will teleport cube to 0, 0, 0 coordinates }

Another Example

function yourFunctionName() { EntFire( “cubex”, “kill”, ” “, 0.0 ); // this will kill my cube }

Video tutorials by Linking Yellow!

Links!

[link]
[link]
[link]

SteamSolo.com