Chat & Commands
Last updated
Last updated
Oftentimes, games send chat messages to all players or to a specific set of players to relay important information. Additionally, chat commands are common in games as well, triggering specific behaviors or altering settings when players or a game admin enters some command in chat.
The HYTOPIA SDK allows you to fully control programmatically sending chat messages as well as defining your own chat commands with callbacks.
All chat and chat command functionality is accessed through an instance of the ChatManager
class. This singleton is automatically created for each world instance and is accessible via world.chatManager
.
There's 2 different ways to send chat messages. The first is broadcasting, which will be received by all players, and the second is sending messages only to a specific player.
Here's a quick example.
Registering chat commands, often preceded with /
(but not required), is a great way to add quick functionality to your game, create your own admin level commands like /ban
, and more.
You can register a new chat command with world.chatManager.registerCommand()
Here's a quick ping/pong example of chat commands.
It's important to note that only one callback can be registered to the same command at a time. If you invoke world.chatManager.registerCommand()
with a previously registered command, the previously set callback will be discarded and the callback provided to the most recent invocation of world.chatManager.registerCommand()
will be used.
Lastly, you can unregister commands altogether with world.chatManager.unregisterCommand()
The ChatManager class is constantly evolving. You can find the latest ChatManager API Reference here.
If there are features that we don't currently support for the ChatManager that you'd like to see added to the HYTOPIA SDK, you can submit a feature request here.