[C++] Class initialization creates a breakpoint for UE4

For my assignment that is due in three weeks as of writing (sadly, I can only see my tutors once a week, and we’re away in two weeks), I am trying to write C++ to develop my understanding of OOP. I admit that I haven’t really had or used enough time to develop my understanding of C++, but this has been even less so with how Unreal Engine 4 does it due to my course’s initial focus on Unity3D and C# (which I could also choose to work with, but even though it’s been easier in my experience I’d rather challenge myself with UE4 and C++).

This link provides the code I’ve worked on so far.

I am trying to instantiate a collective health class in a pawn. This collective health class will contain a TArray of health systems, and each health system will contain functions, variables and a cube mesh as part of its logic. The collective health class has its own set of functions and variables to deal with a health system.

I used the following code:

void APawnDraft::BeginPlay()
{
    Super::BeginPlay();
    ACollectiveHealthSysDraft chd(6, 125, 0, this->GetActorLocation(), this->GetActorRotation()); // class declaration and instantiation: https://www.ibm.com/support/knowledgecenter/SSGH3R_13.1.3/com.ibm.xlcpp1313.aix.doc/language_ref/cplr387.html
    //ACollectiveHealthSysDraft chd(;

}

and when I click “Play” in the editor, Visual Studio triggers a breakpoint. Placing the asterisk after ACollectiveHealthSysDraft to render it as a pointer variable returns this error:

'initializing': cannot convert from 'initializer list' to 'ACollectiveHealthSysDraft *'

I suspect that the error might have something to do with pointers, as in here, but I cannot be entirely sure.

On the other hand, I’ve had more luck with using the following in void APawnDraft::BeginPlay() and void ACollectiveHealthSysDraft::BeginPlay():

Super::BeginPlay();
//ACollectiveHealthSysDraft chd(6, 125, 0, this->GetActorLocation(), this->GetActorRotation()); // class declaration and instantiation: https://www.ibm.com/support/knowledgecenter/SSGH3R_13.1.3/com.ibm.xlcpp1313.aix.doc/language_ref/cplr387.html
FActorSpawnParameters SpawnInfo;
()->SpawnActor<ACollectiveHealthSysDraft>(this->GetActorLocation(), this->GetActorRotation(), SpawnInfo);
//ACollectiveHealthSysDraft* chd(;

Super::BeginPlay();
for (int i = 0; i < 6; i++)
{
    FActorSpawnParameters SpawnInfo;
    ()->SpawnActor<AHealthDraft>(FVector(0, 0, 0), FRotator(0, 0, 0), SpawnInfo);
    //this->cubeCompActors.Emplace(()->SpawnActor<ATestCubeComponent>(loc, rot, SpawnInfo));
    //Cast<ATestCubeComponent>(cubeCompActors[i]);
}

However, I am reluctant to use ()->SpawnActor<T>(), because I cannot really use different method signatures with it (thus leaving me being able to spawn class instances with the default constructor), and right now, this wouldn’t entail much of a link between classes (I think I could remedy this by assigning variables later on).

I’m wondering if there’s anything you can do to help me here.

You seem to relay to much on raw C++ tutorials, rember that you cooperate here with a ready huge code base that UE4 is and it has a lot of things already esablished. Once of them which you see yourself now isobject creation method. Keep in mind that by making C++ project in UE4, you create a dll module, one of meny that enigne already have… and yes this essentially mean you latterly extending the engine by doing so and you code becomes part of it, crash in your module means crash of entire engine. So C++ in UE4 is not some special script system as lot of beginners seems to think, it a normal C++ injected in to already huge code base that UE4 is and you need to follow rules that it imposes. Think of it like you would have access to C++ code of Unity and program a game by adding code to it, thats how C++ programming in UE4 essentially is… but dont be afraid of, this is what makes it programming in powerful and let you do a lot of things that Unity would never let you do.

But back to issue at hand. UObject (which AActor is also one) from is special class which engine tries to track it’s existence and life cycle and control it, thats why you can not use normal constructors with them. One of the reason why that is is because C++ compiels direclly in to CPU machine code, everything you write at the end is converted to instructions and varables turn in to memory addresses or memory address offsets (in class/struct structure). There no real deal run time in C++ (what you install C++ runtime is really a C++ standard library which just provides basic functions defined in C++ specification, but it actually optional to use… which you will learn below), so rest of the code can not track what you doing, that you making new object. Thats why in UE4 you need to use special construction functions to make UObjects so engine actually do proper memory management and track the object state of being, to provide helpful features that you will find useful (like object and actor iteration for example), that why you need to use NewObject<>() for them.

You also can not statically allocate UObject as you attempted, as those can not be deleted in runtime or without deleting the host class and engine needs to have option to do so. Becuase of that you can only have pointers of UObjects. You also should use UPROPERTY() before UObject pointer because UObjects managment use reflection system to check what is referencing the object, it also automatically set null pointer on property if set object gets deleted.

