x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How to use SetTimer correctly?

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?

Product Version: Not Selected
Tags:
more ▼

asked Jul 02 '14 at 09:15 PM in C++ Programming

avatar image

Fred_FS
6 1 2 5

avatar image Adam Davis STAFF Jul 02 '14 at 09:26 PM

Hi Fred_FS,

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.

avatar image Fred_FS Jul 02 '14 at 09:53 PM

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?

avatar image Tim C ♦♦ STAFF Jul 03 '14 at 06:42 PM

Hi Fred_FS,

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?

avatar image Fred_FS Jul 03 '14 at 07:01 PM

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.

avatar image Fred_FS Jul 03 '14 at 07:18 PM

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.

avatar image Tim C ♦♦ STAFF Sep 06 '14 at 06:29 PM

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

avatar image Doug E ♦♦ STAFF Dec 12 '14 at 09:25 PM

Hey Fred_FS-

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

Doug Wilson

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Hi Fred_FS,

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.

more ▼

answered Dec 17 '14 at 10:03 PM

avatar image Fred_FS Dec 17 '14 at 10:08 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question