Hitting a breakpoint with the following function system, I’m trying to return a protected variable of type ‘enum’ via a public function, and then do a comparison to see if it equals another enum. How can I make the following code work properly?
BZGame_Weapon.h
UFUNCTION(BlueprintPure, Category = "Weapon")
EBZGame_WeaponClass GetWeaponClass() const;
protected:
/* Weapon Class - Set By Code Automatically. */
UPROPERTY(VisibleDefaultsOnly, Category = "Weapon")
EBZGame_WeaponClass WeaponClass;
That sets the variable of this specific class, but I want to see if the variable matches another one in the following class, to see if I can spawn it like so:
Not sure if this is related, I’m getting a similar error … perhaps casting to the value of a struct isn’t such a good idea? The following code never prints out the message, indicating that the cast is failing…
Tried the code you posted, and also checked with == nullptr. With the == null ptr it proceeded through, so the cast is definitely failing. This must be something to do with having a struct with a <TSubclassof> system in there maybe.
If you’re worried about the cast failing because WeaponConfig is a struct, don’t be because that doesn’t matter. You already mentioned that in both cases the cast returns null pointers, so the casts are failing. Are you sure you initialized the values you’re attempting to cast? Use an F9 breakpoint right after casting to check the values of your input. For example to check whether WeaponConfig.OrdnanceClass is initialized.
Yea the values are initialized and do exist. Oddly enough I did exactly what you said for a similar issue using TSubclassOf, where the cast couldn’t actually access the discrete class specified there (and so was failing, which was why I kept getting an AV crash).
I did a bit more digging and found that when I was using a TSubclassOf<Blah> inside a USTRUCT, I couldn’t call functions on that Subclass. It makes sense (but for reasons I can’t currently remember), and I’m wondering if the same thing applies to Enums?
To get around the TSubclassOf problem, I have to return the default class of that variable like this (From Answerhub Post ):
The GetDefaultObject function seems to just be a slightly slower but safe cast, and allows me to actually call functions on that object and in this case, return a variable from it. Beforehand, this wouldn’t work and gave me an access violation (same error and in the same location as the enum issue). I’m wondering if there’s a similar function to ‘GetDefaultObject()’ for enums, or if I can use that same function on an enum declared like so (I think, if I remember the syntax properly):
enum class EBZGame_WeaponClass
{
Type:
Blah
Blah
};
Didn’t work , I get a breakpoint on the last function you wrote in that text. This function is the cause of the breakpoint, but the breakpoint appears in the actual ‘GetWeaponClass’ function.
void UBZGame_GameObjectComponent::SpawnDefaultHardpoints()
{
if (GetOwner()->Role < ROLE_Authority)
{
return;
}
int32 NumWeaponClasses = HardPoints.Num();
for (int32 i = 0; i < NumWeaponClasses; i++)
{
if (HardPoints*.DefaultWeapon)
{
ABZGame_Weapon* TestWeapon = Cast<ABZGame_Weapon>(HardPoints*.DefaultWeapon);
if (TestWeapon->GetWeaponClass() == HardPoints*.HardpointClass)
{
FActorSpawnParameters SpawnInfo;
SpawnInfo.bNoCollisionFail = true;
ABZGame_Weapon* NewWeapon = GetWorld()->SpawnActor<ABZGame_Weapon>(HardPoints*.DefaultWeapon, SpawnInfo);
AddWeapon(NewWeapon, i);
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("Weapon Class in Hardpoint %i Doesn't Match Hardpoint Class!"), i));
}
}
}
}