Creating your first script

Welcome to the world of utilitybelt lua scripting! This guide will walk you through creating your very first script, which will display "Hello World, from my first script!" in the in-game chat box when loaded. Don't worry, we'll dive into more advanced concepts in later sections.

Organizing Your Scripts

Each script resides within its own unique subdirectory within the main script directory. This structure prevents conflicts between scripts and enables you to use multiple files within a single script. The name of this subdirectory determines the name of your script.

Tip

By default, the script directory is located at C:\Users\<YOUR USERNAME>\Documents\Decal Plugins\UtilityBelt\scripts\. You can customize this directory by adjusting the settings in UtilityBelt.Service.

Writing Your First Script

Let's start by creating a new subdirectory called helloworld within your scripts directory.

Warning

Remember, script names should only use alphanumeric characters and must be unique. Each script must have a distinct name to avoid conflicts.

Inside the newly created helloworld subdirectory, you'll need to craft the core of your script. Begin by creating an empty file named index.lua. The main entry Lua file for any script is always named index.lua. The entire content of this file will be executed upon loading the script. Open this file in Visual Studio Code (or your preferred code editor) and add the following line:

print("Hello World, from my first script!")

The print() function will output a message to both the in-game chat window and the script console tab within the script manager UI.

Running your script

Let's see your creation in action! Launch the game and log in to your character. Now, access the UtilityBelt Scripts Manager window by using the UB Hud Bar. In the left column, locate your helloworld script, select it, and click the "Start" button.

Once started, you should see the text "Hello World, from my first script!" appearing in the game chat window. If you switch to the Console tab within the script manager, you'll also find the same text there. If the text doesn't show up, double-check for any error messages in the chat window and ensure you've entered the script correctly as shown above.

Tip

Scripts operate in their own isolated context, which means they don't share variables / state with other scripts. Each time you start a script a new context is created, complete with its individual Game API instance. This isolation prevents unintentional variable or state sharing among scripts.

Congratulations! You've successfully created and run your first script.

Using the command line

Alternatively, you can use the ingame chatbox to start/stop scripts, or execute a chunk of lua in the context of a script. The commands are as follows:

CommandDescription
/ubs script <start|stop> <scriptname>Start or stop a script named <scriptname>.
/ubs lexecs <scriptname> <code>Run <code> within the context of <scriptname>.

Script life cycle

Scripts launched via the in-game UI will halt when the current character logs out. To run a script at the character select screen, start it before logging into a character or add it to global autoload scripts or account autoload scripts settings. For scripts to run every time a specific character logs in, add them to the character autoload scripts setting.

Each script can run only in one instance at a time. If you list the same script in multiple autoload settings, it will be started once. Attempting to launch a script already running will stop the current instance before starting a new one.

Tip

For iterative work on scripts—such as refining UI styling—consider enabling Hot Reload from the script manager's settings tab. This feature detects changes in your script directory's files and automatically reloads your script.