Unreal教学(2)——快速开发程序(Programming Quick Start)

来源:互联网 发布:大数据怎么可视化 编辑:程序博客网 时间:2024/06/06 00:39

创建一个C++ 类,命名为FloatActor,该类的父类Actor

就会在源码中自动生成

// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"#include "GameFramework/Actor.h"#include "FloatActor.generated.h"UCLASS()class QUICKSTART_API AFloatActor : public AActor{GENERATED_BODY()public:// Sets default values for this actor's propertiesAFloatActor();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public:// Called every framevirtual void Tick(float DeltaTime) override;float RunningTime;};
对应的cpp文件。

// Fill out your copyright notice in the Description page of Project Settings.#include "FloatActor.h"// Sets default valuesAFloatActor::AFloatActor(){ // 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;}// Called when the game starts or when spawnedvoid AFloatActor::BeginPlay(){Super::BeginPlay();}// Called every framevoid AFloatActor::Tick(float DeltaTime){Super::Tick(DeltaTime);FVector NewLocation = GetActorLocation();float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));NewLocation.X += DeltaHeight * 5.0f;NewLocation.Y += DeltaHeight * 10.0f;NewLocation.Z += DeltaHeight * 20.0f;RunningTime += DeltaTime;SetActorLocation(NewLocation);}

可以看到在.h文件申明了一个变量RunningTime.

然后每帧中都会调用AFloatActor::Tick方法,然后通过RunningTime转换为高度DeltaHeight,最后设置物体的位置。

最终的结果可以看到漂浮的物体。

1. 认识GetActorLocation()函数,这个函数的作用是返回根节点(RootComponent)的的位置,返回的是一个FVector的向量。

举例来说

    // Get the current location      FVector ActorLocation = GetActorLocation();      // Move it slightly      ActorLocation.Z += 10.0f;      // Set the location- this will blindly place the actor at the given location      SetActorLocation( ActorLocation, false );

(1)记住第一个函数,GetActorLocation(),这里可以看到函数的第一个字符大写,和平时常规的有点不同。

属于Engine模块,文件位置在

Runtime/Engine/Classes/GameFramework/Actor.h

这里有很多的命名规则不同,比如命名的变量NewLocation也是大写,需要遵守它们的写法来。


(2) FVector,

这是一个结构体, struct FVector.

A vector in 3-D space composed of components (X, Y, Z) with floating point precision.

是一个3维坐标系中的向量,这个向量含有三个浮点型的X, Y, Z元素。

属于core模块,文件位置

A point or direction FVector in 3d space. The full C++ class is located here: Engine/Source/Runtime/Core/Public/Math/Vector.h

FVector是一个非常基础的结构体,下面有很多重要的构造函数,和成员函数。

FVector这个类中有很多的方法可以宫调用,比如

Normalize 单位化这个向量。

GetMin或者向量中最小的值。

而且有几个常量向量,由这几个常量向量就能知道Unreal所采用的坐标轴。

ForwardVector   (1, 0, 0)

OneVector(1, 1, 1)

RightVector (0, 1, 0)

UpVector (0, 0, 1)

ZeroVector (0, 0, 0)


(3) FMath

也是一个结构体 strcut FMath : public FPlatformMath

Structure for all math helper functions, inherits from platform math to pick up platform-specific implementations

关于数学计算上的全局函数。

在core模块中,头文件目录Runtime/Core/Public/Math/UnrealMathUtility.h

这里用到的Sin函数在strcut FGenericPlatformMath结构体中,所以这里

GenericPlatformMath.h: Generic platform Math classes, mostly implemented with ANSI C++

可以看到这个结构体基本上是实现的C++的方法。

很奇怪FPlatformMath没有搜索到,搜到的是FGenericPlatformMath.

模块属于core,头文件在Runtime/Core/Public/GenericPlatform/GenericPlatformMath.h

DeltaHeight是一个高度随着时间变化的正弦波,(-1, 1)的范围。

然后是Z位置上加上这个值的多少倍数,表示距离。


(4)SetActorLocation和getActorLocation一样,都是AActor类的方法。

可以看到AFloatActor继承AActor所以,自然就获取了父类的方法,可以直接调用,而不是所谓的静态函数调用。

这里需要重点讲AActor这个类。


(5)AActor

class AActor : public UObject

可以看到它的直接父类是UObject,

Actor is the base class for an Object that can be placed or spawned in a level. Actors may contain a collection of ActorComponents, which can be used to control how actors move, how they are rendered, etc. The other main function of an Actor is the replication of properties and function calls across the network during play.

是一个基类,可以含有很多的ActorComponents.

属于模块Engine

头文件在Runtime/Engine/Classes/GameFramework/Actor.h

内部的函数实在太多,先弄清楚两个,GetActorLocation SetActorLocation

GetActorLocation就返回这个对象的FVector值没有什么好讲的,那就介绍SetActorLocation.

bool SetActorLocation(    const FVector & NewLocation,    bool bSweep,    FHitResult * OutSweepHitResult,    ETeleportType Teleport)
实现在Runtime/Engine/Private/Actor.cpp
这个函数的第2, 3, 4应该是默认参数,第一个参数必须有。

第一个参数是表示的目标位置,

第二个参数bSweep是一个标识符,

可以打这样一个比方,bSweep没有设置为true的时候,那么可以直接移动到目的位置。如果设置为true,那么移动的过程中需要检查中间是否有障碍物,如果有就不会设置成功,返回错误。

下面这个例子很好地展示了它的用法,

    // Get the current location      FVector CurrentLocation = GetActorLocation();      // Move it slightly      CurrentLocation.Z += 1.0f;      FHitResult HitResult;      if( SetActorLocation(CurrentLocation, true, &HitResult) == false )      {          // If the set function returned false something is blocking at that location. We can interrogate this result to determine details of this          // @See FHitResult for more information          if( HitResult.GetActor() != nullptr)          {              UE_LOG( LogDocumentationCode, Warning, TEXT("Cannot move object to location, blocked by %s"), *HitResult.GetActor()->GetName() );          }             }  

可以看到这个例子中首先获取这个对象的当前的位置,然后Z轴进行加1,设置现在的对象的位置。

如果设置位置不成功,从解释中可以看到那个位置被其它的物体占据,所以需要打印错误信息,通过第三个参数获取阻挡物体的名字。

这里第一篇就讲完了,很多的Unreal的东西还不是很理解,但是后面会更详细细致地讲述。

简单的物体移动已经讲完。

阅读全文
0 0
原创粉丝点击