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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

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?

Product Version: UE 4.12 Preview
Tags:
more ▼

asked May 08 '16 at 09:52 PM in C++ Programming

avatar image

Wrekk
82 24 24 28

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

1 answer: sort voted first

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 :

alt text

more ▼

answered May 09 '16 at 12:01 PM

avatar image

Maxwell_n11
36 1 8

avatar image Wrekk May 09 '16 at 12:55 PM

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. :)

avatar image Wrekk May 09 '16 at 06:00 PM

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).

avatar image ellocator Jan 28 '19 at 03:12 PM

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.

(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