Improved tutorial

Blixibon 2020-11-28 01:30:28 -06:00
parent 217046c604
commit e57a70dce2

@ -1,4 +1,6 @@
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. This is part of a set of basic VScript tutorials which explain how to use VScript with no programming experience. This particular tutorial focuses on firing an I/O input from within VScript. This is the same kind of input which would normally be triggered from an entity's output in Hammer or with the `ent_fire` console command.
For understanding VScript, it might be useful to look at it as being a lot like the I/O system: Each entity can have its own scripts (similar to outputs) and these scripts can be influenced by other scripts (or just 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. 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.
@ -16,11 +18,9 @@ Open the file using Notepad, Notepad++, or any other text editor.
### 2. Adding the code ### 2. Adding the code
Inside of this script file, we will add code which makes the player fire the `SetHealth` input with a parameter of `85`. Inside of this script file, we will begin by adding `function SetPlayerHealthTo85()` at the top of the file. This defines a new set of code which can be triggered later. In this case, it will be used to fire `SetHealth`. You can technically use any name for this function, but the name `SetPlayerHealthTo85` will be used in this tutorial for simplicity.
First, add `function SetPlayerHealthTo85()` at the top of the file. This will be what causes `SetHealth` to fire. You can technically use any name for this function, but the name `SetPlayerHealthTo85` will be used in this tutorial for simplicity. After you have added `SetPlayerHealthTo85`, add a `{` on the line below it. Then, add a `}` below that one. These are used to group code within the function.
After you have added `SetPlayerHealthTo85`, add a `{` on the line below it. Then, add a `}` below that one.
Your file should now look like this: Your file should now look like this:
@ -43,7 +43,7 @@ function SetPlayerHealthTo85()
`EntFire()` is a VScript function which fires an input into the map, similar to the `ent_fire` command in the in-game console. `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: 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, it must use parameters in this order:
```squirrel ```squirrel
EntFire("!player", "SetHealth", "85") EntFire("!player", "SetHealth", "85")
@ -58,7 +58,7 @@ function SetPlayerHealthTo85()
} }
``` ```
Whenever the `SetPlayerHealthTo85` function is called, it will fire the `SetHealth` input on the player with a parameter of `85`, which sets the player's health to 85. Whenever the `SetPlayerHealthTo85` function is triggered, it will fire the `SetHealth` input on the player with a parameter of `85`, which sets the player's health to 85.
Now that we've written our code, it's time to access it from the map. Now that we've written our code, it's time to access it from the map.
@ -70,13 +70,13 @@ Like outputs, an entity can run a VScript file and store its code until the enti
Entities can run VScript files through a keyvalue (**Entity scripts**) or an input (`RunScriptFile`). Both of these take the filename of the script(s) to run. The extension is optional and you do not need to insert `scripts/vscripts`. For example, if you named your file `test_script.nut` and the file is already in `scripts/vscripts`, then you will only need to use "test_script" as the keyvalue or parameter. Entities can run VScript files through a keyvalue (**Entity scripts**) or an input (`RunScriptFile`). Both of these take the filename of the script(s) to run. The extension is optional and you do not need to insert `scripts/vscripts`. For example, if you named your file `test_script.nut` and the file is already in `scripts/vscripts`, then you will only need to use "test_script" as the keyvalue or parameter.
In Hammer, create an entity (e.g. a `logic_relay` or an `info_target`) and use either the "Entity scripts" keyvalue or the `RunScriptFile` input on the entity to run your test file. In Hammer, you can create an entity (e.g. a `logic_relay` or an `info_target`) and use either the "Entity scripts" keyvalue or the `RunScriptFile` input on the entity to run your test file.
--- ---
### 4. Calling `SetPlayerHealthTo85` ### 4. Calling `SetPlayerHealthTo85`
When the entity runs the script, it will have the `SetPlayerHealthTo85` function stored and waiting to be fired, similar an output. We need to call that function from somewhere, which would then set the player's health to `85`. When the entity runs the script, it will have the `SetPlayerHealthTo85` function stored and waiting to be triggered, similar an output. We need to call that function from somewhere.
In the I/O system, this can be done by firing `RunScriptCode` on the entity running the script. This input should have a parameter of "SetPlayerHealthTo85()", which will tell the entity to call its `SetPlayerHealthTo85` function. In the I/O system, this can be done by firing `RunScriptCode` on the entity running the script. This input should have a parameter of "SetPlayerHealthTo85()", which will tell the entity to call its `SetPlayerHealthTo85` function.