Crash when accessing a custom C++ class
I'm trying to make a talent tree system on C++ based on two classes, a Talent class to store simple data and a TalentTree class to store, manage and access a list of talents. Since they both share the same purpose, both ae in the same source files (TalentTree.h/.cpp) Everything seems to be working as intended, but when I try to access a talent from the list in my GameMode class it causes UE4 to crash.
I may not be initializing everything as needed, but I can't find any answers on why it doesn't work. I've tried changing the classes that inherit from it a few times, but I started inheriting both from UObject. Here's my code, with :
Declaring your object constructor is pretty straightforward. You'll want it to be public, and it's just the class name with no arguments:
In the source you can call the constructor of parent objects via the initialization list, as well as set default values for you variables.
Some older tutorials and example code include the FObjectInitializer argument, but that's no longer passed in; and most (if not all) of its functionality is natively accessible within the constructor method.
If you've declared the variable in your game mode header, remember you still need to manually spawn the actor.
You can do it in your constructor using CreateDefaultSubobject or CreateOptionalDefaultSubobject. If the object you're creating is a UObject you can also use the standard NewObject method; but within the game mode constructor you can't use SpawnActor because the world doesn't yet exist.
Outside your constructor you should use NewObject or SpawnActor, depending on which type of object you're building.
Each of them is templated, and has a number of different forms.
For your ATalentTree you'll want to declare and instantiate it like this:
answered Oct 22 '16 at 02:50 AM
When and where are you calling FindByID? It may be that you are trying to access FindByID before BeginPlay is called.
You can place your initialization in the constructor:
In any event, it's probably a good idea to do a check before accessing the array members:
answered Oct 21 '16 at 04:34 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here