Psych Engine Logo
T-Bar Engine - Documentation

Importing Custom Libraries

The fork allows for importing scripts with custom made functions in it using the `import` keyword in HScript. This is useful for making your own utility script and wanting to use it's functions in multiple other scripts.

To make a library script, find the folder called "libraries" in your modpack (If it doesn't exist, then create one!) and make a new hscript inside of it.

Creating a Custom Library

Inside of it, lets make a class with a function that returns the square root of the number given.

Creating a Custom Library 2

Let's break down the important parts of this script:

  • Firstly, we made a new class with the same name as our script.
  • Then, we added a static function into our class that returns the square root of "num".
  • Finally, we added a callback at the bottom called "onScriptImported" that adds the class as a variable to whatever script imports this one.
  • Now to use the library in another script, we have two options:

  • Using the `import` keyword.
  • Using the `@:include` metadata.
  • Both can work in this case since both of these methods call the `onScriptImported` callback. This is also why you can't just use the `importScript` function, as it doesn't call the callback.

    Also please note that the `import` keyword method automatically starts in the "libraries" folder, while the `@:include` metadata method will start in the modpack itself, so you will need to append the "libraries/" part before your actual script path. This is the same case with the ".hx" extension, as the metadata will check for any text file, while the `import` keyword will append the extension automatically.

    Adding our Custom Library

    Testing our Custom Library

    You can also do this with not just functions! You can do this with variables, custom classes, enums, etc.