How to create an Animated Entity

This page will not cover how to code an Entity from the ground up, only how to add the required Azurelib code for the animations and model to work.

Needed Steps in Entity class

To start, you must implement GeoEntity

Override getAnimatableInstanceCache and registerControllers

Instantiate a new AnimatableInstanceCache via AzureLibUtil.createInstanceCache(this) at the top of your entity class and return it in getAnimatableInstanceCache

private final AnimatableInstanceCache cache = AzureLibUtil.createInstanceCache(this);

@Override
public AnimatableInstanceCache getAnimatableInstanceCache() {
    return cache;
}

Add any controllers you want for animations in registerControllers

@Override
public void registerControllers(ControllerRegistrar controllers) {
	controllers.add(new AnimationController<>(this, "controllerName", 0, event ->
	{
		return event.setAndContinue(
			// If moving, play the walking animation
			event.isMoving() ? RawAnimation.begin().thenLoop("walking"): 
			// If not moving, play the idle animation
			RawAnimation.begin().thenLoop("idle"));
	})
	// Sets a Sound KeyFrame
	.setSoundKeyframeHandler(event -> {
		//Plays the step sound on the walk keyframes in an animation
		if (event.getKeyframeData().getSound().matches("walk"))
			if (level().isClientSide())
				level().playLocalSound(
					this.getX(), this.getY(), this.getZ(), 
					DoomSounds.PINKY_STEP, 
					SoundSource.HOSTILE, 0.25F, 1.0F, false);
	}));
}

Creating the model class

Create a new class that extends GeoModel, then implement all the required methods:

public class ExampleEntityModel extends GeoModel<ExampleEntity> {
// Models must be stored in assets/<modid>/geo with subfolders supported inside the geo folder
	private static final ResourceLocation model = new ResourceLocation("yournamespace", "geo/yourmodel.geo.json");
// Textures must be stored in assets/<modid>/geo with subfolders supported inside the textures folder
	private static final ResourceLocation texture = new ResourceLocation("yournamespace", "textures/<modeltype>/yourtexture.png");
// Animations must be stored in assets/<modid>/animations with subfolders supported inside the animations folder
	private static final ResourceLocation animation = new ResourceLocation("yournamespace", "animations/youranimation.animation.json");

	@Override
	public ResourceLocation getModelResource(ExampleEntity object) {
		return this.model;
	}

	@Override
	public ResourceLocation getTextureResource(ExampleEntity object) {
		return this.texture;
	}

	@Override
	public ResourceLocation getAnimationResource(ExampleEntity object) {
		return this.animation;
	}
}

Creating the renderer class

Create a class that extends GeoEntityRenderer, then just set it like so:

public class ExampleEntityRenderer extends GeoEntityRenderer<ExampleEntity> {
    public ExampleEntityRenderer(EntityRendererProvider.Context context) {
        super(context, new ExampleEntityModel());
    }
}

You will then need to register this class as you would any other renderer, depending on your modloader methods.

Last updated