Created VScript tutorial for HL2 mappers (markdown)

Blixibon 2020-05-24 22:54:43 -05:00
parent cab432f2b5
commit e98d3ff3d0

@ -0,0 +1,67 @@
This article is meant to make VScript in Mapbase easy to understand if you've used logic entities and/or set up sourcemods before, particularly for Half-Life 2/Source SDK 2013. Prior coding experience is not required.
---
VScripts are a lot like the I/O system. Each entity can have its own scripts (similar to outputs) and these scripts can be influenced by inputs.
For this tutorial, we'll create a simple script which fires the `SetHealth` input on the player with a parameter of `85`, which sets the player's health to 85. In the I/O system, this can be done directly from an output. In-game, this can be done with the `ent_fire` command. This tutorial will tell you how this can be done in VScript.
---
### 1. Creating a script file
First, go to the `scripts/vscripts` directory of your mod. If that directory does not exist, create it.
Inside of the `scripts/vscripts` directory, create a `.txt` file. You can name it anything you want, like `test_script`. It should not have any spaces.
Replace the `.txt` extension with `.nut`.
Open the file using Notepad, Notepad++, or any other text editor.
---
### 2. Adding I/O code
Inside of this script file, we will add code which makes the player fire the `SetHealth` input with a parameter of `85`.
First, add `function SetPlayerHealthTo85()`. This will be what causes `SetHealth` to fire. Think of the function as an input in an I/O system. You can technically use any name for this function, but `SetPlayerHealthTo85` will be used in this tutorial for simplicity.
After you have added `SetPlayerHealthTo85`, add a `{` on the line below it, and then another `}` below that.
Your file should now look like this:
```squirrel
function MakePlayerDropWeapon()
{
}
```
Now we will make the function actually fire `SetHealth` on the player.
In between the `{` and `}` lines, add a new line with `EntFire()` inside.
```squirrel
function MakePlayerDropWeapon()
{
EntFire()
}
```
`EntFire()` is a VScript function which fires an input into the map, similar to the `ent_fire` command in the in-game console.
Like inputs, this function takes parameters in between the brackets, separated by commas. These parameters are similar to what you'd use in an output. To make `EntFire()` cause the player (`!player`) to have their health changed (`SetHealth`) to 85 (`85`), it must use parameters in this order:
```squirrel
EntFire("!player", "SetHealth", "85")
```
In the function itself, it would look like this:
```squirrel
function MakePlayerDropWeapon()
{
EntFire("!player", "SetHealth", "85")
}
```