<Reprint> UE4 Actor class C++ simple attempt

I used UE4 for a while in the beginning and found that using the blueprint system really didn't suit my style. Because I have always used Unity3D for program development. But after I came into contact with UE4's C++, I felt quite good. It seems that there are no relevant books and teaching materials in China, so you can only go to the reference documents on the official website.

https://docs.unrealengine.com/latest/INT/Programming/index.html

My general impression of UE4 and Unity3D:

1. UE4 consumes more performance than Unity. Basically, UE4 games cannot be opened
. 2. The blueprint of UE is suitable for artists to see. Programmers should be honest and practical with C++
. 3. Unity can save C# code every time it is written. Running, and UE4 can only be used by manually recompiling and restarting every time the code is written. This is very dependent on computers with strong performance, especially CPU, solid-state, solid-state
4. Unity is based on component programming, each in the view window. Gameobject inherits from MomoBehavior and has system callbacks such as OnGUI, Update, and Start for you to use. However, UE4 can only manually inherit the virtual functions Tick and BeginPlak by itself, and rewrite it. It gives me the feeling that there are 2 good cars, one is automatic and the other is manual.

Create a new C++ basic project for Unreal 4

Paste_Image.png

 

After the new

Paste_Image.png

 

Create a new Actor class

Paste_Image.png

 

add code to it

#pragma once
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS ()
class EMPTY_API AMyActor : public AActor
{
    GENERATED_BODY()

public :
     // Running time 
    float RunningTime;
     // Constructor 
    AMyActor();
     // The callback executed at the beginning is equivalent to void Start() in Unity 
    virtual  void BeginPlay() override ;

    // The callback executed in each frame is equivalent to void Update() in Unity 
    virtual  void Tick( float DeltaSeconds ) override ;
};
#include "empty.h"
#include "MyActor.h"
#include "iostream"
//构造器
AMyActor::AMyActor()
{
    // Set Tick() to be executed once per frame, set to false if not needed 
    PrimaryActorTick.bCanEverTick = true ;
}
// Callback executed at the beginning 
void AMyActor::BeginPlay()
{
    Super::BeginPlay();

    std::cout << " Start execution " ;
}
// Callback executed every frame 
void AMyActor::Tick( float DeltaTime )
{
    std::cout << " execute every frame " ;
    Super :: Tick (DeltaTime);
    // Get the Actor's location and save it to the NewLocation variable 
    FVector NewLocation = GetActorLocation();
     // Get the time difference 
    float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
     // Changed Height, incremented by 20.0f each time 
    NewLocation.Z += DeltaHeight * 20.0f ;
     // Change the value of running time 
    RunningTime += DeltaTime;
     // Set the current Actor's position 
    SetActorLocation(NewLocation);
}
 

Finally, add the code MyActor to a cone, press Ctrl+F5 to compile and you can see it shaking up and down

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325130608&siteId=291194637