Why is my SetTimer function doesn't work

Hi everybody. Can Someone tell me why my SetTimer function Doesn’t Work.

When i tried to LiveCodding i get this Error:

Accepted Live coding shortcut
---------- Creating patch ----------
Running C:\Program Files\Epic Games\UE_5.0\Engine\Binaries\DotNET\UnrealBuildTool\UnrealBuildTool.exe -Target="Udemy4Editor Win64 Development -Project=""C:/Users/kaana/OneDrive/Belgeler/Unreal Projects/Udemy4/Udemy4.uproject""" -LiveCoding -LiveCodingModules="C:/Program Files/Epic Games/UE_5.0/Engine/Intermediate/LiveCodingModules.txt" -LiveCodingManifest="C:/Program Files/Epic Games/UE_5.0/Engine/Intermediate/LiveCoding.json" -WaitMutex -LiveCodingLimit=100
  Log file: C:\Users\kaana\AppData\Local\UnrealBuildTool\Log.txt
  Invalidating makefile for Udemy4Editor (working set of source files changed)
  Parsing headers for Udemy4Editor
    Running UnrealHeaderTool "C:\Users\kaana\OneDrive\Belgeler\Unreal Projects\Udemy4\Udemy4.uproject" "C:\Users\kaana\OneDrive\Belgeler\Unreal Projects\Udemy4\Intermediate\Build\Win64\Udemy4Editor\Development\Udemy4Editor.uhtmanifest" -LogCmds="loginit warning, logexit warning, logdatabase error" -Unattended -WarningsAsErrors -abslog="C:\Users\kaana\AppData\Local\UnrealBuildTool\Log_UHT.txt" -installed
  Reflection code generated for Udemy4Editor in 8,9931808 seconds
  Building Udemy4Editor...
  Using Visual Studio 2022 14.33.31630 toolchain (C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629) and Windows 10.0.19041.0 SDK (C:\Program Files (x86)\Windows Kits\10).
  [Adaptive Build] Excluded from Udemy4 unity file: BaseBombLauncher.cpp, BaseGun.cpp, Bomb.cpp, BTService_PlayerLocation.cpp, BTService_PlayerLocationIfSeen.cpp, BTTaskNode_Shoot.cpp, BTTask_ClearBlackboardValue.cpp, KillEmAllGameMode.cpp, ShooterAIController.cpp, ShooterCharacter.cpp, ShooterPlayerController.cpp, Udemy4.cpp, Udemy4GameModeBase.cpp
  Determining max actions to execute in parallel (4 physical cores, 8 logical cores)
    Executing up to 4 processes, one per physical core
    Requested 1,5 GB free memory per action, 5,79 GB available: limiting max parallel actions to 3
  Building 3 actions with 3 processes...
  [1/3] Compile BaseBombLauncher.cpp
  C:\Users\kaana\OneDrive\Belgeler\Unreal Projects\Udemy4\Source\Udemy4\BaseBombLauncher.cpp(39): error C2665: 'FTimerManager::SetTimer': 4 a??r? y?klemeden hi?biri ba??ms?z de?i?ken t?rlerinden t?m?n? d?n??t?remedi
  C:\Program Files\Epic Games\UE_5.0\Engine\Source\Runtime\Engine\Public\TimerManager.h(217): note: 'void FTimerManager::SetTimer(FTimerHandle &,TFunction<void (void)> &&,float,bool,float)' olabilirdi
  C:\Program Files\Epic Games\UE_5.0\Engine\Source\Runtime\Engine\Public\TimerManager.h(207): note: veya       'void FTimerManager::SetTimer(FTimerHandle &,const FTimerDynamicDelegate &,float,bool,float)'
  C:\Program Files\Epic Games\UE_5.0\Engine\Source\Runtime\Engine\Public\TimerManager.h(202): note: veya       'void FTimerManager::SetTimer(FTimerHandle &,const FTimerDelegate &,float,bool,float)'
  C:\Users\kaana\OneDrive\Belgeler\Unreal Projects\Udemy4\Source\Udemy4\BaseBombLauncher.cpp(39): note: 'void FTimerManager::SetTimer(FTimerHandle &,const FTimerDelegate &,float,bool,float)' : 1 ba??ms?z de?i?keni 'float' de?erinden 'FTimerHandle &' de?erine d?n??t?r?lemez
  C:\Program Files\Epic Games\UE_5.0\Engine\Source\Runtime\Engine\Public\TimerManager.h(202): note: 'FTimerManager::SetTimer' bildirimine bak?n
  C:\Users\kaana\OneDrive\Belgeler\Unreal Projects\Udemy4\Source\Udemy4\BaseBombLauncher.cpp(39): note: '(float, ABaseBombLauncher *, void (__cdecl ABomb::* )(void), float, bool)' ba??ms?z de?i?ken listesi e?lenmeye ?al???l?rken
  [2/3] Compile BaseBombLauncher.gen.cpp
  [3/3] Compile ShooterCharacter.cpp
