PSA: "Accessing UI Classes in C++" Please do not use FClassFinder

For any who stumble upon this and happen to see this. There are so many better ways to reference a blueprint object in C++ code and using FClassFinder is the absolute worst method of doing it. I am a veteran UE4 developer and I picked up this course because there are a few bits of gold nuggets here.
But NEVER hard code a reference to an asset for your project, Please for the love of your developers and designers sanity never ever do that. Always have it be configurable in Blueprint, ALWAYS. UE4 has so many ways to get a reference to the object you need in your game that you should never need to use FClassFinder. C++ and Bueprints provide, Property Pointers, TWeakObjectPtr, TAssetPtr, TSoftClassPtr, and TSubclassOf which can be edited and even created in Blueprints. Couple this with UE4’s built in support for a DataSingleton UObject or use one of the classes that exist on both clients and servers such as GameInstance, or GameState and all of this can be configurable within your game without the use of Hard coding a reference to something you’d have to perform a recompile to change, it’s not worth it and causes way more headaches than problems solved.

In the example used for this project the developer should have used a TSubclassOf < Pawn > property and assigned the class and created a blueprint derived version of the game instance, configure the property, and then within the project and assigned it through the ProjectConfig properties under General Settings to the BP GameInstance.

13 Likes

You’re absolutely right. We include this method for completeness but it’s definitely the inferior way. Thanks for sharing your experience.

@ArcainOne What do you think about it being put in the game mode like it is here?

https://docs.unrealengine.com/en-US/Programming/Tutorials/UMG/index.html

1 Like

@Shinijami Personally, no I would not suggest using the GameMode. Especially if you are building a multiplayer game because the GameMode only exists on the Server, clients who need UI will not have it. You can get away with it if you are building a single player game but it will come back to bite you in the end if you decide later to try and add multiplayer.

For UI, I have a tendency to use the AHUD as a Menu controller that manages what menus are shown and is manipulated through player controller functions. You can also use the PlayerController since every player will have access to that but I often reflect on the Single Responsibility Principle which is why I use the AHUD for UI management. The Game Instance is also an appropriate choice for UI since it always exists and is where you could most likely implement things like Loading Screens which can use widgets as well. PlayerController, HUD, and GameInstance have everything you need to Create Widgets which require either a GameInstance or a PlayerController to create. I would choose one and stick with it for consistency stake.

For Managing General Assets such as DataTables, Particle Systems, Meshes, Textures, etc and be them things you need globally (say like a blood splatter particle effect, Item Database, or foot steps sound effects based on Physics assets) You can use either the GameInstance or UE4’s built in support for a GameSingleton which can be defined as any UObject. I personally use the GameSingleton for this. The GameSingleton can be assigned in ProjectSettings->Engine->General Settings under “Default Classes”.

For all these cases the idea is to define them in C++ and then subclass them in Blueprints allowing you to set the appropriate Properties you need for your game. This same principle holds in general for developing with UE4 and blending C++ and Blueprints. Using Dependency Inversion and Injection is the key which is a topic discussed later in this course and I was very happy to see it. This is why I am so adamant about NOT using FClassFinder, there are simply so many better ways to do it that won’t cause problems if some designer accidentally deletes your hard wired reference. It is truly the Ultimate UE4 sin, and plain bad programming practice to boot. And sadly I have seen it done far too often by young devs who I now suspect learned it from here.

2 Likes

Thanks for the detailed response. Shortly after posting I found that limitation of the GameMode being server side only and I went ahead and stuck it in the gameInstance like Sam had us doing. But I did go the route of making a BP to configure it.

I was looking to layout my assets like some guideline I found on the wiki before it was so rudely closed. Here is someone who took it over into their own website. https://www.gamecoderblog.com/en/unreal-engine4/ue4-recommended-folder-structure

Since you seem to have a pretty gosh darn good understanding of whats going on I have 2 unrelated questions, if you dont mind.

  1. How do you persist information? (Like player stats, gear, and other things like that)
  2. How do you do test automation in Unreal? Specifically to ensure client server interaction are working. Although TDD and Unit testing principles applied to UE4 would be much appreciated too.

I too have been very surprised of the removal of the offical wiki but it looks like there may be a new one. https://www.ue4community.wiki/

Unfortunatly Google indexes are still broken on it but you can search directly to find the topics you need, so that’s nice.

There are at least two methods for persisting information. The first is to use UE4’s built in SaveGame Object which is very easy to leverage. The second is to use UE4’s built in compression and binary writer.

https://www.ue4community.wiki/Legacy/Save_System,Read%26_Write_Any_Data_to_Compressed_Binary_Files

Retrieving that data can be tricky especially when trying to test multiplayer games as the default OnlineSubsystem gives each connection a random ID each time. Steam and other Online subsystems will have consistant IDs for each player. So making a sort of “Login” system before hand may be valuable for developing to allow you to simulate loading data from a server.

As for TDD I wish I could offer more but I have only dabbled a bit in using UE4’s built in Automation Tools for executing Unit Tests. I did hear of one game Epic developed game where they leveraged the Replay system to perform Game Play Unit testing, but I’ve not looked much into that. They have some documentation on Automation Tools available:

3 Likes

You can also find all the original content using the Wayback Machine

5 Likes

Privacy & Terms