C++ Interface: "Member Declation not allowed here".

I would like all of my instanced classes that feature the ‘IBZGame_GameObjectInterface’ Interface, to have a UPROPERTY Struct that I can edit in all of the blueprints that I make of those classes.

So for example, my struct contains things such as ‘build cost’, or ‘build time’, and I want to be able to specify those in every class that has the GameObject interface, and expose the structs variables to Blueprints so that each BP class can have it’s own values.

However, when I try to declare a UPROPERTY in the interface class, I get the following compiler error, but I cannot understand why this won’t work, or how to work around it? (Note: Removing the UPROPERTY macro allows me to compile, but of course it means this variable won’t be exposed to blueprints of classes that have this interface).

UINTERFACE(MinimalAPI)
class UBZGame_GameObjectInterface : public UInterface
{
	GENERATED_UINTERFACE_BODY()

};

class BZGAME_API IBZGame_GameObjectInterface
{
	GENERATED_IINTERFACE_BODY()

	/* Return the GameObject Data for use */
	FORCEINLINE FGameObjectData GetGameObjectData() const { return GameObjectData; }

protected:
	/* Struct Containing All Relevant Game Object Information, such as Build Times, Resource Costs etc. */
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "GameObjectClass")
	FGameObjectData GameObjectData;
};

The Error:
error : In BZGame_GameObjectInterface: ‘Member variable declaration’ is not allowed here

How can I workaround this?

#A Solution

You can make a virtual getter in your interface

virtual FYourDataStruct& GetDataStruct() = 0;

Then each class that implements the interface has to provide their stored version of the structure

But the end result is that you can perform logic on the data structure at the interface level, in the .cpp, using GetDataStruct() (even though it is pure virtual in the .h)

That’s as uniform as you can make it with an interface + variable data that is common to all extenders of the interface

To re-iterate, you then do the logic on the data structure in the .cpp of the interface itself.

Because the function is returning by reference, you are able to manipulate the data!

Any other class wanting to manipulate the data that is common to the interface can use the function too!

:slight_smile:

Rama

Thanks Rama! I need to look into this technique more :slight_smile:

Hey The Jamsh

Did Rama’s solution work? I’m trying to get a very similar thing working, but still getting the error you reported, after using Rama’s solution. Cheers

Hey The Jamsh, I think the struct has to be static. But I don’t think you can make a static variable a uproperty…

I can’t seem to make this work with blueprints. The function doesn’t show up in the editor, and any combination of UFunction with specifiers that tell the blueprint that it can implement the function doesn’t compile. Any suggestions?

,The Issue is with the UPROPERTY().You need to remove all UPROPERTY() in the interface for you to build