Build failed.

Intellisense underlined settimer try replacing its dot . With an arrow (a function call)

GetWorld()->GetTimerManager()->SetTimer(paramters…)

Doesn’t Work :frowning:

Give me a sec. I’ll try a working example and post it

Thank you. I’m Waiting

Seems the syntax for the timer has changed in 5.0. Wasn’t able to get it to work directly with the handle but with the added delegate it works ok.
Don’t forget to set the bomb class in the launcher from within the editor (BombClass)

BaseBombLauncher.h



// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Bomb.h"
#include "BaseBombLauncher.generated.h"



UCLASS()
class DROID_API ABaseBombLauncher : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ABaseBombLauncher();

	

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	float BombDelay = 1;

	UFUNCTION(BlueprintCallable)
	void PullTrigger();

	UPROPERTY(EditAnywhere)
		USceneComponent* ProjectileSpawnPoint;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		UClass* BombClass;

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

BaseBombLauncher.cpp




#include "BaseBombLauncher.h"
#include "GameFramework/Actor.h" 
#include "TimerManager.h"
#include "Bomb.h"

// Sets default values
ABaseBombLauncher::ABaseBombLauncher()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	
	ProjectileSpawnPoint = CreateDefaultSubobject< USceneComponent>(TEXT("Projectile Spawn Point"));

}

// Called when the game starts or when spawned
void ABaseBombLauncher::BeginPlay()
{
	Super::BeginPlay();
	
}

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

}


void ABaseBombLauncher::PullTrigger() {
	FVector Location = ProjectileSpawnPoint->GetComponentLocation();
	FRotator Rotation = ProjectileSpawnPoint->GetComponentRotation();
	FActorSpawnParameters sp;

	

	ABomb* Bomb = GetWorld()->SpawnActor<ABomb>(BombClass, Location, Rotation, sp);
	if (Bomb != nullptr) {
		Bomb->SetOwner(this);
	}
					
	FTimerHandle BombTimer;
	FTimerDelegate BombDelegate;
	BombDelegate.BindUFunction(Bomb, "Explode");
	GetWorld()->GetTimerManager().SetTimer(BombTimer, BombDelegate, BombDelay, false);

}


Bomb.h


#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Bomb.generated.h"

UCLASS()
class DROID_API ABomb : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ABomb();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UFUNCTION()
	void Explode();

};

Bomb.cpp


#include "Bomb.h"
// Sets default values
ABomb::ABomb()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void ABomb::BeginPlay()
{
	Super::BeginPlay();
	
}

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

}

void ABomb::Explode() {

	GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red, GetName() + " goes BOOM!");
	Destroy();

}

P.S.

Change DROID_API to your project API

Thank you but one last thing. how can i add delegate to it?

It’s already internally defined in the function

this part

FTimerDelegate BombDelegate;
	BombDelegate.BindUFunction(Bomb, "Explode");
	GetWorld()->GetTimerManager().SetTimer(BombTimer, BombDelegate, BombDelay, false);

where should i add it to?

in the launcher function PullTrigger in cpp

Thanks again i’m gonna try it right now

Hey Thanks again. I think it worked.

I’ll still try a fresh project with just the timer. The lyra example has a simpler syntax so I might be able to still reduce it.

OK I forgot I need to pass in the correct object instance… must be low on coffee :stuck_out_tongue:

Did a quick fresh project and here is a simpler version without the need of a delegate, you just have to pass in an instance of the object which needs the function called


	if (UWorld* world = GetWorld()) {

		FActorSpawnParameters sp;
		FTransform transform = spawnOrigin->GetComponentTransform();
		ABomb* bomb = world->SpawnActor<ABomb>(BombClass, transform, sp);

		FTimerHandle BombHandle;
		world->GetTimerManager().SetTimer(BombHandle, bomb, &ABomb::Explode, BombDelay);

	}