Struct with Struct Pointer of same type as Parent-Struct

Hey I’m new to C++ and I’m wondering if this is possible in Unreal C++:



//Works fine in C++11
struct OctreeNode{
	int level;
	OctreeNode* child;

	OctreeNode(){
		child = nullptr;
	}
};


I want to do this for my Octree-implementation.

Compile-tested Code For You

I just tested this as compiling just fine, just put it below all your #includes

Use int32 not int because int is ambiguous in terms of size and is compiler/platform dependent, int32 is exact on all platforms.

You can also use int64 if you need more value range.

I also changed to use capitalizations/naming scheme that conforms to UE4 standards.

You really must initialize Level since it will not get a default value, structs get garbage/uninitialized memory for values unless specified in constructor.



//Works fine in UE4 C++
struct FOctreeNode
{
	int32 Level;
	FOctreeNode* Child;

	FOctreeNode()
	{
                Level = 0;
		Child = nullptr;
	}
};


Enjoy!

Rama

Alright thanks Rama!

Actually, this gives me the following exception:



USTRUCT(BlueprintType)
struct FOctreeNode {

	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Octree Struct")
	int32 Level;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Octree Struct")
	int32 Depth;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Octree Struct")
	FOctreeNode* Child;

	FOctreeNode(){
		Level = 0;
		Depth = 0;
		Child = nullptr;
	}
};




FOctreeNode.h(18): error : In TemporaryUHTHeader_FOctreeNode: Inappropriate '*' on variable of type 'FOctreeNode', cannot have an exposed pointer to this type.


Hi there!

You can’t use a pointer to a USTRUCT with UPROPERTY()

This means you cannot replicate such a pointer using USTRUCT replication

But I dont think you really want to replicate an octatree over the network anyway right! Just do the calcs on the server and send move commands / required visual updates

so just do this



USTRUCT(BlueprintType)
struct FOctreeNode {

	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Octree Struct")
	int32 Level;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Octree Struct")
	int32 Depth;

	FOctreeNode* Child;

	FOctreeNode(){
		Level = 0;
		Depth = 0;
		Child = nullptr;
	}
};


:slight_smile:

You can still edit such a struct in the editor, you just will have to link the pointers together in code at runtime :slight_smile:

Rama