How To Make UStruct
| Overview Hi, I'm mintchococookie. I am a newbie to UE4, and I spent 2 days wondering, searching for the way I can make Ustruct. All I got was bunch of errors...nevertheless, I made it. Now I deci...
|
Overview
Hi, I'm mintchococookie. I am a newbie to UE4, and I spent 2 days wondering, searching for the way I can make Ustruct. All I got was bunch of errors...nevertheless, I made it. Now I decided to make a brief tutorial about making Ustruct, for newbies like me.
The goal of this tutorial is to make a Ustruct that is accessible through blueprint.
Caution: To follow this tutorial without any problem, please use UE4.17 and VS2017
Scope & Notes
- You will be able to make UStruct, which is accessible through Blueprint, as you want.
- You will be able to make an array of arrys, using UStruct.
What is UStruct?
You may know what struct is in C and C++. UStruct is Unreal version of struct. It's a composite data type that has various types of variables in it. The cool thing is, you can wrap anything inside a UStruct. For example, Unreal Engine does not provide an array of arrays; TArray wiil definitely produce an error. But if you wrap an array inside your UStuct, and then declare an array of that Ustruct, you can make an array of arrays.
Making a UStruct
Making a New C++ UE4 Project
In your UE4 launcher, select C++ project with basic codes option. Name the project as Ustruct0. UE4 and VS will open up.
Declaring a UStruct
Some guys like me might wonder, 'Where should I declare my Ustruct? Do I have to make special h and cpp files for my Ustruct?'. The answer is "You don't have to do it." I am going to declare my Ustruct in the default game h file provided by UE4. That is, Ustruct0.h file.
In your VS, locate the Ustruct0.h. Copy and paste the code below. Don't forget to include .generated.h file! In this case, it is Ustruct0.generated.h.
inside Ustruct0.h
#pragma once
#include "CoreMinimal.h"
#include "Ustruct0.generated.h"
USTRUCT(BlueprintType)
struct FStudent
{
GENERATED_BODY()
UPROPERTY(BlueprintReadWrite, Category = "Student")
int32 Age;
UPROPERTY(BlueprintReadWrite, Category = "Student")
FString Name;
};
BlueprintType keyword allows us to access the UStruct we made.
Using your UStruct inside Blueprint
Let's make our project output student's age whenever A key is pressed and output student's name whenever N key is pressed.
-
Open Level Blueprint
-
Add a new variable with Student type. Name it student
-
Drag and drop student variable into Level Blueprint editor. Choose set. SET node will be created.
-
Drag out the input pin from the SET node. Choose Make Student. Make Student node will appear.
-
In your Make Student node, enter 7 into the blank named Age. Enter Mintchoco into the blank named Name.
-
Right click your mouse and search for Begin Play and create the node by clicking it. Connect the Begin Play node to the SET node.
-
Drag and drop student variable into Level Blueprint editor. Choose get. student variable will be created inside the editor.
-
Drag out the output pin from the student variable. Select Break Student. Break Student node will appear.
-
Drag out the Age output pin from the Break Student node. Search for Print String and create the node by clicking it.
-
Drag out the input execution pin from the Print String node. Search for Input A and create the node by clicking it.
-
Drag out the Name output pin from the Break Student node. Search for Print String and create the node by clicking it.
-
Drag out the input execution pin from the Print String node. Search for Input N and create the node by clicking it.
Click Compile and Play to see the result.
Let's See the Result!
Whenever you press the A key, you will see the number 7 on the left top side of the viewport. Also, whenever you press the N key, you will see my nick "Mintchoco" on the viewport.
Making an Array of Arrays Using UStruct
UE4 does not support an array of arrays. In order to implement this, we need to wrap array within UStruct. Let's modify the above code to add an array named Subjects to the Student USturct, which is an array of subjects that the student had taken. Also, we will make an array named Students, which is an array of Student UStructs. The structures of the modified Student UStruct and Students array are described in the picture below.
Modify the Ustruct0.h as following:
#pragma once
#include "CoreMinimal.h"
#include "Ustruct0.generated.h"
USTRUCT(BlueprintType)
struct FStudent
{
GENERATED_BODY()
UPROPERTY(BlueprintReadWrite, Category = "Student")
int32 Age;
UPROPERTY(BlueprintReadWrite, Category = "Student")
FString Name;
UPROPERTY(BlueprintReadWrite, Category = "Student")
TArray Subjects;
};
More Information
You have finished your long journey. Congratulations. You will be able to make UStruct as well as array of arrays as you want.
You can read more about me on my . Have a nice day :).
- Mintchococookie