Announcement

Collapse
No announcement yet.

Crash after calling a function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Crash after calling a function

    Hey! I am working on a function for my Weapon system. The problem is that when I call the "FireWeapon()" function, the Editor crashes with the following message:

    http://pastebin.com/JF8LQm0P

    I have no idea why it happense, but I am guessing that it has to do with some kind of code overload?

    Here are all the calls associated with "FireWeapon()":

    Code:
    void AFPSCharacter::OnFire()
    {
    	if (CurWP != NULL)
    	{
    		const FVector cameraLocation = FirstPersonCameraComponent->GetComponentLocation();
    		const FRotator cameraRotation = FirstPersonCameraComponent->GetComponentRotation();
    		CurWP->InstPos = cameraLocation;
    		CurWP->InstRot = cameraRotation;
    		CurWP->FireWeapon();
    	}
    }
    Code:
    void ADemoWeapon::FireWeapon()
    {
    	Super::FireWeapon();
    
    	UWorld* const World = GetWorld();
    	if (World != NULL)
    	{
    		// Spawn the projectile at the viewpoint (camera) of the player
    		World->SpawnActor<AFPSProjectile>(wpData.Projectile, InstPos, InstRot);
    	}
    }
    Anyone know what the problem could be? Help with this matter would be much appreciated ) Thank you in advance! Feel free to ask any questions if something needs clarifying.

    #2
    Hey Borzi!

    A few things you might want to do, just to be sure:

    In "OnFire()", do a null check for your "FirstPersonCameraComponent"
    (It's not where the error is, but it's good practice for pointers)

    And secondly, the only issue I can see with "FireWeapon()" is that "wpData.Projectile", could potentially be null.
    So a check there would fix the issue, but then the issue of not having a projectile could arise.

    Also, I'm assuming "wpData" is a struct, so make sure it's a UPROPERTY, since garbage collection can be an issue.
    (I'm not a fan of pointers inside structs. I tend to use "TWeakPtr" if I have to).

    I would suggest a few On screen debugs to make sure you actually have a projectile. xD

    I hope this helps!

    -Peace
    Last edited by Loken01; 09-06-2014, 05:55 AM. Reason: Added stuff!
    For all and any info on our game in progress: SleepStep: Nightfall
    Go to our site: GearDrop Games


    Check out my tutorials/code snippets!

    Comment


      #3
      Thank you very much for the reply, but im afraid that this did not solve the problem There was indeed a projectile attached. Have you taken a look at the crash error? I don't really think that the firing function has anything to do with it anymore. Maybe the declaration?

      Here is what I have in my Weapon.h:
      Code:
      /** [local] weapon specific fire implementation */
      	virtual void FireWeapon() PURE_VIRTUAL(AWeapon::FireWeapon, );
      And here is what I have for the DemoWeapon.h:
      Code:
      virtual void FireWeapon() override;
      Oh and by the way: What is WeakPtr? It would be kind of you to tell me, because i have seen it, but never understood how it works or is implemented.

      Comment


        #4
        Originally posted by Borzi View Post
        Oh and by the way: What is WeakPtr? It would be kind of you to tell me, because i have seen it, but never understood how it works or is implemented.
        You're using garbage collection when you use C++ in Unreal. What that means is that your memory is managed for you, if you have a pointer to an object that object will stick around, if not then the memory can be freed for other things to use it.

        A weak pointer lets you hold a reference to something without forcing it to stay in memory. You can use it to access that data as long as it sticks around, but if there are only weak pointers to an object then the garbage collector will be free to clean up the object anyway.

        EDIT: https://docs.unrealengine.com/latest...ter/index.html

        Comment


          #5
          Originally posted by Veovis Muad'dib View Post
          A weak pointer lets you hold a reference to something without forcing it to stay in memory. You can use it to access that data as long as it sticks around, but if there are only weak pointers to an object then the garbage collector will be free to clean up the object anyway.
          Thanks for the reply, that makes a lot more sense now Anyone maybe also have a solution for the crash?

          Comment


            #6
            Pure virtual not implemented (AWeapon::FireWeapon)
            Code:
            virtual void FireWeapon() PURE_VIRTUAL(AWeapon::FireWeapon, );
            From your error log and your code, it tells you whats wrong. I don't know what the PURE_VIRTUAL macro does, but I don't think you want it. Pure Virtual functions are abstract functions that don't have an implementation, which isn't what you want.

            Does your FireWeapon override another function, because if not, it shouldn't be virtual either.

            Comment


              #7
              So what would you suggest instead? I need to have the "FireWeapon" override in my subclass, I just adapted this from the shooter example where they used this. Besides, doesn't the function have to be of type virtual void so i can override it in the first place?

              Comment


                #8
                I haven't looked at the example, so I'm not sure. I would try replacing that line with:

                Code:
                virtual void FireWeapon();
                and see what happens.

                Comment


                  #9
                  That does not seem to work :/ here is the error i get (I still have the same line to override the function in the DemoWeapon):

                  'ADemoWeapon::FireWeapon' : method with override specifier 'override' did not override any base class methods

                  It seems like I have to flag the function in the base class as being overrideable, but I don't know how...

                  Comment


                    #10
                    Can you post the .h files for both the parent class and the subclass?

                    Comment


                      #11
                      Sure!

                      Here is what I have in the base class:

                      Code:
                      /** [local] weapon specific fire implementation */
                      	virtual void FireWeapon() PURE_VIRTUAL(AInstinctWeapon::FireWeapon,);
                      Here is what I have in the sub class:

                      Code:
                      virtual void FireWeapon() override;

                      ....and thats basically all I have for the FireWeapon() function

                      EDIT: I should note that I have tried the previously mentioned suggestions, but they resulted in a compile error.
                      Last edited by Borzi; 09-10-2014, 04:03 AM.

                      Comment


                        #12
                        I have found the solution. The problem was that I was calling the virtual void externally from a different script, which is not possible aparrently. I made the following function and then called it from my character script:

                        Code:
                        void AWeapon::ManageFire()
                        {
                        	GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("Managing weapon"));
                        	if (bReadyToShoot())
                        		{
                        			FireWeapon();
                        			FireTimer = WeaponInfo.FireRate;
                        			if (WeaponInfo.UsesClip)
                        				{
                        				BulletsFired++;
                        				}
                        			else
                        				{
                        				Ammo--;
                        				}
                        		}
                        	if (bEmptyClip())
                        		{
                        		if (bCanReload())
                        			{
                        			ClipReload();
                        			}
                        		}
                        }
                        So Pure_Virtual is the way to go

                        Comment

                        Working...
                        X