Skip to content

Hot Reload

hiperbou edited this page Mar 19, 2021 · 9 revisions

This tutorial assumes you have completed the Using Blueprint Properties.

In this tutorial we will learn some methods of applying code changes while being on play mode.

In game programming, having low iteration times is very convenient, although changes in the code can be applied by stopping the game and entering into play mode again, we will provide a way to reload code for a specific actor withoutd leaving play mode.

If you finished the Using Blueprint Properties, you should have a level with some rotating cubes on them. Select one of them and Open the Level Blueprint

Right click to add a new node and select Create a reference to CubeTutorialBlueprint

Again, right click to add a new node and search "key r" to add a node that will be triggered when the key R is pressed on the keyboard.

Right click and search for "hot reload" and a function "Hot Reload" should appear.

Now connect all nodes to get something similar to this.

That would make that every time we press the key "R" the object will reload its code while we are playing.

So let's enter into Play mode, and make sure we have a nice view of our cubes so we can see the changes easily. Press Shift+F1 to release your mouse from the game window, and open the CubeTutorial class in IntelliJ IDEA.

We will change the rotation code and the message printed when the collision happens.

import ue.*
import kotlin.math.cos

external class CubeTutorialActor: Actor {
    var Yaw:Float
    var Speed:Float
}

class CubeTutorial: KotlinObject() {
    var accumulatedDeltaTime = 0.0f

    override fun Tick(deltaTime:Float) {
        val actor = GetOwner<CubeTutorialActor>()
        accumulatedDeltaTime += deltaTime
        actor.Yaw = 45.0f * cos(accumulatedDeltaTime)
        actor.SetActorRotation(Rotator(Yaw  = actor.Yaw), false)
    }

    override fun BeginOverlap(other: Actor):String {
        println("I've been changed! Collided with $other!")
        return ""
    }
}

Compile using the "Play" button, and get back to the Unreal editor. Click on the game view and press the "R" key on your keyboard.

You should see the changes applied to the actor we selected on the Level Blueprint, while the others will continue using the old code.

Try changing the 45.0f constant to other values, and see how your new changes are applied.

Sometimes it may be desirable to reload all instances of a class, in that case you can use the node Hot Reload by Class, and select the CubeTutorialBlueprint.

It also exists Hot Reload All that will reload the Kotlin classes for every actor on the level.

Depending on your needs, you may want to use one or the other, or maybe trigger the reload differently. That's up to you.

Clone this wiki locally