Level change after a time

I’m new to UE5 and don’t understand how to make a level change with delay but to make it work once like:

delay (5 min) > open level by name (level_1) > delay (5 min) > open level by name (level_2)

Now when writing this code it is endless change on the same level_1 “delay (5 min) > open level by name (level_1)”

Are you using Blueprints or C++? If you’re using Blueprints then you could load each next level in a Blueprint that’s specific to each level. Have a variable that keeps track of the amount of time that the level has been active, and when the time is greater than 5 minutes, load the next level.

So for example in a blueprint in a level that runs when the level starts, like the Gamemode blueprint, you could do something like:

I created the variable LevelTime in the variable’s tab on the left of the Blueprint editor, it’s not a default value:

If you’re working in c++ you can do a similar thing but in a c++ class that runs when the level starts. For creating a variable in c++, you write code like this in the header (.h) file of a given Actor:

public:	
	// Sets default values for this actors properties

	AMyActor();

	UPROPERTY()
		float levelTime;

(Note that I only wrote in the UPROPERTY() and float levelTime; lines. These lines create a UPROPERTY for the actor that essentially behaves like an instance specific variable for the actor.)

Then in the c++ file (.cpp file), you would write in the same logic that I showed in the Blueprint version. First we need to include:

#include "Kismet/GameplayStatics.h"

Then we initialize the time keeper variable. We can actually initialize levelTime before the level start in the constructor (in the BP version we initialized it in BeginPlay()):

// Sets default values
AMyActor::AMyActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you dont need it.
	PrimaryActorTick.bCanEverTick = true;

	levelTime = 0.0f;
}

Then in the Tick() method:

// Called every frame
void AMyActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	levelTime += DeltaTime;

	if (levelTime / 60.0f >= 5.0f) {
	
        UGameplayStatics::OpenLevel(GetWorld(), "Level2");
	}

}

This code produces this (I shortened the time delay to 5 seconds). Level 1 has a chair in it and level 2 has a door frame:

ezgif-4-2bce2993eb

I hope this helps!

2 Likes

You were very helpful, thank you very much, I almost gave up :heart:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.