Announcement

Collapse
No announcement yet.

Events and Calling Functions from other classes

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Events and Calling Functions from other classes

    Hi, I am trying to make an event triggered in C++ so that you can handle states in the animation blueprint (AnimInstance).
    I am trying to make it work for a AI bot, lets say we want to trigger Attack state,
    so first I created a new class extending from "AIController", this class determines the behavior of my AI Bot.
    Next I created a class extending from "AnimInstance":
    Code:
    #include "Animation/AnimInstance.h"
    #include "JurijAnimationInstance.generated.h"
    
    UCLASS(transient, Blueprintable, hideCategories = AnimInstance, BlueprintType)
    class UJurijAnimationInstance : public UAnimInstance
    {
    	GENERATED_UCLASS_BODY()
    
    public:
    	UFUNCTION(BlueprintImplementableEvent, meta = (FriendlyName = "PlayOrStopAttackAnimation"))
    		virtual void PlayAttackAnimation(bool IsAttacking);	
    };
    This creates an Event you can add in your blueprint (extending from this class), so that you can trigger from code when the animation states will change.

    My problem is that I need to trigger this function from my AIController class, and I don't know how to make a pointer to it.
    I have been trying and I know it shouldn't be that hard , I hate to admit but I seem to be missing something, code compiles but my event never gets triggered from AIController.
    This is a failed attempt, first the header:
    Code:
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement)
    		TSubobjectPtr<class UJurijAnimationInstance> TriggerTheEvent;
    Then I initialize it in .cpp:
    Code:
    TriggerTheEvent = PCIP.CreateDefaultSubobject<UJurijAnimationInstance>(this, TEXT("AnimInstanceSender"));
    and call it like this :
    Code:
    TriggerTheEvent->PlayAttackAnimation(true);
    I have also tried using * to point to class , but in the debug it said function is not allowed to call.
    I am somewhat new to pointers so thanks a bunch to anyone who replies.
    ------------------------------------------------------------
    If I extend on this question, lets say we want to make a selectable AI Bot, so while having all the behavior of an AI Bot,
    we want to be able to control the bot using our own controller. Like for example in strategy games. How would I tackle this?
    Should I include the AIController class in MyController class ? and make pointers to be able to call its functions?
    I believe it is a same problem. Thanks!!!
    Last edited by Jurif; 05-08-2014, 02:37 PM.

    #2
    Why does the subobject thing not work? Errors? Or isn't it even triggered?

    To your strategy game question:
    With the PlayerController you receive input and can determine which AI Units get which orders.
    Then you give the corresponding AIController input (functions or states) and it will react based on your implementation of the corresponding subclass of AIController.
    It can be useful to create some kind of command system. PM me, I will be forced to do that soon.

    Comment


      #3
      Actually the pointers are working fine,I created a new function in the JurijAnimationInstance class:
      Code:
      UFUNCTION(BlueprintCallable, Category = Attack)
      	    virtual void TriggerTheEvent(bool TrueOrNot);
      so when this function gets triggered from AIController , it calls the Event like this:
      Code:
      void UJurijAnimationInstance::TriggerTheEvent(bool TrueOrNot)
      {
      	this->PlayAttackAnimation(TrueOrNot);
      }
      so I figured out the part of calling functions from other classes,
      but obviously still my problem is calling the event, the code compiles fine,
      but in the editor-game the event doesn't happen.
      Anyway, I had a busy day, maybe tomorrow I'll find enough time to figure this one out.

      Comment


        #4
        I don't think you want to be creating your own AnimInstance instance, that should be handled by the SkeletalMeshComponent, you tell it what class of AnimInstance you want to use and it instantiates it for you and uses it to animate the mesh. Normally AnimInstance's are set up to 'pull' data from their owning Actor. Maybe you could use a Montage to play the attacking animation?
        Lead Programmer - UE4 Animation/Physics/Audio Team - Epic Games
        Twitter: @EpicJamesG

        Comment


          #5
          Hi James, yeah true, I could use AnimMontage or maybe just PlayAnimSlot, I am still learning about everything.
          Actually the code above is all correct, except the part of creating a DefaultSubObject is not needed ,
          the code works fine, I created a AnimInstance class and added an event and a function to it.
          So my problem the whole time was referencing the correct AnimInstance in game, I tested this by making a for loop Iterating through AnimInstance objects and calling the Event.
          Of course this is not good cause then all AnimInstances in the game trigger the event and animation is played on all.
          I need to learn more about referencing and making pointers. I tried to do it this way cause I felt for a strategy game would be a good idea to use Animation States, since there would be many pawns\characters in game.

          Thanks a lot for the tip, I will learn about AnimMontage .

          For everyone interested there are new tutorials for AnimMontage:
          https://docs.unrealengine.com/latest...age/index.html
          http://www.youtube.com/watch?v=zQrNQtfNOHc
          Last edited by Jurif; 05-08-2014, 03:05 PM.

          Comment

          Working...
          X