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:
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()”:
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.
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
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:
/** [local] weapon specific fire implementation */
virtual void FireWeapon() PURE_VIRTUAL(AWeapon::FireWeapon, );
And here is what I have for the DemoWeapon.h:
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.
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.
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.
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?
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:
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();
}
}
}