when play demo in the editor, we should click the play button and reclick the viewport to get into the game. If in the Editor Preferences -> Level Editor -> Play -> CHECK the Game Gets Mouse Control OPTION. We dont need to reclick mouse.
Instead of change default loading map, get into the Editor Preference -> Loading & Saving -> CHECK Load Level at Startup TO Last Opened. This will help you when debuging cpp src and reopen the UE editor, load the test level instead default level.
When talking about story advantage game, Inventory system is simple, because there arent so many items to use or craft. While in RPG game, inventory system should be designed carefully. There are some features which I could imagine.
Pick up items. (food, wood, guns, bullet, quest items)
Use items. (food heal health, wood build house)
Drop items.
Craft items. (like minecraft)
As for the design of the UI, minecraft, PUBG, and Resident Evil are the typical typies.
I have searched several inventory system examples, and make a summary in this page.
I think the system could be improved, such as change the OnUse function from Items Class to InventoryComponent.
Inventory System realize the minecraft function.
This a free template which could be found in marketplace, called Inventory System. I think this system is inspired by minecraft, because the item can be picked up when player close to the item. It also has a hotbar which player can use items like eating apples. Crafting function and simple chest are also in the template. Oh, and the item can be dropped down just like minecraft cube rotating automatically. The system is almost complete.
Items are create in struct, inventory component controls the save/load of items, initial of user widget, operator function of items.
However, I found there are issues when the inventory is initialized.
I still remember when I began to learn blueprint from the official document, actor communication can be realized by interface, cast-to or event dispatchers. According to the example, it will happen when player goes into a collision trigger, make the on actor begin overlap event in the blueprint to communicate with the player.
However, in the cpp version, the event dispatchers is explained as the delegate. At that time, I didnt realize the example as there are too many parameters in the bind event. Now I found an use case with delegate which is also the unreal FPS project template. The delegate is used for the projectile. When the projectile hits other actors, it calls the custom event OnHit() . Lets take a look at the projectile source code.
classAProjectile:public AActor { UPROPERTY() USphereComponent* CollisionComp; //U indicate this is a collision component rather than a mesh UFUNCTION() voidOnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit); }
AProjectile::AProjectile() { CollisionComp->OnComponentHit.AddDynamic(this, &AFPSCPPProjectile::OnHit); //seems like an event calls the custom function in the blueprint }
//not all the parameters are used, but the "other actor" and "other component" are the things that the projectile is hitting. If it has the physics feature, add an impulse depend on the projectile. the code is similiar to the blueprint event. voidAProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) { if ((OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr) && OtherComp->IsSimulatingPhysics()) { OtherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation()); Destroy(); } }
Get into the engine source code.
The reason why USphereComponent reference can point to the OnComponentHit is that OnComponentHit is a signature of UPrimitiveComponent , its also considered as a delegate name.
1 2 3 4 5 6 7 8 9 10 11
//PrimitiveComponent.h
UCLASS() classENGINE_API UPrimitiveComponent : public USceneComponent, public INavRelevantInterface { UPROPERTY(BlueprintAssignable, Category="Collision") FComponentHitSignature OnComponentHit; //considered as an event which can be created in blueprint in the same name }
//long marco declaretion include parameters DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_FiveParams( FComponentHitSignature, UPrimitiveComponent, OnComponentHit, UPrimitiveComponent*, HitComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, FVector, NormalImpulse, const FHitResult&, Hit );
How to bind a function to the delegate
1 2 3
//Delegate.h #define AddDynamic( UserObject, FuncName ) //OnComponentHit.AddDynamic(this, &AFPSCPPProjectile::OnHit) on the top
How to confirm the parameter in the custom function.
1 2 3 4
//SparseDelegate.h //as we can see, in the projectile example, there are five parameters #define DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_FiveParams( SparseDelegateClass, OwningClass, DelegateName, Param1Type, Param1Name, Param2Type, Param2Name, Param3Type, Param3Name, Param4Type, Param4Name, Param5Type, Param5Name )
By the way, in the FPS blueprint template, event Hit is used to add impluse. The target of Hit is actor itself.
While in the cpp version, OnComponentHit is used which target is sphere collision. And it is actually including 5 parameters as we saw in the source code.
Above all is the summary of the projectile example.
Go back to the actor communication example.
I create the first actor including a box collision and a delegate.
The second actor bind the delegate.
When player overlap with the first box collision, delegate is active, and the second actor log a message.
//second actor classAReactActor { classATriggerActor* TriggerActorReference; voidGetIntoTrigger(){ UE_LOG(LogTemp, Log, TEXT("get into the trigger")); } }
Finally, built unreal engine source code successfully. First time clone source code from github was the main branch which is UE5, but I haven’t installed vs2022 yet, so the build tool cant generate vs sln file. Error message point out that the version of MSVC is less than 14.2. Anyway, I decide to turn to clone the UE 4.27 which is the most recent version before UE5. And I found a website to learn the operation about git. It explain the branch setting in local repository and how deal with the commit conflict when operating an open-source project with other developers. I have learned for hours, made a temp repository, but the command I really need below is from google, haha.
1
git clone -b 4.27 https//....
So in this part I want to record unreal source code which I found. As we know, the inheritance relationship in the whole is complex. It seems impossible to draw a graph or make a data table to show the inheritance. Fully understanding the engine seems to take a very long long time.
GameModeBase
When create an empty cpp project, the default mode is GameModeBase. As game play, we can control a camera move, so I guess the camera is generated by default. First, as DefaultPawnClass is declared as a TSubclassOf<APawn> type, and is defined = ADefaultPawn::StaticClass() .
The class ADefaultPawn is inheritance from class Pawn . According to the doc,
DefaultPawns are simple pawns that can fly around the world.
It’s right, the flying camera. Let’s see the cpp source code.
1 2 3 4 5 6 7 8 9 10 11 12
//DefaultPawn.cpp ADefaultPawn::ADefaultPawn(const FObjectInitializer& ObjectInitializer): Super(ObjectInitializer){ //construct function initalize the collision component,movement component //choose a spheremesh as the default subobject } voidInitializeDefaultPawnInputBindings(){ //initialize move forward, right,up, turn and lookup } voidADefaultPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent){ //bind axis } voidADefaultPawn::MoveRight(float Val){} //same as move forward, move up and so on
But there’s nothing to do with camera. Of course, camera is auto setup in the class PlayerController .
The reason why I made this title is inspired from a video about the dark side in the self-study by a math youtuber. And also this is my first time to write blog in English. Of course I have been writing and reading articles in university, but they are shit and I don’t care what they are saying. Back to the Title, I found that I have been learning unreal engine and cpp for over 2 months, and I still can’t make my own game demo. Maybe I didn’t work hard or I realize that I get the wrong direction from that video, sorry I forgot the youtuber name. The topic of that video is that self-study is actually a difficult thing for every person, and that’s why there are so many colleges in the world, right?
Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.