Creating functions that can be called in Blueprint

While marking classes as BlueprintType or Blueprintable allows us to pass instances of the class around in Blueprint, or to subclass the type with a Blueprint class, those specifiers don't actually say anything about member functions or variables, and if they should be exposed to Blueprint.

This recipe shows you how to mark a function so that it can be called within Blueprint graphs.

How to do it…

  1. Create a new Actor class using the editor. Call the actor SlidingDoor.
  2. Add the following UPROPERTY to the new class:
    UFUNCTION(BlueprintCallable, Category = Door)
    void Open();
    UPROPERTY()
    bool IsOpen;
    
    UPROPERTY()
    FVector TargetLocation;
  3. Create the class implementation by adding the following to the .cpp file:
    ASlidingDoor::ASlidingDoor()
    :Super()
    {
      auto MeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/BasicShapes/Cube.Cube'"));
      if (MeshAsset.Object != nullptr)
      {
        GetStaticMeshComponent()->SetStaticMesh(MeshAsset.Object);
        GetStaticMeshComponent()->bGenerateOverlapEvents = true;
      }
      GetStaticMeshComponent()->SetMobility(EComponentMobility::Movable);
      GetStaticMeshComponent()->SetWorldScale3D(FVector(0.3, 2, 3));
      SetActorEnableCollision(true);
      IsOpen = false;
      PrimaryActorTick.bStartWithTickEnabled = true;
      PrimaryActorTick.bCanEverTick = true;
    }
    void ASlidingDoor::Open()
    {
      TargetLocation = ActorToWorld().TransformPositionNoScale(FVector(0, 0, 200));
      IsOpen = true;
    }
    
    void ASlidingDoor::Tick(float DeltaSeconds)
    {
      if (IsOpen)
      {
        SetActorLocation(FMath::Lerp(GetActorLocation(), TargetLocation, 0.05));
      }
    }
  4. Compile your code and launch the editor.
  5. Drag a copy of your door out into the level.
  6. Make sure you have your SlidingDoor instance selected, then open the Level blueprint. Right-click on the empty canvas, and expand Call function on Sliding Door 1.
    How to do it…
  7. Expand the Door section, then select the Open function.
    How to do it…
  8. Link the execution pin (white arrow) from BeginPlay to the white arrow on the Open node, as seen in the following screenshot:
    How to do it…
  9. Play your level, and verify that the door moves up as expected when Open is invoked on your door instance.
    How to do it…

How it works…

  1. Within the declaration of the door, we create a new function for opening the door, a Boolean to track if the door has been told to open, and a vector allowing us to precompute the target location of the door.
  2. We also override the Tick actor function so that we can perform some behavior on every frame.
  3. Within the constructor, we load in the cube mesh and scale it to represent our door.
  4. We also set IsOpen to a known good value of false and enable actor ticking by using bCanEverTick and bStartWithTickEnabled.
  5. These two Booleans control if ticking can be enabled for this actor and if ticking starts in an enabled state respectively.
  6. Inside the Open function, we calculate the target location relative to the door's starting position.
  7. We also change the IsOpen Boolean from false to true.
  8. Now that the IsOpen Boolean is true, inside the Tick function, the door tries to move itself towards the target location using SetActorLocation and Lerp to interpolate between the current location and the destination.

See also

  • Chapter 5, Handling Events and Delegates, has a number of recipes relating to the spawning of actors
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.151.21