- Mastering Unreal Engine 4.X
- Muhammad A.Moniem
- 1496字
- 2021-07-14 11:20:39
The Gladiator header (.h) file
Now let's jump back to the header file again, and let's start adding some more functions and variables to it so we can start building gameplay logic for the character and make something that fits our target.
Considering that a class based on FTableRowBase
and called AGameDataTables
will be used later to read the gameplay data from Excel tables; here is the header file code I ended up with.
To make it easier to understand the code, I would like to breakdown all the variable components and methods into a set of chunks; that way it will be very easy to understand them.
Everything starts with the includes, just like any form of C++ coding you are used to making, including the header files that are going to be used or referenced and must be done at the top of the code.
#pragma once #include "GameDataTables.h" #include "GameFramework/Character.h" #include "Gladiator.generated.h"
Defining the class itself is essentially a step directly after the include
statements.
UCLASS(config = Game) class AGladiator : public ACharacter { GENERATED_BODY()
BeginPlay
: This is the virtual void of the override BeginPlay
from the Character
class base and this one will be called once the game is started.
virtual void BeginPlay() override;
CameraBoom
: This is a USpringArmComponent
that will be added to the character blueprint that is based on that class. This component will be used to control the camera.
//Camera boom positioning the camera behind the character UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) class USpringArmComponent* CameraBoom;
FollowCamera
: This is the camera itself that will be viewing the game and following the player. This one will also be added to the blueprints.
//Follow camera UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) class UCameraComponent* FollowCamera;
EffectSprite
: This is a Paper2D
sprite component (Paper 2D is the main 2D framework for Unreal Engine 4.x). There are lots of ways we can use this to achieve an on-screen draw texture, but this one is the easiest and most flexible. I managed to add a sprite component that is very close to the camera, and then we can use it to draw whatever effect we need.
//The sprite used to draw effect, better and more contrallable than using the HUD or Textures UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Effects, meta = (AllowPrivateAccess = "true")) class UPaperSpriteComponent* EffectSprite;
AGladiator
: This is the constructor, and as mentioned earlier, it is used to build the object in edit mode.
public: AGladiator();
BaseTurnRate
: This is a float
variable in degrees to control the camera turn rate.
//Base turn rate, in deg/sec. Other scaling may affect final turn rate. UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera) float BaseTurnRate;
BaseLookUpRate
: Another float
variable to control the camera, but this time it's for lookups. This one is also in degrees.
//Base look up/down rate, in deg/sec. Other scaling may affect final rate. UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera) float BaseLookUpRate;
JumpingVelocity
: This is a float
variable to determine the jump velocity.
//Base Jump velocity UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Player Attributes") float jumppingVelocity;
IsStillAlive
: This is a Boolean variable to tell us what the current state of the layer is. It is a very important variable, as most of player behavior and inputs will be based on it.
//Is the player dead or not UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Player Attributes") bool IsStillAlive;
IsAttacking
: Another Boolean variable to report if the player is attacking now or not. It is important for animations.
//is the player attacking right now? UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Player Attributes") bool IsAttacking;
WeaponIndex
: This is an integer to determine the current active weapon index. The player could have several weapons; to be able to load the weapon's data, it is a good idea to give each weapon its own index.
//the index of the current active weapon. UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Player Attributes") int32 WeaponIndex;
IsControlable
: Sometimes the player is not dead, but you also need to take the control out of his hands, maybe because the character is carrying out an attack, or maybe because the player paused the game. So this is a bool
variable meant to tell us if the player is in control now or not.
//To be able to disable the player during cutscenes, menus, death....etc UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Player Attributes") bool IsControlable;
TablesInstance
: A datatable
variable to hold the active instance of the game tables. It is just here to load some data.
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Game DataTables") AGameDataTables* TablesInstance;
GetIsStillAlive
: A getter method to return the value of the IsStillAlive
Boolean variable.
//Return if the player dead or alive UFUNCTION(BlueprintCallable, Category = "Player Attributes") bool GetIsStillAlive() const { return IsStillAlive; }
OnSetPlayerController
: A method that takes a parameter of true
or false
, and uses it to set the status of the player controller. So it is here we take the control from the player, or give it to him at a certain moment.
//Enable or disable inputs UFUNCTION(BlueprintCallable, Category = "Player Attributes") void OnSetPlayerController(bool status);
OnChangeHealthByAmount
: A method that takes a float
value, and reduces the total player health using it. It is usually used when the player gets damaged.
//the attack effect on health UFUNCTION(BlueprintCallable, Category = "Player Attributes") void OnChangeHealthByAmount(float usedAmount);
OnGetHealthAmount
: This is a getter
function that returns the TotalHealth
value of the player as a float
value.
UFUNCTION(BlueprintCallable, Category = "Player Attributes") float OnGetHealthAmount() const {return TotalHealth;}
OnPostAttack
: A method that holds some procedurals after the player has done an attack.
UFUNCTION(BlueprintCallable, Category = "Player Actions") void OnPostAttack();
GetCameraBoom
: A getter
method to return the CameraBoom
component variable.
//Returns CameraBoom subobject FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }
GetFollowCamera
: Another getter
method to return the FollowCamera
component variable.
//Returns FollowCamera subobject FORCEINLINE class UCameraComponent* GetFollowCamera() const { return FollowCamera; }
MoveForward
: A method that holds the code responsible for player movement to the forward and backward. Notice that it is a BlueprintCallable
in order to be able to use it from the Gladiator
class blueprint instances.
protected: UFUNCTION(BlueprintCallable, Category = "Player Actions") void MoveForward(float Value);
MoveRight
: A method that holds the code responsible for player movement to the left and right.
UFUNCTION(BlueprintCallable, Category = "Player Actions") void MoveRight(float Value);
Jump
: A method that is responsible for applying the jump action to the character based on the base character class.
UFUNCTION(BlueprintCallable, Category = "Player Actions") void Jump();
StopJumping
: A method that is responsible for stopping the jump, and resuming the idle/run animation.
UFUNCTION(BlueprintCallable, Category = "Player Actions") void StopJumping();
OnAttack
: A method that is responsible for attacking.
UFUNCTION(BlueprintCallable, Category = "Player Actions") void OnAttack();
OnChangeWeapon
: A method that is responsible for switching between weapons.
UFUNCTION(BlueprintCallable, Category = "Player Actions") void OnChangeWeapon();
TurnAtRate
: A method that is responsible for applying turns to the following camera.
//Called via input to turn at a given rate. void TurnAtRate(float Rate);
LookUpAtRate
: A method that is responsible for applying the camera look-up rate to the follow camera.
//Called via input to turn look up/down at a given rate. void LookUpAtRate(float Rate);
TotalHealth
: A float
variable that holds the player's total health (the current health), as at any moment the player's health gets reduced, that will be the final value. Some people like the approach of creating two variables:
TotalHealth
: This is the base and default healthCurrentHealth
: This represents the current health status
Feel free to use any approach you want, but I like to make one health variable for such a small game.
//The health of the enemy UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player Attributes") float TotalHealth;
AttackRange
: A float
variable that holds the current weapon attacking range.
//The range for the enemy attack UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player Attributes") float AttackRange;
SetupPlayerInputComponent
: Another override of a virtual function from the base class, and this one is called to set up and map the key inputs (the ones we made in Chapter 1, Preparing for a Big Project) to the class.
protected: //APawn interface virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override; //End of APawn interface };
As you can see, there are some stylings that have been applied to the code. It looks weird, strange, and unique at first glance, but once you get used to writing Unreal code you'll get very familiar with these things.
The styling I used involved some access specifiers and macros, which include:
Public
: Any variables or methods listed underneath this access specifier will be publicPrivate
: Any variable or method listed underneath this access specifier will be privateUPROPERTY
: This is used before a variable and is a macro to define attributes for the variable, such as how and where it should or could be changed or seen, and its category to make sure it is listed in the correct placeUFUNCTION
: This is used only before a function and is a macro to define attributes for a function, such as how and where it should or could be called, and its category to make sure it is listed in the correct place
- Mastering Visual Studio 2017
- Reporting with Visual Studio and Crystal Reports
- 跟小海龜學(xué)Python
- 面向STEM的Scratch創(chuàng)新課程
- x86匯編語(yǔ)言:從實(shí)模式到保護(hù)模式(第2版)
- Raspberry Pi 2 Server Essentials
- 零基礎(chǔ)學(xué)Java程序設(shè)計(jì)
- SQL Server 2016數(shù)據(jù)庫(kù)應(yīng)用與開(kāi)發(fā)習(xí)題解答與上機(jī)指導(dǎo)
- Modern JavaScript Applications
- JavaScript 程序設(shè)計(jì)案例教程
- C++新經(jīng)典
- jQuery炫酷應(yīng)用實(shí)例集錦
- 基于SpringBoot實(shí)現(xiàn):Java分布式中間件開(kāi)發(fā)入門與實(shí)戰(zhàn)
- C#程序設(shè)計(jì)(項(xiàng)目教學(xué)版)
- JavaScript+jQuery網(wǎng)頁(yè)特效設(shè)計(jì)任務(wù)驅(qū)動(dòng)教程