Updated VScript : Basic I:O Tutorial (markdown)

Blixibon 2020-05-25 09:23:19 -05:00
parent e820632ca1
commit 3799bb2d54

@ -68,15 +68,15 @@ 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. 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 put the file is already in `scripts/vscripts`, then you will only need to use "test_script" to refer to it in Hammer. 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 keyvalue or the input 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.
--- ---
### 4. Calling `SetPlayerHealthTo85` ### 4. Calling `SetPlayerHealthTo85`
When the entity runs the script, it will have the `SetPlayerHealthTo85` 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 fired, similar an output. We need to call that function from somewhere, which would then set the player's health to `85`.
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.
@ -89,3 +89,38 @@ You can fire `RunScriptCode` with this parameter by using something in the map o
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 `SetPlayerHealthTo85()` parameter. If you followed these steps correctly, the player's health will be set to 85.
If this does not work, check the steps above and make sure you followed them correctly. If this does not work, check the steps above and make sure you followed them correctly.
---
### 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 coding.
For example, this is the script we used in the tutorial:
```squirrel
function SetPlayerHealthTo85()
{
EntFire("!player", "SetHealth", "85")
}
```
Now look at this one:
```squirrel
function SetPlayerHealthToRandom()
{
local health = RandomInt( 55, 85 )
EntFire("!player", "SetHealth", health)
printl("Set player health to " + health)
}
```
This slightly different function, `SetPlayerHealthToRandom`, sets the player's health to a random number in between `55` and `85`, also printing the result to the console.
That is a peek at some of the more complicated logic you can use in VScript, where it actually becomes proper programming. There's even a way to set the player's health without using the I/O system at all.
---
* [Back to Mapbase's VScript Tutorials](https://github.com/mapbase-source/source-sdk-2013/wiki/VScript-in-Mapbase#Tutorials)