本篇文章是我學習UE4的筆記
學習地址如下:
http://www.sikiedu.com/my/course/518
參考地址如下:
因本人才疏學淺,如有錯誤之處,還請見諒
我們還是要開啟程式碼
修改我們設定的藍圖類的程式碼
.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class SIKI_PROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
//Visble就是設定可見的.
UPROPERTY(VisibleAnywhere,Category="My Actor Components")
UStaticMeshComponent* StaticMesh;
//US就是剛剛設定的那個元件,我們這裡設定了一個這個元件的指標.
//Instance就是範例的意思,Only就是隻允許.就是隻允許在範例上
UPROPERTY(EditInstanceOnly, Category = "My Actor Properties | Vector")
FVector InitLocation;
protected:
//開始的重寫函數
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
//每一幀呼叫的函數
virtual void Tick(float DeltaTime) override;
};
.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
//Tick是不是每幀都呼叫
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//建立了一個物件來呼叫我們設定的指標.
//Create的那個函數是一個模板函數和vector差不多
StaticMesh
= CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
InitLocation = FVector(0.0f);
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
//Fvector就是一個空間的向量.如果你想仔細檢視的話,就按下F12就OK了
///0.0f就是吧這個0.0分別複製給x,y,z
// 這樣的話,不管開始的地方,在哪裡.開始遊戲之後,都會回到0.0f
SetActorLocation(InitLocation);
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
//通過Super呼叫了父類別裡面的方法
Super::Tick(DeltaTime);
}
我們找到藍圖列建立的範例.
範例上,是有我們建立的分類的屬性的.
但是我們開啟藍圖類就沒有.
因為我們設定的關鍵字就只在範例上生效.
EditDefaultsOnly 就是隻能在編輯頁面,就是材質的編輯頁面裡面.設定.範例不行
VisibleInstanceOnly 只能設定視覺化
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class SIKI_PROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
//Visble就是設定可見的.
UPROPERTY(VisibleAnywhere,Category="My Actor Components")
UStaticMeshComponent* StaticMesh;
//US就是剛剛設定的那個元件,我們這裡設定了一個這個元件的指標.
//Instance就是範例的意思,Only就是隻允許.就是隻允許在範例上
UPROPERTY(EditInstanceOnly, Category = "My Actor Properties | Vector")
FVector InitLocation;
UPROPERTY(VisibleInstanceOnly, Category = "My Actor Properties | Vector")
FVector PlacedLocation; //就是一個放置的地方
UPROPERTY(EditDefaultsOnly,Category="My Actor Properties|Vector ")
bool bGotoInitLocation; //是否要去我們設定的位置
protected:
//開始的重寫函數
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
//每一幀呼叫的函數
virtual void Tick(float DeltaTime) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
//Tick是不是每幀都呼叫
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//建立了一個物件來呼叫我們設定的指標.
//Create的那個函數是一個模板函數和vector差不多
StaticMesh
= CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
InitLocation = FVector(0.0f);
PlacedLocation = FVector(0.0f);
bGotoInitLocation = false; //預設讓這個小球不改變位置
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
//Fvector就是一個空間的向量.如果你想仔細檢視的話,就按下F12就OK了
///0.0f就是吧這個0.0分別複製給x,y,z
// 這樣的話,不管開始的地方,在哪裡.開始遊戲之後,都會回到0.0f
//獲取到PlaceLocation裡面.就是儲存下一開始的值.
PlacedLocation = GetActorLocation();
if (bGotoInitLocation)
{
//只有當bGotoInitLocation為真的時候,我們才改變InitLocation
//的位置.
SetActorLocation(InitLocation);
}
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
//通過Super呼叫了父類別裡面的方法
Super::Tick(DeltaTime);
}
然後再VS裡面的設定
這個就是那個邏輯的語句.
然後我們在加幾個控制符.
今天學的比昨天明白了一點.簡單的總結一下控制符.
VisibleAnywhere 字面意思,就是不管在範例還是在原型中都可以看到這個屬性
EditInstanceOnly 字面意思, Instance 範例 Edit編輯.
就是隻能在範例中編輯.
VisibleInstanceOnly 只能在範例中可以看到這個屬性
VisibleDefaultsOnly 只能在原型中看到這個屬性
EditAnywhere 最常用的屬性,就是不管在範例還是在原型中,都可以修改這個屬性.
//Visble就是設定可見的.
UPROPERTY(VisibleAnywhere,Category="My Actor Components")
UStaticMeshComponent* StaticMesh;
//US就是剛剛設定的那個元件,我們這裡設定了一個這個元件的指標.
//Instance就是範例的意思,Only就是隻允許.就是隻允許在範例上
UPROPERTY(EditInstanceOnly, Category = "My Actor Properties | Vector")
FVector InitLocation;
UPROPERTY(VisibleInstanceOnly, Category = "My Actor Properties | Vector")
FVector PlacedLocation; //就是一個放置的地方
UPROPERTY(EditDefaultsOnly,Category="My Actor Properties|Vector ")
bool bGotoInitLocation; //是否要去我們設定的位置
UPROPERTY(VisibleDefaultsOnly, Category = "My Actor Properties|Vector ")
FVector WorldOrigin; //就是用來做參考的世界原點座標系
//EditAnywhere 就是不管在範例還是原型都可以修改
UPROPERTY(EditAnywhere, Category = "My Actor Properties|Vector ")
FVector TickLocationOffset;
UPROPERTY(EditAnywhere, Category = "My Actor Properties|Vector ")
bool bShouldMove;
然後我們設定不移動.設定一個TickLoationOffset 的值.
在點選開始,元件就會自己動起來樂.
完整的程式碼如下:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class SIKI_PROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
//Visble就是設定可見的.
UPROPERTY(VisibleAnywhere,Category="My Actor Components")
UStaticMeshComponent* StaticMesh;
//US就是剛剛設定的那個元件,我們這裡設定了一個這個元件的指標.
//Instance就是範例的意思,Only就是隻允許.就是隻允許在範例上
UPROPERTY(EditInstanceOnly, Category = "My Actor Properties | Vector")
FVector InitLocation;
UPROPERTY(VisibleInstanceOnly, Category = "My Actor Properties | Vector")
FVector PlacedLocation; //就是一個放置的地方
UPROPERTY(EditDefaultsOnly,Category="My Actor Properties|Vector ")
bool bGotoInitLocation; //是否要去我們設定的位置
UPROPERTY(VisibleDefaultsOnly, Category = "My Actor Properties|Vector ")
FVector WorldOrigin; //就是用來做參考的世界原點座標系
//EditAnywhere 就是不管在範例還是原型都可以修改
UPROPERTY(EditAnywhere, Category = "My Actor Properties|Vector ")
FVector TickLocationOffset;
UPROPERTY(EditAnywhere, Category = "My Actor Properties|Vector ")
bool bShouldMove;
protected:
//開始的重寫函數
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
//每一幀呼叫的函數
virtual void Tick(float DeltaTime) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
//Tick是不是每幀都呼叫
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//建立了一個物件來呼叫我們設定的指標.
//Create的那個函數是一個模板函數和vector差不多
StaticMesh
= CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
InitLocation = FVector(0.0f);
PlacedLocation = FVector(0.0f);
bGotoInitLocation = false; //預設讓這個小球不改變位置
WorldOrigin = FVector(0.0f);
TickLocationOffset = FVector(0.0f);
bShouldMove = false;
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
//Fvector就是一個空間的向量.如果你想仔細檢視的話,就按下F12就OK了
///0.0f就是吧這個0.0分別複製給x,y,z
// 這樣的話,不管開始的地方,在哪裡.開始遊戲之後,都會回到0.0f
//獲取到PlaceLocation裡面.就是儲存下一開始的值.
PlacedLocation = GetActorLocation();
if (bGotoInitLocation)
{
//只有當bGotoInitLocation為真的時候,我們才改變InitLocation
//的位置.
SetActorLocation(InitLocation);
}
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
//通過Super呼叫了父類別裡面的方法
Super::Tick(DeltaTime);
if (bShouldMove) {
AddActorLocalOffset(TickLocationOffset);
}
}
還有一點.就是指標是不可以亂編輯的.
這點懂的都懂(好吧,我並不懂)-只是直到指標不可以亂編輯
然後就是.我們還可以手動的給TickLocationOffset限制範圍.
//EditAnywhere 就是不管在範例還是原型都可以修改
//ClampMax就是手動輸入最大值的意思.ClampMax就是手動輸入最小值的意思.
//UI MAx和Min 就是通過UI改變的最大最小值.
UPROPERTY(EditAnywhere, Category = "My Actor Properties|Vector ",
meta=(ClampMin=-5.0f,ClampMax=5.0f,UIMin=-5.0f,UIMax=5.0f))
FVector TickLocationOffset;
如果這篇文章對你有張幫助的話,可以用你高貴的小手給我點一個免費的贊嗎
相信我,你也能變成光.
如果你有任何建議,或者是發現了我的錯誤,歡迎評論留言指出.