skip to Main Content

Structs Explanation

A structs is a custom variable which you can create. Think of structs as containers, which are constituted by different data types.

Creating a Struct

In order to add a struct, from the class wizard check the “Show all Classes” and select the class Object (I named my stuct PlayerStats in this case). When visual studio pops up, delete the UCLASS() along with all it’s contents and create your struct using the following syntax:

Note that I’ve commented out all the lines of code which need to be deleted. Note that you must declare every property you want to have in the editor as UPROPERTY.

Also, don’t forget to include the Engine/DataTable.h header file right before the .generated.h.

Consuming a Struct

If you want to use the Struct I made above:

  1. Go to your desired class
  2. Before the “NameOfYourActor.generated.h” header include the corresponding header file for your struct (if you used the same name as mine it will be “PlayerStats.h”)
  3. Declare the PlayerStats using the following syntax:

FPlayerStats Stats;

Your header file should be similar to the image below:

structs_header

Now, in your source file you can type for example:

int32 x=Stats.XpToLevel;

Avatar photo

This Post Has 4 Comments

  1. Hi again, indeed another great lesson but shouldn’t you warn people about the efficiency of OOP and classes and the fact that it would be better to deal with data by methods (and especially with getters and setters) through classes ?

    And what would be the best cases to work with either classes or structs ?

    Thanks again so much for your work. =)

    1. Hello, that’s actually a good point. Both classes and structs can use the basic principles of OOP. The main difference is that structs have by default public access to their data, unlike classes which have private access.

      Generally speaking, you should use structs when you want a simple data structure (like a vector or some weapon stats for your game). I would recommend to reverse engineer my Scifi Shooter demo where I’ve used both structs and classes in order to achieve a flexible weapon system which can be extended by only using the UE4 Editor.

      -Orfeas

  2. Hey Orfeas,
    Just wanted to say keep up the work. Was linked now the second time to your page while figuring out stuff for ue4.

Leave a Reply to c4tTi Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top