Properly casting structs

Hello.


I’m trying to cast my child struct to my base struct, but without any luck.

I declared this child struct, which inherits from my base struct.

struct FItemWeaponInfo : public FItemBaseInfo

Then I tried to add a FItemWeaponInfo struct to an array of FItemBaseInfo(the variable called Inventory).

FItemWeaponInfo WeaponTest;
Inventory.Add(WeaponTest);

But I can’t seem to get it to work, I tried casting, but I couldn’t make that work either.

Any ideas?

Hi,

I have tried to reproduce your problem and i don’t have any problem when i add my
FItemWeaponInfo to my array of ItemWeaponInfo.

.h file

#pragma once

#include "GameFramework/Actor.h"
#include "TestStruct.generated.h"

USTRUCT(BlueprintType, Blueprintable)
struct FItemBaseInfo {
    
    GENERATED_BODY()
    
    UPROPERTY(Category = "Item Base Info", BlueprintReadWrite, EditAnywhere)
    int32 Id;
    
    UPROPERTY(Category = "Item Base Info", BlueprintReadWrite, EditAnywhere)
    FName Title;
};

USTRUCT(BlueprintType, Blueprintable)
struct FItemWeaponInfo : public FItemBaseInfo {
    
    GENERATED_BODY()
    
    UPROPERTY(Category = "Item Weapon Info", BlueprintReadWrite, EditAnywhere)
    bool Enable;
};

UCLASS()
class PROTOTYPE_4_12_API ATestStruct : public AActor
{
	GENERATED_BODY()
	
public:
    
    UPROPERTY(Category = "Items", BlueprintReadWrite, EditAnywhere)
    TArray<FItemBaseInfo> Items;
    
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

    UFUNCTION(Category = "Items", BlueprintCallable)
    void AddToArray();
	
};

.cpp file

#include "Prototype_4_12.h"
#include "TestStruct.h"

// Called when the game starts or when spawned
void ATestStruct::BeginPlay()
{
	Super::BeginPlay();
	
    this->AddToArray();
}

void ATestStruct::AddToArray()
{
    //Create items
    FItemBaseInfo ItemBase;
    ItemBase.Title = "Title1";
    
    FItemWeaponInfo ItemWeapon;
    ItemWeapon.Title = "Title2";
    
    //add to array
    Items.Add(ItemBase);
    Items.Add(ItemWeapon);
    
}

Result :

89909-ue4_answerhub_struct.png

Huh, you’re right. I had a chain of inheritance, like weapon inherits from stats, stats inherits from base. But I had forgotten to actually inherit from the base on the stat info. My bad.

Thank you, though. Probably would’ve taken me a while to figure it out. :slight_smile:

I have another problem though. When I cast it from FItemBaseInfo to FItemWeaponInfo from the Inventory array, the variables contained doesn’t seem to work.

I tried this

FItemWeaponInfo WeaponTest = FItemWeaponInfo();
WeaponTest.WeaponDamage = 125.f;

GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, FString::SanitizeFloat(WeaponTest.WeaponDamage));

Inventory.Add(WeaponTest);

for (int i = 0; i < Inventory.Num(); i++)
{
	FItemWeaponInfo* WeaponInfoTest = static_cast<FItemWeaponInfo*>(&Inventory[i]);
	if(WeaponInfoTest)
		GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, FString::SanitizeFloat(WeaponInfoTest->WeaponDamage));
}

But it didn’t print the value I set it to.

EDIT: The first debug message printed the correct value, but the second one(in the for loop) printed -431602080, which I’m guessing is just because it isn’t set to a value (which it should be).

The static_cast will always return a pointer, even if it fails to cast. It just gives you a garbage-memory pointer struct which passes the “if”-check unfortunately.