|
| 1 | +--- |
| 2 | +title: "Spawning Entities" |
| 3 | +description: "Learn how to spawn Entities in Hytale" |
| 4 | +authors: |
| 5 | + - name: "Neil Revin" |
| 6 | + link: "https://itsneil.dev" |
| 7 | +--- |
| 8 | + |
| 9 | +In this guide, you will learn how to spawn entities in Hytale. It is **highly recommended** to familiarize yourself with the Entity Component System (ECS) before proceeding with this guide. You can find the ECS guide [here](./ecs). |
| 10 | + |
| 11 | + |
| 12 | +## Getting the World Object |
| 13 | + |
| 14 | +You need to first get the `World` object before proceeding with spawning entities. You can get this object in a few ways: |
| 15 | + |
| 16 | +### Using the `Player` Object |
| 17 | + |
| 18 | +You can use the `Player` object to get the `World` object by calling the `getWorld()` method on the `Player` instance. |
| 19 | + |
| 20 | +```java |
| 21 | +World world = player.getWorld(); |
| 22 | +``` |
| 23 | + |
| 24 | +### Using the `Universe` Class |
| 25 | + |
| 26 | +You can use the `Universe` class to get the `World` object by calling the `getWorld(string UUID)` method on the `Universe` instance. |
| 27 | + |
| 28 | +```java |
| 29 | +World world = Universe.get().getWorld("your-world-uuid"); |
| 30 | +``` |
| 31 | + |
| 32 | +Once you have the world object, you need to get a instance of `Store<EntityStore>`, you can do so by: |
| 33 | + |
| 34 | +```java |
| 35 | +Store<EntityStore> store = world.getEntityStore().getStore(); |
| 36 | +``` |
| 37 | + |
| 38 | +## Creating Entities |
| 39 | + |
| 40 | +Once you have the `EntityStore` instance, you can start to create a entity and give it components. Entities are just instances of `Holder<EntityStore>` with several components attached to them. |
| 41 | + |
| 42 | +<Callout type="warning"> |
| 43 | + To use the `store` variable we got above, we need to run this inside the world. You can do so by using the `world.execute()` method: |
| 44 | + |
| 45 | + ```java |
| 46 | + world.execute(() -> { |
| 47 | + // run all the code below in this context |
| 48 | + }) |
| 49 | + ``` |
| 50 | +</Callout> |
| 51 | + |
| 52 | +Let's start by creating a blank holder: |
| 53 | + |
| 54 | +```java |
| 55 | +Holder<EntityStore> holder = EntityStore.REGISTRY.newHolder(); |
| 56 | +``` |
| 57 | +## Getting a Model |
| 58 | + |
| 59 | +You now need a model for your Entity. You can find a list of all entities [here](../../server/entities), we're going to use the Minecart entity for this example: |
| 60 | + |
| 61 | +```java |
| 62 | +ModelAsset modelAsset = ModelAsset.getAssetMap().getAsset("Minecart"); |
| 63 | +Model model = Model.createScaledModel(modelAsset, 1.0f); |
| 64 | +``` |
| 65 | + |
| 66 | +### The Transform Component |
| 67 | + |
| 68 | +The `TransformComponent` is the component that tells Hytale where to place your entity, what it's location is. You can get this in multiple ways. |
| 69 | + |
| 70 | +1. Getting the Transform Component from the `Player` object |
| 71 | + |
| 72 | +```java |
| 73 | +TransformComponent transform = store.getComponent(context.senderAsPlayerRef(), EntityModule.get().getTransformComponentType()); |
| 74 | +``` |
| 75 | + |
| 76 | +2. Generating a new TransformComponent |
| 77 | + |
| 78 | +```java |
| 79 | +Vector3d vector3d = new Vector3d(0, 0, 0); // position |
| 80 | +Vector3f vector3f = new Vector3f(0, 0, 0); // rotation |
| 81 | +TransformComponent transform = new TransformComponent(vector3d, vector3f); |
| 82 | +``` |
| 83 | + |
| 84 | +## Adding Components to our Entity |
| 85 | + |
| 86 | +Now we need to add all the components we made above to our entity, here's all the components we need to add one by one: |
| 87 | + |
| 88 | +```java |
| 89 | + |
| 90 | +holder.addComponent(TransformComponent.getComponentType(), new TransformComponent(vector3d, new Vector3f(0, 0, 0))); |
| 91 | +holder.addComponent(PersistentModel.getComponentType(), new PersistentModel(model.toReference())); |
| 92 | +holder.addComponent(ModelComponent.getComponentType(), new ModelComponent(model)); |
| 93 | +holder.addComponent(BoundingBox.getComponentType(), new BoundingBox(model.getBoundingBox())); |
| 94 | +holder.addComponent(NetworkId.getComponentType(), new NetworkId(store.getExternalData().takeNextNetworkId())); |
| 95 | +holder.addComponent(Interactions.getComponentType(), new Interactions()); // you need to add interactions here if you want your entity to be interactable |
| 96 | +``` |
| 97 | + |
| 98 | +### Ensuring Hytale's Components are present |
| 99 | + |
| 100 | +These act as "configuration" or "default" components that Hytale expects to be present on all entities. |
| 101 | + |
| 102 | +```java |
| 103 | +holder.ensureComponent(UUIDComponent.getComponentType()); |
| 104 | +holder.ensureComponent(Interactable.getComponentType()); // if you want your entity to be interactable |
| 105 | +``` |
| 106 | + |
| 107 | +## Spawning the Entity |
| 108 | + |
| 109 | +Finally, we can spawn the entity in the world by calling the `spawnEntity` method on the `EntityStore` instance: |
| 110 | + |
| 111 | +```java |
| 112 | +store.spawnEntity(holder, AddReason.SPAWN); |
| 113 | +``` |
0 commit comments