I’m making a spelling game and I need help with sorting an array of structs of words in alphabetic order. (Each Struct has a string member called word and I need to sort by it)
I have a Blueprint FunctionLibrary C++ That needs to be exposed to blueprints. I think I got that part covered not sure though first time trying to use it. Very new to C++
For the Sort function I got it from the unreal Docs I Just removed the length part, because I’m trying to not sort by length.
I get errors
FunctionLibrary.cpp(10): [C2059] syntax error: '.'
FunctionLibrary.cpp(10): [C2143] syntax error: missing ';' before '{'
FunctionLibrary.cpp(11): [C2065] 'A': undeclared identifier
FunctionLibrary.cpp(11): [C2065] 'B': undeclared identifier
FunctionLibrary.cpp(12): [C2059] syntax error: ')'
FunctionLibrary.cpp
#include "FunctionLibrary.h"
#include "UStructures.h"
#include "Containers/Array.h" // This is greyed out for some reason... Is this how to add an array header I got it from the docs.
void UFunctionLibrary::sortStruct()
{
struct FProfileStruct.WordArray.Sort([](const FString& A, const FString& B) {
return A < B;
});
}
UStructures.h
#pragma once
#include "CoreMinimal.h"
/**
*
*/
#include "UStructures.generated.h"
struct FWordStruct;
USTRUCT(BlueprintType)
struct FWordStruct
{
GENERATED_BODY()
UPROPERTY(BlueprintReadWrite)
FString Word;
UPROPERTY(BlueprintReadWrite)
float DisplayTime = 4.5;
UPROPERTY(BlueprintReadWrite)
int32 Stars;
UPROPERTY(BlueprintReadWrite)
int32 Tested;
UPROPERTY(BlueprintReadWrite)
int32 Correct;
UPROPERTY(BlueprintReadWrite)
int32 Incorrect;
UPROPERTY(BlueprintReadWrite)
int32 Chain;
UPROPERTY(BlueprintReadWrite)
bool NewStar0 = true;
UPROPERTY(BlueprintReadWrite)
bool NewStar1 = true;
UPROPERTY(BlueprintReadWrite)
bool NewStar2 = true;
UPROPERTY(BlueprintReadWrite)
bool NewStar3 = true;
UPROPERTY(BlueprintReadWrite)
bool NewStar4 = true;
};
USTRUCT(BlueprintType)
struct FProfileStruct
{
GENERATED_BODY()
UPROPERTY(BlueprintReadWrite)
TArray<FWordStruct> WordArray;
UPROPERTY(BlueprintReadWrite)
TArray<FWordStruct> TempWordArray;
UPROPERTY(BlueprintReadWrite)
FString Player;
UPROPERTY(BlueprintReadWrite)
int32 StarCount;
UPROPERTY(BlueprintReadWrite)
int32 WordCount;
UPROPERTY(BlueprintReadWrite)
int32 TestedWords;
UPROPERTY(BlueprintReadWrite)
bool ShowTutorial = true;
UPROPERTY(BlueprintReadWrite)
bool ShowCountdown = true;
};
So I thought it was Scope so I made a member function in UStructures.cpp like so…
#pragma once
#include "CoreMinimal.h"
/**
*
*/
#include "UStructures.generated.h"
struct FWordStruct;
USTRUCT(BlueprintType)
struct FWordStruct
{
GENERATED_BODY()
UPROPERTY(BlueprintReadWrite)
FString Word;
UPROPERTY(BlueprintReadWrite)
float DisplayTime = 4.5;
UPROPERTY(BlueprintReadWrite)
int32 Stars;
UPROPERTY(BlueprintReadWrite)
int32 Tested;
UPROPERTY(BlueprintReadWrite)
int32 Correct;
UPROPERTY(BlueprintReadWrite)
int32 Incorrect;
UPROPERTY(BlueprintReadWrite)
int32 Chain;
UPROPERTY(BlueprintReadWrite)
bool NewStar0 = true;
UPROPERTY(BlueprintReadWrite)
bool NewStar1 = true;
UPROPERTY(BlueprintReadWrite)
bool NewStar2 = true;
UPROPERTY(BlueprintReadWrite)
bool NewStar3 = true;
UPROPERTY(BlueprintReadWrite)
bool NewStar4 = true;
};
USTRUCT(BlueprintType)
struct FProfileStruct
{
GENERATED_BODY()
UPROPERTY(BlueprintReadWrite)
TArray<FWordStruct> WordArray;
UPROPERTY(BlueprintReadWrite)
TArray<FWordStruct> TempWordArray;
UPROPERTY(BlueprintReadWrite)
FString Player;
UPROPERTY(BlueprintReadWrite)
int32 StarCount;
UPROPERTY(BlueprintReadWrite)
int32 WordCount;
UPROPERTY(BlueprintReadWrite)
int32 TestedWords;
UPROPERTY(BlueprintReadWrite)
bool ShowTutorial = true;
UPROPERTY(BlueprintReadWrite)
bool ShowCountdown = true;
void SortStructArray()
{
WordArray.Sort([](const FString& A, const FString& B) {
return A < B;
});
}
};
Which works at first glance until I try to call the function.
#include "FunctionLibrary.h"
#include "UStructures.h"
#include "Containers/Array.h"
void UFunctionLibrary::sortStruct()
{
struct FProfileStruct.SortStructArray();
}
How do I use a struct from another file and how to sort a struct by alphabetic order?