Frictional Games Forum (read-only)
Useful custom script functions - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Articles (https://www.frictionalgames.com/forum/forum-40.html)
+---- Thread: Useful custom script functions (/thread-53193.html)



Useful custom script functions - Mudbill - 11-08-2016

I figured I wanted to share some custom functions I've written recently. Amnesia/HPL2 is quite limited in what you can do, so I've used some workarounds to achieve what could've been done easily with external support. This might be a bit of an advanced topic for some of you, dunno.

What do you mean by custom script functions?
Spoiler below!

We all know how useful this function is, right?

PHP Code:
void SetLocalVarInt(stringasNameint alVal); 

This function is pre-defined in HPL2, and that's why you can use it. Anything listed on the Engine Scripts page is pre-defined. Everything else has to be created within the file in order to be used. This of course excludes the primitive syntax native to the scripting language.

So if you wanted to use a non-existant function like
PHP Code:
void SetLocalVarBool(stringasNamebool abVal); 
then you'd need to create it. Now, this particular one isn't exactly game-changing, since the same effect can be achieved by using the VarInt function as 0/1, but it can be convenient for those who work with booleans more since you can directly input the variable without converting.

How do I use these functions?
Spoiler below!

It's simple enough. Each function depends on a snippet of code. Include the snippet for the function(s) you want to use within your .hps file. As long as it's present within the same file, you can call it like any of the pre-defined functions.

I will list the function itself as it is listed on the Engine Scripts page. I will give a brief explanation of the function and provide the script you need to include in order to use it.

Below I'll list whatever I've published so far below.




Boolean variables
Spoiler below!
These add to the already existing set for Int, Float and String.
Pretty self explanatory what they do; allow you to create and return a local or global true/false variable. These rely on there not already existing an Int variable with the name "bool_yourvar" (shouldn't really be an issue).

PHP Code:
void SetLocalVarBool(stringasNamebool abVal);
void SetGlobalVarBool(stringasNamebool abVal); 

Sets a local or global boolean variable.

PHP Code:
bool GetLocalVarBool(stringasName);
bool GetGlobalVarBool(stringasName); 

Returns a local or global boolean variable.

Script to include
PHP Code:
void SetLocalVarBool(string &in asNamebool abVal) {
    
SetLocalVarInt("bool_"+asNameabVal == true 0);
}
void SetGlobalVarBool(string &in asNamebool abVal) {
    
SetGlobalVarInt("bool_"+asNameabVal == true 0);
}
bool GetLocalVarBool(string &in asName) {
    return 
GetLocalVarInt("bool_"+asName) == true false;
}
bool GetGlobalVarBool(string &in asName) {
    return 
GetGlobalVarInt("bool_"+asName) == true false;



String array variables
Spoiler below!
These are rather interesting and can be quite useful. They mimic the use of the array data type which is unsupported in HPL2. If you know how arrays work, these are self-explanatory.

These rely on there not existing string vars named "array_yourvar" and int vars named "array_yourvar_index". Can be changed of course.

PHP Code:
void SetLocalVarStringArray(string &in asNamestring[] &in asData);
void SetGlobalVarStringArray(string &in asNamestring[] &in asData); 

Sets a local or global string array to the specified string array.

asName - The name of the variable.
asData - The string array to set.

PHP Code:
void AddLocalVarStringArray(string &in asNamestring &in asText);
void AddGlobalVarStringArray(string &in asNamestring &in asText); 

Adds a string to the next index of a string array.

asName - The name of the variable.
asText - The string to add.

PHP Code:
string[] GetLocalVarStringArray(string &in asName);
string[] GetGlobalVarStringArray(string &in asName); 

Returns a string array.

asName - The name of the variable.

Script to include

PHP Code:
void SetLocalVarStringArray(string &in asNamestring[] &in asData) {
    
int size asData.length();
    for(
int i=0;i<size;i++)
        
AddLocalVarString("array_"+asName+iasData[i]);
    
SetLocalVarInt("array_"+asName+"_index"size);
}
void AddLocalVarStringArray(string &in asNamestring &in asText) {
    
SetLocalVarString("array_"+asName+
        
GetLocalVarInt("array_"+asName+"_index"), asText);
    
AddLocalVarInt("array_"+asName+"_index"1);
}
string[] GetLocalVarStringArray(string &in asName) {
    
string[] arr;
    
arr.resize(GetLocalVarInt("array_"+asName+"_index"));
    for(
int i 0arr.length(); i++)
        
arr[i] = GetLocalVarString("array_"+asName+i);
    return 
arr;
}
void SetGlobalVarStringArray(string &in asNamestring[] &in asData) {
    
int size asData.length();
    for(
int i=0;i<size;i++)
        
AddGlobalVarString("array_"+asName+iasData[i]);
    
SetGlobalVarInt("array_"+asName+"_index"size);
}
void AddGlobalVarStringArray(string &in asNamestring &in asText) {
    
SetGlobalVarString("array_"+asName+
        
GetGlobalVarInt("array_"+asName+"_index"), asText);
    
AddGlobalVarInt("array_"+asName+"_index"1);
}
string[] GetGlobalVarStringArray(string &in asName) {
    
string[] arr;
    
arr.resize(GetGlobalVarInt("array_"+asName+"_index"));
    for(
int i 0arr.length(); i++)
        
arr[i] = GetGlobalVarString("array_"+asName+i);
    return 
arr;



String processing functions
Spoiler below!
This one relies on the string array functions above, but can be re-written to include that functionality in itself.

PHP Code:
string[] StringSplit(string &in asStringstring &in asDelimiter); 

Scans asString where everytime it finds asDelimiter, it splits the contents up into a string array.

asString - The string to scan/process.
asDelimiter - The delimiter to split each index up by.

Script to include:

PHP Code:
string[] StringSplit(string &in asStringstring &in asDelimiter) {
    
int pause 0;
    
string[] flush = {};
    
SetLocalVarStringArray("_string_split"flush);
    Print(
"INFO: Splitting string '"+asString+"' with delimiter '"+asDelimiter+"'\n");
    for(
int i 0<= asString.length(); i++) {
        
string s StringSub(asStringiasDelimiter.length());
        if(
== asDelimiter || == asString.length()) {
            
string split StringSub(asStringpausei-pause);
            
AddLocalVarStringArray("_string_split"split);
            
pause asDelimiter.length();
        }
    }
    return 
GetLocalVarStringArray("_string_split");



Float Decimal Limiter
Spoiler below!
This function will limit the amount of decimals a float value has. If you give it a value of 1.23456f and limit it to 3, it will return 1.234f. It does not round to nearest, it simply ignores the excess data.

PHP Code:
float FloatTrimDecimals(float afValueint alX); 

Formats afValue to only include alX number of decimals.

afValue - The value to format
alX - The amount of decimals

Script to include:

PHP Code:
float FloatTrimDecimals(float afValueint alX) {
    
uint64 iDeci 1;
    for(
int i 0alXi++) iDeci *= 10;
    
uint64 iTemp int(afValueiDeci);
    
float result iTemp float(iDeci);
    return 
result;



I will add more later on. Probably.


RE: Useful custom script functions - HappyMatt12345 - 11-02-2018

OOO a boolean function that isn't the standard bool (name) = (value); I like this.