Unions and TUnion

Unreal Community,

I am currently using the 4.1 branch of the Unreal source release. And I have run into a slight issue. Nothing that stops me, but is a question. Are there others with issues with compiling when using unions (assuming you have used any)? I have recently attempt to declare a simple union for a class and it stops building and says the declared property is not recognized. I just wanted to not have to waste any memory, but I went with a struct instead. The biggest reason I ask this is because there is a TUnion template in the documentation but when looking at the Core, it says that it is not included because it doesn’t compile. So my real question is, am I going to have to live without unions for UE4 programming?

I’ve used unions just fine, as does the UE4 Source code :slight_smile:

here’s the union I’ve used:

Note it is the typedef that I use in the PlayerController main class header


//declared above player controller header

typedef union floatdata {
  float f;
  unsigned long byteData;
} FloatUnionData;


//inside player controller header

//for converting bytes to float, uses union def
FloatUnionData FloatUnion;


**New Wiki**

I just composed a wiki on using unions in UE4 (mostly just the code)

https://wiki.unrealengine.com/Unions,_Different_Data_Types,_Same_Memory

Rama

I was declaring them just the same, as follows:


#pragma once

typedef union itemValue{
	float fValue;
	int32 iValue;
}ItemValue;

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

/**
 * 
 */
UCLASS()
class AItem : public AActor
{
	GENERATED_UCLASS_BODY()

	UPROPERTY()
	TSubobjectPtr<class UStaticMeshComponent> Mesh;

	UPROPERTY()
	FString ItemName;

	UPROPERTY()
	ItemValue Value;

//The Properties and Functions continue


But I still get a build error:

Oooooh

:slight_smile:

You can’t make unions UPROPERTY() (at the moment)

You can always write a Static Blueprint Library to access your union data in whatever way you want, from any blueprint

like GetUnionAsFloat(ItemValue Value ) or GetUnionAsInt32(ItemValue Value)

You’ll have to declare your union in a header that you can include in your blueprint library class, or have your Blueprint Library class dependson AItem

Wiki Tutorial on Blueprint Libraries

Rama

Ahhhhh, I see now Rama. Thanks. So unions cannot be a UPROPERTY(). That clears it up.

Hi there, did it change since then? Can I somehow use unions so I could see variables straight from blueprint editor? Or should I still use methods to pull any union value?
Thanks