Updated VScript : Basic Entity Code Tutorial (markdown)

Blixibon 2020-05-29 11:45:11 -05:00
parent 18e0ba935b
commit 6f249ca97a

@ -157,17 +157,17 @@ Now that we've written our code, it's time to access it from the map.
Like outputs, an entity can run a VScript file and store its code until the entity is removed.
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_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.
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.
---
### 4. Calling `SetPlayerHealthTo85`
### 4. Calling `SetPlayerHealthToNPC`
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 `SetPlayerHealthToNPC` 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 that of the "wyatt" NPC.
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 "SetPlayerHealthToNPC()", which will tell the entity to call its `SetPlayerHealthToNPC` function.
You can fire `RunScriptCode` with this parameter by using something in the map or by using the `ent_fire` console command in-game.
@ -175,7 +175,7 @@ You can fire `RunScriptCode` with this parameter by using something in the map o
### 5. Testing
Compile your map and load it in-game. Test the `RunScriptCode` input with the `SetPlayerHealthTo85()` parameter. If you followed these steps correctly, the player's health will be set to 85.
Compile your map and load it in-game. Test the `RunScriptCode` input with the `SetPlayerHealthToNPC()` parameter. If you followed these steps correctly and there is a NPC in the map named "wyatt", the player's health will be set to the health of that NPC.
If this does not work, check the steps above and make sure you followed them correctly.
@ -183,30 +183,43 @@ If this does not work, check the steps above and make sure you followed them cor
### 6. Conclusion
You have now written a basic script file which interacts with the I/O system. This tutorial was meant to give a simple interpretation of what VScript can do. The function we wrote was very simple and not very useful alone, but it's 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 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.
For example, this is the script we used in the tutorial:
```squirrel
function SetPlayerHealthTo85()
function SetPlayerHealthToNPC()
{
EntFire("!player", "SetHealth", "85")
local wyatt = Entities.FindByName(null, "wyatt")
if (wyatt != null)
{
local health = wyatt.GetHealth()
player.SetHealth(health)
}
}
```
Now look at this one:
```squirrel
function SetPlayerHealthToRandom()
function SetPlayerHealthToAllOfNPC()
{
local health = RandomInt( 55, 85 )
EntFire("!player", "SetHealth", health)
local health = 0
local wyatt
while((wyatt = Entities.FindByName(wyatt,"wyatt")) != null)
{
health += wyatt.GetHealth()
}
player.SetHealth(health)
}
```
This slightly different function, `SetPlayerHealthToRandom`, sets the player's health to a random number in between `55` and `85`.
This slightly different function, `SetPlayerHealthToAllOfNPC`, sets the player's health to the health of all NPCs named "wyatt" added together. If there's a headcrab named "wyatt" with `10` health and also a zombie named "wyatt" with `50` health, the player's health will be set to `60`.
That is a peek at some of the more complicated logic you can use in VScript, where it actually becomes proper coding. There's even a way to set the player's health without using the I/O system at all.
That is a peek at some of the more complicated programming you can do with VScript. `GetHealth()` and `SetHealth()` are also just two functions out of many that can be used to have finer control than what I/O and logic entities are capable of.
---