Difference between revisions of "HPL3/Scripting/Scripting Guide/Helper Files"
Line 41: | Line 41: | ||
</syntaxhighlight>As you can see, the helper file <code>helper_player.hps</code> is included in the script file. If this file wasn't included, the game will display an error message and our map won't run. | </syntaxhighlight>As you can see, the helper file <code>helper_player.hps</code> is included in the script file. If this file wasn't included, the game will display an error message and our map won't run. | ||
− | == | + | == Creating Helper Files == |
− | + | You can create custom helper files for your mod in addition to using the game helper files: | |
− | |||
− | Add /script to | + | # In your mod folder create a <code>scripts</code> folder. |
+ | # Inside that folder, create a <code>helpers</code> folder. | ||
+ | # Inside that folder, create a file called <code>MyHelper.hps</code> (Or any other name). | ||
+ | # Add /script to the mod's <code>resources.cfg</code> file, so the game will search for script files in that folder. Your mod folder structure may look like this now: | ||
+ | |||
+ | <syntaxhighlight lang="css"> | ||
+ | MinimalCustomMapMod/ | ||
+ | ├── config/ | ||
+ | │ ├── lang/ | ||
+ | │ │ ├── english.lang | ||
+ | │ ├── main_init.cfg | ||
+ | ├── maps/ | ||
+ | ├── script/ | ||
+ | │ ├── helpers/ | ||
+ | │ │ ├── MyHelper.hps | ||
+ | ├── entry.hpc | ||
+ | ├── resources.cfg | ||
+ | </syntaxhighlight> | ||
+ | This is how your resources file may look like: | ||
+ | <syntaxhighlight lang="css"> | ||
+ | <Resources> | ||
+ | <Directory Path="/config" AddSubDirs="true" /> | ||
+ | <Directory Path="/maps" AddSubDirs="true" /> | ||
+ | <Directory Path="/script" AddSubDirs="true" /> | ||
+ | </Resources> | ||
+ | </syntaxhighlight> | ||
− | |||
We have two map files. Now, instead of copy-pasting our function Entity_Explode, we can just use the helper file and call that function. | We have two map files. Now, instead of copy-pasting our function Entity_Explode, we can just use the helper file and call that function. | ||
As you can see, using helper files reduces the size of our script file, and makes our code reusable everywhere. | As you can see, using helper files reduces the size of our script file, and makes our code reusable everywhere. | ||
− | |||
== See Also == | == See Also == |
Revision as of 21:13, 11 August 2020
This article is actively undergoing a major edit. The user who added this notice will be listed in its edit history should you wish to contact them. |
This article explains what helper files are and how to use them.
Introduction
When you're writing your script, you can find yourself ending up with a lot of code. If all this code is inside a single file, that can make things difficult to manage - whenever you want to add or change something, you have to pour through hundreds or thousands of lines of code looking for that one spot.
Also, what if you want to use some code that was written by someone else? Do you just copy their code and paste it into your own? What if they update their code, forcing you to find everywhere in your code that was updated and perform the update yourself?
There is an easier way. There's a special keyword that references a script file and “pastes” it on top of another file. This allows functions and other things to be called from that other file without requiring that the code is in the same place - the #include
keyword.
Using Helper Files
As the name suggests, we include external script files in our code, which allows us to call other functions from other files. There are several includes at the top of our map script file. Each include points to another script file, which are labeled as helpers, because they help us with performing common scripts and actions.
For example, if we want to use player related functions, we need to include helper_player.hps
:
#include "interfaces/Map_Interface.hps"
#include "base/Inputhandler_Types.hps"
#include "helpers/helper_map.hps"
#include "helpers/helper_props.hps"
#include "helpers/helper_effects.hps"
#include "helpers/helper_audio.hps"
#include "helpers/helper_imgui.hps"
#include "helpers/helper_sequences.hps"
#include "helpers/helper_game.hps"
#include "helpers/helper_modules.hps"
#include "helpers/helper_ai.hps"
// Required helper file for player related functions
#include "helpers/helper_player.hps"
//...Somewhere in your code:
////////////////////////////
// Run first time starting map
void OnStart()
{
Player_SetMoveSpeedMul(5.0f); // Here we use a function from the player helper file.
}
As you can see, the helper file helper_player.hps
is included in the script file. If this file wasn't included, the game will display an error message and our map won't run.
Creating Helper Files
You can create custom helper files for your mod in addition to using the game helper files:
- In your mod folder create a
scripts
folder. - Inside that folder, create a
helpers
folder. - Inside that folder, create a file called
MyHelper.hps
(Or any other name). - Add /script to the mod's
resources.cfg
file, so the game will search for script files in that folder. Your mod folder structure may look like this now:
MinimalCustomMapMod/
├── config/
│ ├── lang/
│ │ ├── english.lang
│ ├── main_init.cfg
├── maps/
├── script/
│ ├── helpers/
│ │ ├── MyHelper.hps
├── entry.hpc
├── resources.cfg
This is how your resources file may look like:
<Resources>
<Directory Path="/config" AddSubDirs="true" />
<Directory Path="/maps" AddSubDirs="true" />
<Directory Path="/script" AddSubDirs="true" />
</Resources>
We have two map files. Now, instead of copy-pasting our function Entity_Explode, we can just use the helper file and call that function.
As you can see, using helper files reduces the size of our script file, and makes our code reusable everywhere.