Hi, I got a background in C++ and I am finding it difficult to separate what is memory managed and what is not, and how to use UObject or Struct correctly to integrate with blueprints.
My ‘‘game’’ is mostly a puzzle/question-solving game. The puzzles/questions are generated by a separate class I created as a UBlueprintFunctionLibrary
.
This class has a GenerateQuestions(...)
function returns a TMap<ELevelName, F_CPP_QuestionArray>
. F_CPP_QuestionArray
is a TArray<F_CPP_Question>. An AI controller calls GenerateQuestions()
and stores them. It then pops a question of the right level and instructs a widget creator to make the UI for the F_CPP_Question
. The AI binds the right events and continues till the right number of questions have been answered.
Below are the structs and the skeleton for the GenerateQuestions function:
USTRUCT(BlueprintType)
struct F_CPP_Question
{
GENERATED_BODY()
FORCEINLINE F_CPP_Question();
//constructor
explicit FORCEINLINE F_CPP_Question(
const FString &pQuestion,
const TArray<FString> &pAnswers,
const FString &pCorrectAnswerString,
const int32 pCorrectAnswerInt,
const TArray<FString> &pQTags,
const E_CPP_LevelName pQLevel);
//~ The following member variable will be accessible by Blueprint Graphs:
// This is the tooltip for our test variable.
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CIS|Exercise")
FString Question;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CIS|Exercise")
TArray<FString> Answers;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CIS|Exercise")
FString CorrectAnswerString;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CIS|Exercise")
int32 CorrectAnswerInt;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CIS|Exercise")
TArray<FString> QTags;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CIS|Exercise")
E_CPP_LevelName QLevel;
};
STRUCT(BlueprintType)
struct F_CPP_QuestionArray
{
GENERATED_BODY()
FORCEINLINE F_CPP_QuestionArray();
explicit FORCEINLINE F_CPP_QuestionArray(const TArray<F_CPP_Question>& pQArray);
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CIS|Exercise")
TArray<F_CPP_Question> QArray;
};
void U_CPP_ExerciseGenerator::GenerateExerciseQuestions(int32 MaxValEx, TArray<int32> ExerciseValuesToUse, int32 NumberOfQuestions, TMap<E_CPP_LevelName,F_CPP_QuestionArray>& QuestionsArrayByLevel){
F_CPP_QuestionArray QuestionArr = F_CPP_QuestionArray();
QuestionArr.QArray.Add(F_CPP_Question()); //logic missing on q gen
//will this not go out of scope?
QuestionsArrayByLevel.Add(E_CPP_LevelName::Level_1, QuestionArr);
}
So, my question is. Will the above work? Is &QuestionsArrayByLevel
already managed by Unreal? I am confused as I read that UObjects
are memory-managed and Structs
are not. In that case, should I wrap F_CPP_QuestionArray
in a UObject
? Or, a smart_ptr
?
In normal C++ I would normally just call new()
but blueprints don’t support pointers if I am not wrong.
So, can someone let me know what is correct?
Thanks a lot.