AActor is even more special, thats why it has own special prefix A instead of U despite of it being UObject. Most basic function of AActor is to be tied to the world instance, which manifest it self as UWorld object and world instance also controls actors life cycle. Thats why insted of NewObject you need to use ()->SpawnActor<T>() so world instance can properly setup actor in to the worlds. UWorld works as a host of actors, if UWorld dies all actors tied to it goes with it and this always happens when you restart the level or change the map, this is something that even blueprint programmers need to know.

So what you can do? Do exacly what UE4 already does with AActor, make your own construction function which could be even static function (but then you would need to pass UWorld* as argument), that would spawn actor and set up varbales in it that you need. Also note that you don’t need to make those properties set in C++, you can do so in property editor, whish is what lot UE4 code does.

But you don’t really usually do this this way in UE4. You will most likely end up working with people who are allergic to even looks at C++ code or even installing VS on there machine (and trust me there lot of post-Unity C# programmers which have exact same phobia ;p), you will need to make able to edit values via blueprints or any other property editors that engine provides for those people. Imagine that you making component in Unity with properties for people to set, in UE4 actors (which are called object is Unity) are not soulless bodies like in Unity, they also contain code and can have those properties too and you can edit them by selecting the actor in level editor. Note that All actors you place in UE4 without any C++ programming was made exact same way you are doing and oyu actors should behave in similar fashion and in most cases (at least in case of actors) the end user for you, will be blueprint programmer or artist, not player and you need to make actors friendly to work with in editor. So instead of jumping in to constructor functions, think if you can solve this using normal propery editing

This brings me to problem of your design chooses. First of all you don’t need to create “collective actor”, you can do the tracking inside other objects that are created anyway, for example AGameMode (main game rules control class) which exists exactly for this kind stuff, even if you make management actor you will end up spawning it in your AGameMode either way.

Secoundly, the what you really trying to do… isn’t it remind you of something you already using from the engine? … something that you can attach to actor… something like… UStaticMeshComponent for example :stuck_out_tongue: Yes, you have option to create components that yo can attach to actor, you have a UActorComponent which is non physical and just attach code to actor and USceneComponent which is physical (by that i mean it has positional data) which UStaticMeshComponent is. System you trying to make screams that it wants to be a component, so you might want ot learn how to make one

Fact that you doing so much misuse everything make me believe you have low knowledge about game play framework of UE4, so i think before jumping in C++ you should learn that first

It also impotent to understand that blueprints are classes same as C++ classes and you use them same way just in different language, in most cases they are translatable to each together (in fact UE4 provides Nativisation feature which in game packaging attempts to convert blueprint class in to C++ class). You can see that by looking on Class Viewer (Window->Development Tools->Class Viewer) where yo can see blueprints becoming part of class tree (btw you can use Class Viewer same as Content Browser, yo can drag actor classes and place them in level editor… thats actually how you place things on level before UE4)

…thats what im thinking, best things for you would be make a step back and be first try to do something with Blueprints, this will help you learn how UE4 actually works, everything (well mostly, for example you dont have delays in C++) you do in blueprints can be done in C++ and generally should be done some way, everything you place there can be done in C++, like components (which btw can be also made in Blueprint). Fun fact assets are also UObject, and what really asset are are savable UObjects so observe behavior of them too. Blueprint will help you avoid misleading non-UE4 C++ tutorials and let you concrete your view on UE4. Because you clearly lacking on knowledge how to actually make game using UE4 by errors you making. Try viewing some UE4 tutorials how to do thigns in blueprint, you probably learn a lot more about UE4 then looking on raw C++ tutorial even UE4’s C++ tutorials.

Also in made series of streams trying to explain C++ from core and translate that to UE4, you might take a look:

https:///videos?view=0&sort=dd&shelf_id=0

Problem is… you are different case, as this primerly targets blueprint only users, while you lacking typical knowledge of such a person and it mainly concentrate on how C++ work at it’s core and don’t even talk about UE4 until parts later. Also those streams are kind of hiatus as making them takes a lot of time and kind of lost motivation on making them.

Oh and one more thing, you should avoid using C++ standard library in UE4, thats because UE4 got wrapper code for most of them or it’s own implementation (as i mention before C++ standard library is optional to use and UE4 is prime example of that). So you should not use functions you see in raw C++ tutorials, because oyu will probably never use them in UE4, types use in there are not compatible with types used in UE4 and oyu only use standard library to communicate with liberies that use them and in that case you will need to do type conversion or create some kind of wrapper. So learn using UE4 APIs exclusively, if you use something else you should question if there no UE4 API equivalent of it. I saying this because i notice this in your code which in UE4 code rarely appers:

#include <iostream>
#include <string>
#include <cstdlib>

Main reason why UE4 does that is to make sure that code you make in UE4 will work on any platform and build with any compiler that UE4 supports, it is also to engine make aware of what your code is doing (same reason why yoiu need to use as NewObject ;p), for UE4 to have full control on things happening and as well for your code to use engine level optimizations which standard library wont provide.

So if your goal of using UE4 is to learn standard raw C++, not to make a game in it, then UE4 might not good place to do so.

But since you mention TArray instead of some std::vector, i suspect you might already aware of that :stuck_out_tongue:

2 Likes