[QUESTION-SOLVED] How to add random attack animations in a custom code?

Started by __SK__ on

Topic category: Help with modding (Java Edition)

Last seen on 03:20, 19. Mar 2024
Joined May 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
[QUESTION-SOLVED] How to add random attack animations in a custom code?
Sat, 10/15/2022 - 03:33 (edited)

Recently I shared a tutorial which implements Geckolib entity animations to MCreator. It added 4 basic options ... walking, idle, death, and attack. Now I am trying to add a random statement to the attack animation. But when using cases or random the animations will flicker (meaning, sort of starts but will not complete the assigned random animation). Here is the topic with the full code.

https://mcreator.net/forum/91171/tutorial-how-add-geckolib-animated-ent…

 

Partial code ... 


   private <E extends IAnimatable> PlayState movementPredicate(AnimationEvent<E> event) {

      if (event.isMoving()) {
         event.getController().setAnimation(new AnimationBuilder().addAnimation("walk", true));
         return PlayState.CONTINUE;
      }
      if (!event.isMoving()) {
         event.getController().setAnimation(new AnimationBuilder().addAnimation("idle", true));
         return PlayState.CONTINUE;
      }
      if (this.isDeadOrDying()) {
         event.getController().setAnimation(new AnimationBuilder().addAnimation("death", false));
         return PlayState.CONTINUE;
      }
      if (this.isAggressive()) {
         event.getController().setAnimation(new AnimationBuilder().addAnimation("attack", false));
         return PlayState.CONTINUE;
      }
      return PlayState.CONTINUE;
   }

Any pointers will be greatly appreciated.

Edited by __SK__ on Sat, 10/15/2022 - 03:33
Last seen on 03:20, 19. Mar 2024
Joined May 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
One of many solutions ...  …
Sat, 10/15/2022 - 03:32

One of many solutions ...

  REMOVE - isAggressive statement from the movementPredicate

  

  private <E extends IAnimatable> PlayState movementPredicate(AnimationEvent<E> event) {

      if (event.isMoving()) {
         event.getController().setAnimation(new AnimationBuilder().addAnimation("walk", true));
         return PlayState.CONTINUE;
      }
      if (!event.isMoving()) {
         event.getController().setAnimation(new AnimationBuilder().addAnimation("idle", true));
         return PlayState.CONTINUE;
      }
      if (this.isDeadOrDying()) {
         event.getController().setAnimation(new AnimationBuilder().addAnimation("death", false));
         return PlayState.CONTINUE;
      }
      //if (this.isAggressive()) {
      //   event.getController().setAnimation(new AnimationBuilder().addAnimation("attack", false));
      //   return PlayState.CONTINUE;
      //}
      return PlayState.CONTINUE;
   }

 

Create an new int 

Let's name it attackingMarker with a value of 1

private int attackingMarker = 1

 

In this attackPredicate, when swinging add a simple 1 ... 0 sequence (not random - it will be 1...0...1...0...etc, but works as intended alternating animations)

private PlayState attackPredicate(AnimationEvent event) {
if(this.swinging && event.getController().getAnimationState().equals(AnimationState.Stopped)) {
event.getController().markNeedsReload();
if (attackingMarker == 1) {
event.getController().setAnimation(new AnimationBuilder().addAnimation("attack1", false));
attackingMarker = 0;
}
else {
event.getController().setAnimation(new AnimationBuilder().addAnimation("attack2", false));
attackingMarker = 1;
}
this.swinging = false;
}
return PlayState.CONTINUE;
}

 

What happened?

Now it will start with initial value of 1 (playing attack1 animation, not looping), after animation1 is played it will change the value to 0, and finally execute in else statement (playing attack2 animation, not looping) ... in which attackingMarker ends with a value of 1. Then the next attack will start with the value of 1.

 

And you are done!

Have fun

 

===================================================================

If it is useful, feel free to add/use it for your modding and/or future plugin templates.

===================================================================

Last seen on 08:52, 19. Mar 2024
Joined Jul 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
isAgressive was useless to…
Thu, 10/20/2022 - 10:52

isAgressive was useless to begin with, unless you want to do something like a skeleton's arm lifting animation when it shoots at you, by having a looping animation.

Last seen on 03:20, 19. Mar 2024
Joined May 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Yes, it took me a while to…
Thu, 10/20/2022 - 19:01

Yes, it took me a while to see the difference (still in the learning phase). Your Geckolib plugin is great, thanks for your contribution.