How do i let WordTimerManager.SetTimer stay within the same class instance?

Hello, I am new and trying to Learn Unreal with C++.
I’ve run into an odd issue and I cannot seem to get a lot of material on how to solve this issue.

In short, I’ve created a Test_Class to learn how WordTimerManager.SetTimer works. Currently the class is testing the accuracy of the timer.

The issue that I am facing is that the Timer is not using the original instance of the class that has all of my initialized variables (or some other issue that I have not noticed). And this causes exceptions when trying to run this class.

I’ve setup the class as follows

UCLASS()
class RPG01_API ATest_Effect: public AActor{
	GENERATED_BODY()

	private:
        /*
        Some Variables
	    */
	    tickrate;
	    FTimerHandle repeater;
	    FTimerDelegate delegate;

    public:
        ATest_Effect():		
		tickrate(0.1),
		Var2...){
			delegate.BindLambda([this](){tick(tickrate);});
			PrimaryActorTick.bCanEverTick = false;
		}
    /*Other Function declarations*/
        

And the implementation as follows:

 void ATest_Effect::BeginPlay()
    {
        Super::BeginPlay();
        GetWorld()->GetTimerManager().SetTimer(repeater, delegate, tickrate, true);
        Debug::Log(TEXT("Start Effect Test"));
    }

    bool ATest_Effect::Tick(float X){
        // Sequence of tests
    }

When Debugging the code i can see that the variables are properly initialized in the BeginPlay(); function.

But when I run the code until the fist time the game ticks my Tick(x) function, then the variables and pointer to *this changes:

Note: This is the only class / actor in this test world.
How do i prevent this from happening?

An update on this.

It seems like WorldTimerManager SetTimer actually points to the original instance of the class.
The reason the debugger does not display the content of the expected class instance is due to the fact that FTimerDelegate uses a weak pointer to point to the Function in the original instance of the class.

An easy work around to allow you to inspect the variable values of the original Instance is to use the function tick(x) to call another function in the declared in the class, for example:

 void ATest_Effect::BeginPlay()
    {
        Super::BeginPlay();
        GetWorld()->GetTimerManager().SetTimer(repeater, delegate, tickrate, true);
        Debug::Log(TEXT("Start Effect Test"));
    }

    
    bool ATest_Effect::runTest(float X){
        // Sequence of tests
    ]

    bool ATest_Effect::Tick(float X){
        return runTest(x);
    }

This will allow me to access and inspect the original values of the class and find the actual problem that causes the exceptions.

PS:
Please correct me if I am mistaken.

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