Skip to content

Commit c5793db

Browse files
committed
feat: add 'Playing Sounds' guide
Signed-off-by: ItsNeil17 <neil@willofsteel.me>
1 parent efd0e35 commit c5793db

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
title: "Playing Sounds"
3+
description: "Learn how to Play Sounds with your Hytale Plugin"
4+
authors:
5+
- name: "Neil Revin"
6+
link: "https://itsneil.dev"
7+
---
8+
9+
In this guide, you will learn how to play sounds to a player.
10+
11+
## Sound Indexes
12+
13+
We currently have a lot of Sounds available to be used as a key in the AssetMap, [click here](../../server/sounds) to see the full list of available sounds.
14+
15+
You can use the `getAssetMap()` method from the `SoundEvent` class to get the index of a specified sound:
16+
17+
```java
18+
int index = SoundEvent.getAssetMap().getIndex("SFX_Cactus_Large_Hit");
19+
```
20+
21+
## The Transform Component
22+
23+
The `TransformComponent` is the component that tells Hytale where to play the sound. You can get this in multiple ways.
24+
25+
1. Getting the Transform Component from the `Player` object
26+
27+
```java
28+
TransformComponent transform = store.getComponent(context.senderAsPlayerRef(), EntityModule.get().getTransformComponentType());
29+
```
30+
31+
2. Generating a new TransformComponent (although this is not recommended, as if your player is not close enough they won't hear the sound)
32+
33+
```java
34+
Vector3d vector3d = new Vector3d(0, 0, 0); // position
35+
Vector3f vector3f = new Vector3f(0, 0, 0); // rotation
36+
TransformComponent transform = new TransformComponent(vector3d, vector3f);
37+
```
38+
39+
### PlayerRef
40+
41+
You need a Player Reference, which is a instance of `Ref<EntityStore>`. If you have the `Player` object, you can use the `getReference()` method to get a Player Reference
42+
43+
### World
44+
45+
You need a World reference, as the sound needs to be played inside the `execute()` method from the `World` class.
46+
47+
### SoundCategory
48+
49+
There is a `SoundCategory` enum that defines different sound categories, such as `Music`, `Ambient`, `SFX` and `UI`. You can use these categories to specify the type of sound you are playing.
50+
51+
## Playing Sounds
52+
53+
You can use the `SoundUtil` class to play sounds, using the `playSoundEvent3dToPlayer` method.
54+
55+
```java
56+
int index = SoundEvent.getAssetMap().getIndex("SFX_Cactus_Large_Hit");
57+
World world = player.getWorld();
58+
EntityStore store = world.getEntityStore();
59+
Ref<EntityStore> playerRef = player.getReference();
60+
world.execute(() -> {
61+
TransformComponent transform = store.getStore().getComponent(playerRef, EntityModule.get().getTransformComponentType());
62+
SoundUtil.playSoundEvent3dToPlayer(playerRef, index, SoundCategory.UI, transform.getPosition(), store.getStore());
63+
});
64+
```

0 commit comments

Comments
 (0)