Skip to content

Using Actor Events

hiperbou edited this page Mar 19, 2021 · 7 revisions

This tutorial assumes you have completed the Hello World Tutorial.

In this tutorial we will learn how to call Kotlin code that will be executed on the Tick and ActorBeginOverlap events.

First create a new Actor Blueprint by right clicking on the content browser and selecting Blueprint Class and selecting **Actor ** as base class. The new Blueprint is created so let's name it something like "CubeTutorialBlueprint". Open the newly created Blueprint by double clicking it.

Then let's add a Static Mesh and the KotlinComponent to our Blueprint. Click on the Add Component button and select "Cube", then drag and drop the "Cube" component over the "DefaultSceneRoot" to override it. Click on the Add Component and search for "kotlin" and add the Kotlin component.

Now, select the "Kotlin" component and in the details panel, look for Kotlin Class and set it to "CubeTutorial". That will be our Kotlin class that will contain our code.

To make things even more interesting, add a Box Collision component by clicking on Add Component and select Box Collision, and now scale it to 2,5 in every axis.

The result should look similar to this.

We will use the Blueprint events in order to call our Kotlin class functions. Go to the Event Graph tab and select the "Kotlin" component. Then rigth click in an empty space on the Event Graph and select Call Function on Kotlin/Kotlin/Begin Overlap Repeat the process with Call Function on Kotlin/Kotlin/On Tick

Tidy up and connect everything like this:

Save and compile the Blueprint.

Now we need to add the actual CubeTutorial class in our Kotlin project, so let's go to IntelliJ and add a class named CubeTutorial on the src folder and copy and paste this code.

import ue.*

class CubeTutorial: KotlinObject() {
    var yaw = 0.0

    override fun Tick(deltaTime:Float) {
        yaw += 100 * deltaTime
        GetOwner<Actor>().SetActorRotation(Rotator(Yaw  = yaw), false)
    }

    override fun BeginOverlap(other: Actor):String {
        println("Collided with $other!")
        return ""
    }
}

Click on the "Play" button to trigger the Build configuration to compile our code and make it available to our Unreal project.

Back on the Unreal editor, drag and drop our CubeTutorialBlueprint into the level, and enter into play mode. You should see the cube rotating and a message should appear on the Javascript Console when you get close to it with the camera.

Exit play mode, and add some more CubeTutorialBlueprint to the level, and you will have your own army of rotating cubes.

Amazing.

Next: Using Blueprint Properties

Clone this wiki locally