How to use SetTimer correctly?

Hi ,

Try including an if statement around that line to make sure that your GetWorldTimerManager is valid before attempting to run it. It also may be that the parenthesis after GetWorldTimerManager may not be necessary, try removing those if the first option does not work.

Thanks for your quick reply. GetWorldTimerManager is a method of the Actor class. So I definitely need the parenthesis (at least I get an error if I remove them and I could not find an other way to get the TimerManager).
Additionally the GetWorldTimerManager gives me a reference to the TimerManager object. So it should be a valid object (at least it can’t be null). But it seems to be the right way. Because it does not matter which TimerManager function I call, but I always get the same access violation.
Even the reference that I get using
FTimerManager& mgr = GetWorld()->GetTimerManager();
seems to bee invalid. But I cannot check if the reference is valid (the reference is, but the pointer that is referenced by it seems to be not).
Do I have to initialize the TimerManager in some way before using it?

Hi there,

I try to get warm using the Unreal Engine and try to implement a very lightweight shooter system. Therefore I had a look at the shooter game example and tried to implement a very basic version of this step by step.
I want to use the SetTimer-method as the ShooterGame does to recall my HandleFiring-method again after a certain time between two shots.

So I used it similar as the ShooterGame:

void AWeapon::HandleFiring()
{
	print( TEXT( "HANDLE FIRED" ) );
	if( MyPawn && MyPawn->IsLocallyControlled() )
	{
		FireWeapon();
	}
	
	if( MyPawn && MyPawn->IsLocallyControlled() )
	{
		if( Role < ROLE_Authority )
		{
			ServerHandleFiring();
		}

		if( currentWeaponState == WeaponState::Firing )
			GetWorldTimerManager().SetTimer( this, &AWeapon::HandleFiring, 0.1f, false );
	}
}

If I do so, the game will crash as soon as I press the fire button and the debugger tells me that there is an access violation in the SetTimer line. But I cannot really see what I am doing wrong. For me it looks quite similar as the shooter game does handle this. So do I have to initialise this TimerManger first? Or prepare the usage somewhere? Or do you have any other idea why this piece of code could crash?

Thank you for your help.

EDIT:
Okay the problem is not the TimerManager, but the World. If use getWorld() to get a pointer to the world class this pointer is NULL. So my Weapon does not have a correct world pointer. Has anyone an idea what I can do to fix this? Currently my Character class has a Weapon-Field that is set within the Character-Blueprint to my basic weapon class. So probably I have to attach the Weapon somewhere to the world. But I have no idea how I could do this, since the weapon object should only be part of the player’s inventory.
Do you have any hint for me?

Hi ,

I tried editing the first person code template to include automatic-fire from the weapon. I was able to get it working using GetWorldTimerManager().SetTimer(). Do you get the same error if you modify the first person code template?

Hi, the first person code template does not implement a weapon class, but simply creates projectiles on fire. So I had to implement the GetWorldTimerManager().SetTimer() within the character. This works - even in my own project.
So I can use the timer within my Character class, but within my weapon class the world pointer is null. So I probably missed to do some initialization work to get a valid world pointer in my weapon class.
My weapon is used by my character with the help of:
UPROPERTY( EditDefaultsOnly, Category = Inventory ) TSubclassOf<class AWeapon> DefaultWeapon;

This DefaulWeapon variable is set within my character blueprint to my weapon class. That’s all I am doing for weapon handling currently. I use this DefaultWeapon.GetDefaultObject()->StartFire() to trigger my weapon’s fire-method. But within this fire-methods (in the weapon class) I cannot use the timer, since I get an invalid world pointer.

Ah I see. Probably just using this SubclassOf is not correct. I have to spawn the weapon actors at the start of my character class. I will try this and hope that this will resolve my problems :). Thank you.

Sorry for the delayed response. Were you able to get this working the way you needed?

Hey -

Just wanted to see if you were able to get the SetTimer working the way you needed or if this was still an issue. If so could you test to see if it is working for you in 4.6?

Cheers

Hi ,

We have not heard back from you in a few days, so we are marking this post as Resolved for tracking purposes. If you are still experiencing the issue you reported, please respond to this message with additional information and we will offer further assistance.

Thank you.

Sorry I simply forgot to answer to your question. Thank you, I could solve the problem and it was exactly the mistake that I missed spawning actors.