Download

Small problem with struct

Hi there!
I’m having trouble compiling a simple character I wanted with a prebuilt set of variables within a struct I could access to.
This is my code:


#pragma once

#include "GameFramework/Character.h"
#include "CustomCharacter.generated.h"

UCLASS()
class ACustomCharacter : public ACharacter
{
	GENERATED_UCLASS_BODY()

public:
	
	USTRUCT(Blueprintable)
	struct FMyCommand
	{
		GENERATED_USTRUCT_BODY()

		UPROPERTY()
			int32 ID;

		UPROPERTY()
			int32 BaseDamage;

		UPROPERTY()
			float Playrate;
	};

	struct FMyCommand NCommand;
};

The compiler keeps saying “FMyCommand isn’t a class name or namespace” and “Identifier not declared”. What am I doing wrong? Am I supposed to name my struct in a very specific way or something?

Structs are like Classes, in fact they work very similar, both are skeletons which can contains properties and functions. There for class and struct declaration can’t be contained as property of another, so place your struct outside any class and in the class make a property using struct as a type of your object :slight_smile: Best would be if you declere your struct in header file

I tried that as you see here:


#pragma once

#include "GameFramework/Character.h"
#include "CustomCharacter.generated.h"

USTRUCT(Blueprintable)
struct FMyCommand
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY()
	int32 ID;

	UPROPERTY()
		int32 BaseDamage;

	UPROPERTY()
		float Playrate;
};

UCLASS()
class ACustomCharacter : public ACharacter
{
	GENERATED_UCLASS_BODY()
private:

public:
	struct FMyCommand NCommand;
};

However, this doesn’t seem to compile either.
Struct outside the class definition and then I use FMyCommand as a type.

Ohhhh i missed that, change

struct FMyCommand NCommand;

to

FMyCommand NCommand;

as you already declared struct and making property of type of struct, compiler things you trying to declere struct again

That won’t help… you can define a struct variable starting with the keyword struct. This is actually how you have to declare them in C (which is why you find a lot of typedefs for structs in old code).

Yeah, that doesn’t seem to work either.
Isn’t there any example out there of a struct being used in a class? It’s kinda embarrassing that I can’t get a simple struct compiling properly.

For some reason (and I really don’t know why) but if you change the struct to this:



USTRUCT(Blueprintable)
struct FMyCommand
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY()
	int32 Id; //Lowercase d here

	UPROPERTY()
		int32 BaseDamage;

	UPROPERTY()
		float PlayRate; //Capital R in rate
};


Then it will compile fine. My guess is that the generated code requires variable names to be a specific format? This could be a bug, I don’t know.

EDIT:
By the looks of it, you still have to define it outside of the class.

For some weird reason that seems to compile properly. I guess that there’s some rule telling the compiler not to compile variables with a certain name…
However, even after compiling I can’t access the struct from my blueprint. Is there any parameter or anything I need to add to be able to access it?

I do need to access it from the blueprint. I want to make my character have a “equippable” attacks from a menu to use in-game, and organizing all the information for each attack in a struct would be the way to go in my opinion. However, if I use a regular struct I can’t access it from the blueprint system, can I?

EDIT: Nevermind, I actually just got it working. I can just use the Break node from the blueprint system.