An introduction to building NPC behaviors

In today’s article, we’re going to give you an overview of how Hytale’s behavior scripting system provides ways to bring NPCs and creatures to life. To do this we’re going to show you some aspects of Trork behavior - how they react to the player, the environment, and each other. Of course, these principles apply to Hytale’s other creatures too - and all of them will be customizable by modders. As ever, all of the screenshots and clips in this post represent work in progress, and details may well change over the course of future development.

After a creature has been designed and its model and animations have been created in Hytale Model Maker, the NPC developers are tasked with bringing it to life using the behavior scripting system.

The system uses JSON scripts that call on modular behavior elements that allow us to specify how an NPC perceives the world and how it reacts in a variety of situations. These scripts can trigger animations, changes in state, and more - a very simple example of this would be a script that determines whether a creature that has been attacked flees, or attempts to defend itself. More sophisticated scripts can send NPCs chasing after players or cause them to seek out things they like, such as campfires or food.

“We use building blocks to create an NPC from the ground up” says developer Ioseff Griffith. “We have sensors that are how an NPC interprets the world around it, and that’s how it decides what it wants to do. Then we have actions and motions, which are how they will then interact with the world or their target or whatever it is that they’re focusing on. We combine these to build up components and templates from which variants can quickly and easily be derived.”

In this example, you can see a snippet of the script that causes Trorks to spring into action when they see the player. The script handles several elements of this interaction. It triggers an ‘alerted’ animation, which helps the player understand that they’ve been spotted. It also initiates a ‘beacon’ response that causes other Trorks within a certain radius to be alerted to the player’s presence too. The exact details of this interaction can be precisely tweaked, but the overall implementation is grounded in top-level templates that are common to most NPCs.

This modular approach to behavior makes it easier to implement entirely new NPCs or creatures. “When you script an NPC you don’t need to specify things like ‘swing a sword’ or ‘move X distance to the right’” Ioseff explains. Simply applying an abstract component like ‘flee’ or ‘seek’ is enough - the behavior system then handles the basics of carrying out the desired action.

“At the most basic level, the easiest way to create an NPC is to use existing templates” he says. “These templates are made up of components that we build out of sensors, actions, and motions to make it easy to reuse behaviors across multiple scripts.” One example of a component is sound detection, Ioseff explains. This uses a combination of sensors that allow an NPC to interpret its environment. “When you drop a new NPC into the world it should be able to cope with environments that it hasn’t been specifically scripted for, at least to some degree” he says.

“An important feature of this whole system is high reusability” says developer Eric Reinhart. “It’s easy to modify existing behaviors, replace parts, and it’s also easy to build up something new.”

“For someone who wants to mod things or create content, the advantage is that it’s easy to make simple modifications” Eric continues. “But it also allows modders to change everything down to the smallest detail.”

Here’s an example. By adding a few extra parameters to the Trork hunter’s behavior script, we can add bears to the list of creatures that cause them to enter an ‘alert’ state.

Now we’ve got a Trork hunter who yells at bears! This is purely an example - they won’t necessarily do this in the final game. Even so, this provides an example of how straightforward it is to introduce new details to a creature’s behavior.

Often the behavior set for a new NPC will be mapped out by the design team before it comes to be implemented. Sometimes, this means adding new features to the behavior system that then expand the possibilities of the system further. Occasionally, however, surprises during development will lead to the addition of new behavioral quirks.

“Some small pieces get added during the process” Ioseff explains. “For example, Trorks have a sparring behavior where they start punching each other. When I was implementing this, a defeated Trork would change state. Sometimes they’d immediately switch to a ‘nap’ state - this was funny, because it looked like they’d been knocked out. I implemented it properly, and currently sparring Trorks will knock each other out after a fight.”

The team’s intent is to create creatures and NPCs that contribute to the immersiveness of the world by acting independently of the player. By learning how each creature reacts, players can then come up with creative ways to interact with them. For example, Trorks have a particular fondness for discarded meat:

How the player chooses to take advantage of this information is completely up to them!

The team has found that combining lots of simple behaviors can produce a surprisingly lifelike result. “I’d created a script for a Trork archer, and was trying to test how well the archer fires” Eric says. “This was the first time when we had the possibility for NPCs to switch weapon. It was amazing to try to pursue this archer, and then when you were close enough he would turn around and attack you with a melee weapon. Having these really simple actions combined together was really rewarding.”

In the clip above, you can see an example of several simple Trork behaviors working together. The Trork is napping outside when it begins to rain. This triggers a change of state from ‘asleep’ to ‘awake’, at which point the Trork decides to seek out somewhere warm. He then moves to the nearby campfire and sits down. Several different elements of the Trork’s behavior script are involved in creating this moment, but taken together they create a sense of the creature’s personality. Our goal with the behavior system is to furnish the player with immersive moments like this, and we’re excited to see what modders can come up with when they get their hands on it!

Here are a few more examples of NPC behaviors that you may encounter as you explore Orbis:

Kweebecs don’t need to eat or drink in a normal sense. As plant-people, they’ll seek out bright spots and happily sunbathe to get all the nutrients they need!

Goblins are fond of throwing bombs at a problem. These can destroy blocks and create holes, which is a problem in and of itself. Good job, goblins.