First I should preface, I'm not stopping learning blueprints or using blueprints in the projects in unreal, I simply see that as good as blueprints are, they're also limited and can be tricky to solve issues. Now this doesn't mean C++ is easy, no its actually harder but I believe this will enable me to learn more and have more fundamental knowledge of programming in general so that if I do come across a problem in blueprints or C++, I can identify and solve the issue much quicker.
First I created a C++ game mode base by going to tool new C++ class and selecting game mode. Then I built the C++ code By holding ctrl shift B. And I saved the level as base map.
A game state is essentially the raw data, so In a gun system, you have the ammo, the ammo stored, the ammo taken away etc. the game states are what hold this data. The game mode processes all the logic stored in the game states and also act as sort of rules, how many players are allowed in the game, have we reached the maximum amount of players, should the game be paused, how should we transition between levels etc.
Header files and Source files:
Header files (.h) are where you define the things which we are going to have. Source files (.cpp) is where the actual code which does whatever we want it to do is located. The reason there are both header and source files is so the compiler can search all the header files first and know what definitions are what before it actually runs the code which does things.
Constructors and Deconstructors as well as other things like pointers:
Classes act as a template from which you create individual object instances, for example in UE you can create an instance with something like (CreateNewObject() or SpawnNewActor()). Constructors are functions which automatically run when instances are created so the instance can set itself up, deconstructors are functions which automatically run when an instance is destroyed so the class can shut itself down.
In unreal engine, object instances generally get cleaned up and deleted when they no longer hold on to any references of them (a pointer). Pointers are variables which store a memory address of other variables or memory items, they look like this (Variable*) the Asterix is the pointer and you can get pointers later by using the &symbol (&Variable). Marking the pointer with UPROPERTY(); tells UE to leave it alone.
In UE generally constructors are used for initializing a Class Default Object or CDO, a template object which holds the default values and behaviours for that class. This CDO is used to clone copies from during play and runs when the code the class is in is loaded, or when the engine starts or maybe in live coding when code is changed. Constructors are also used for setting initial property values and adding contained items (sub objects) these initializations apply to created instances (Using CreateNewObject() for example).
There is a function called BeginPlay() which is provided, when you Override it, you can run code when each instance is created and ready to use. There is also a function called BeginDestroy() when you Override this, the code runs when an instance is cleaned up. Important to note Clean up happens when the engine feels like doing it not necessarily at the moment the instance in no longer used.
For naming conventions each word in a type name or variable should start with a capital letter, UE also used US grammar and spelling. Type names are distinguished from regular variables with an extra prefix, some examples include (T, U, A, S, I, E, b) most other classes use F and some use other letters. Prefix meanings: (T) is for template classes, (U) is for classes which inherit from (UObject), (A) is for classes which inherit from (AActor), (S) is for classes which inherit from (SWidget), (I) is for classes which are abstract interfaces, (E) is for Enums, and (b) is for Booleans.
In blueprint, classes can be one of four container types, Single, Array, Set and Map. TArray is a one dimensional array of T (templates) it is dynamically resizable and can be marked with UPROPERTY as it is implemented using unreal engine. TMap, stores objects by Key and Value, (Key is some form of ID and Value is the type of object stored). TSet, storage of usually unique items which are keyed by the stored objects hash value.
UE C++ uses macros
First, I set up a new C++ class pawn. In the cpp folder, there is a construction, which sets the can ever tick to true. Then a begin play, which does the same as blueprint begin play, a tick section which calls the code every frame and finally player input component, which is where you can set up all the players inputs. Then on the new default pawn h, I added the Camera component to include it (this way I can add a camera to the pawn), then under begin play override, I simply call the U Camera Component and called it Camera. Then back in the cpp file, to the construction script I added a new component Camera equal to the sub component, U Camera Component which is then called camera.
I then found out that in order for any component to work, there needs to be a root component for it to be attached to, which is done above. I also added UPROPERTY Edit anywhere so the properties of the camera can be edited when the pawn is placed or spawned etc.
In the game mode base h I included the new default pawn, so we can then set it as the default pawn class.
Here I set up a mesh by first including static mesh components, and then setting the variable as player mesh. Then In the default pawn header, I created the sub object for the static mesh component and attached it to the root component (same as camera). Then I setup some camera settings like the FOV, and its relative location (50 on the z and -100 on the x, so sort of like 3rd person) Then I attached the camera with the parent as the player mesh which just specifies where offset of the camera is relative to.
Then I imported a simple box model, and applied a texture. Then in order to apply this mesh to the pawn, I created a blueprint class based on the C++ code (right click to open actions window). This way I cant use it like a normal blueprint and select the static mesh I want to apply.
Then, to implement the blueprint I did the same for the game mode and made it a blueprint. Then set the default pawn class to the blueprint pawn, and lastly I added the blueprint game mode as the default game mode.
What it currently looks like. I then offset the camera a bit more for a full third person view.
Here I setup the axis mappings for forward, backward, right and left.