The instructor creates de Data Item struct using a Blueprint Function Library. But it also says: “but we could do this in any number of ways”. And that is what I want to know, the other ways to do it because creating a class to just add a USTRUCT, I think it isn’t useful:
Singleplayer or Multiplayer? What is the struct being used for? Which area of gameplay. These are the questions that will help you determine where else.
By example, I have a structs in my gamestate, playerstate and etc (multiplayer game). Depending on the need and function, you could add to player character, actor component, controller (there are more examples).
There is no “all those places”. You can literally declare structs anywhere. The question you should be asking is where does it make sense code-wise. The error that you received clearly says what the issue is as well. You are using USTRUCT and UPROPERTY macros, which means Unreal will generate boilerplate code for blueprint support. This generated code is in a file called <OriginalFile>.generated.h. You don’t have this included.
If the data needs to be available to all players, you can put in PlayerState. Alternatively you can use it in the PlayerPawn.
Where you can you put it?
Game State
Player State
Player Pawn
Player Character
A universal class if you want to make one
A player controller (not a good call overall)
When would you put it in each?
Game State - Its a struct you want available to all classes, players and needs to pass the information between player and server specifically. If it contains data you don’t want shared, choose another option.
Player State - Its a struct you want available to all classes, but really just contains player information that you may need to share with others to feed stats or popup/inspect data.
& 4. Player Pawn or Player Character - Contains private data that you don’t necessarily want to share with others and pertains only to the character or particular pawn.
Universal class - you have a bunch of data you want to use on repeat or you will be using for a lot of classes, this could be a choice for you. Biggest thing here is to make sure its being initiated and such.
I struggled to find why I would put a struct in a player controller. I am generally against this but thats personal. Technically, just like with the player pawn/character you could store specific data for the player in the controller.
Most importantly make sure you initialize the struct, remember it will only store the most recent data passed and to keep the data you will need a variable, class, tarray etc to actually hold the records. I know you didn’t ask, no offense intended just a reminder