Improved tutorial

Blixibon 2020-11-28 01:48:35 -06:00
parent e57a70dce2
commit f292879f17

@ -1,6 +1,8 @@
VScripts are a lot like the I/O system. Like how entities have their own inputs, entities also have their own functions that could be "fired" in VScript. 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 creating something which has practical advantages over the I/O system.
For this tutorial, we'll create a simple script which sets the player's health to the health of a NPC named "wyatt". In the I/O system, it would be difficult to get the health of an entity. You may have to use `logic_keyfield` or `logic_datadesc_accessor` *(both are Mapbase entities)* to get the entity's health. In VScript, this is much easier. For understanding VScript, it might be useful to look at it as being a lot like the I/O system: Like how entities have their own inputs, entities also have their own functions that could be "fired" in VScript.
For this tutorial, we'll create a simple script which sets the player's health to the health of a NPC. In the I/O system, it would be difficult to get the health of an entity. You may have to use `logic_keyfield` or `logic_datadesc_accessor` *(both are Mapbase entities)* to get the entity's health. In VScript, this is much easier.
--- ---
@ -12,15 +14,15 @@ Inside of the `scripts/vscripts` directory, create a `.txt` file and change its
Open the file using Notepad, Notepad++, or any other text editor. Open the file using Notepad, Notepad++, or any other text editor.
Inside of this script file, we will add code which finds the "wyatt" NPC by name, gets its health, and then sets the player's health to that number.
--- ---
### 2. Adding the code ### 2. Adding the code
Inside of this script file, we will add code which finds the "wyatt" NPC by name, gets its health, and then sets the player's health to that number. First, add `function SetPlayerHealthToNPC()` at the top of the file. This defines a new set of code which can be triggered later. You can technically use any name for this function, but the name `SetPlayerHealthToNPC` will be used in this tutorial for simplicity.
First, add `function SetPlayerHealthToNPC()` at the top of the file. This is where all of our code will be written. You can technically use any name for this function, but the name `SetPlayerHealthToNPC` will be used in this tutorial for simplicity. After you have added `SetPlayerHealthToNPC`, 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 `SetPlayerHealthToNPC`, 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:
@ -30,18 +32,18 @@ function SetPlayerHealthToNPC()
} }
``` ```
Now we will make the function actually find the "wyatt" NPC and get its health. Now we will make the function actually find the NPC and get its health. In this case, we will refer to the NPC as "wyatt", with the code searching for an entity which has the targetname "wyatt".
In between the `{` and `}` lines, add a new line with the text "local wyatt". In between the `{` and `}` lines, add a new line with the text "local wyatt =".
```squirrel ```squirrel
function SetPlayerHealthToNPC() function SetPlayerHealthToNPC()
{ {
local wyatt local wyatt =
} }
``` ```
Then, on that same line, add the text "= Entities.FindByName()". Then, on that same line, add the text "Entities.FindByName()".
```squirrel ```squirrel
function SetPlayerHealthToNPC() function SetPlayerHealthToNPC()
@ -52,7 +54,7 @@ function SetPlayerHealthToNPC()
`local wyatt` creates a new variable named "wyatt" inside of `SetPlayerHealthToNPC`. `local wyatt` creates a new variable named "wyatt" inside of `SetPlayerHealthToNPC`.
`Entities.FindByName()` is a VScript function which finds an entity in the map by its name, similar to how an output finds an entity and fires an input on it. The `=` makes the `wyatt` variable point to the NPC it finds. `Entities.FindByName()` is an internal function which finds an entity in the map by its name, similar to how an output finds an entity and fires an input on it. The `=` makes the `wyatt` variable use the NPC it finds.
This function takes parameters in between the parentheses, separated by commas. These parameters are similar to what you'd use in an output. To make `Entities.FindByName()` find "wyatt", it must use parameters in this order: This function takes parameters in between the parentheses, separated by commas. These parameters are similar to what you'd use in an output. To make `Entities.FindByName()` find "wyatt", it must use parameters in this order:
@ -149,7 +151,7 @@ function SetPlayerHealthToNPC()
} }
``` ```
Our code is now complete. `SetPlayerHealthToNPC` searches for an entity named "wyatt" and, if it exists, it sets the player's health to the entity's health. Our code is now complete. `SetPlayerHealthToNPC` searches for an entity named "wyatt" and, if it exists, it sets the player's health to wyatt's health.
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.
@ -161,7 +163,7 @@ 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_transfer_health.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_transfer_health.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. You can also run it on the "wyatt" NPC itself. 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. You can also run it on the "wyatt" NPC itself.
--- ---
@ -185,7 +187,7 @@ If this does not work, check the steps above and make sure you followed them cor
### 6. Conclusion ### 6. Conclusion
You have now written a basic script file which uses the functions on a class. This tutorial was meant to give a peek into what VScript is *really* capable of. The function we wrote can be useful in its own right, but it's also a starting point for more complicated logic. You have now written a basic script file which uses the functions on a class. This tutorial was meant to give a peek into some of VScript's advantages over the I/O system. The function we wrote can be useful in its own right, but it's also a starting point for more complicated logic.
For example, this is the script we used in the tutorial: For example, this is the script we used in the tutorial: