c++ - Unreal C++ 使用线条跟踪从 Actor 调用 void

标签 c++ line trace unreal-engine4 raycasting

我是 C++ 新手(使用 C#),并尝试在 Actor 受到光线转换击中时调用 void 并将其发送给它。 这是我尝试调用“EditMesh”的脚本标题(靠近底部)

 #pragma once
    #include "CoreMinimal.h"
    #include "GameFramework/Actor.h"
    #include "ProceduralMeshComponent.h"
    //#include "Core.h"
    #include "Chunk.generated.h"




    UCLASS()
    class VOXELWARS_API AChunk : public AActor 
    {

        GENERATED_BODY()

    public:
        // Sets default values for this actor's properties
        AChunk();

    protected:
        // Called when the game starts or when spawned
        virtual void BeginPlay() override;


    public:
        // Called every frame
        //virtual void Tick(float DeltaTime) override;

        //UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Default)
        //  UMaterialInterface* TheMaterial;
        UPROPERTY(EditAnywhere)
            AChunk* ChunkRef; 

        UPROPERTY(EditAnywhere)
            class UMaterialInterface* OnMaterial;

        UPROPERTY(VisibleAnywhere)
    UProceduralMeshComponent * mesh;
        UPROPERTY(VisibleAnywhere)
    int faceCount = 0;
    TArray<FVector> vertices;
    UPROPERTY(VisibleAnywhere)
    TArray<int32> Triangles;
    TArray<FVector> normals; 
    TArray<FVector2D> UV0;
    TArray<FProcMeshTangent> tangents;
    TArray<FLinearColor> vertexColors;
    TArray<FVector2D> blocks; 

    int worldData[16][16][16] = { {} };
    float tUnit = 0.0625; 


    public:void CubeTop(int x = 0, int y = 0, int z = 0, int block = 0);
    public:void CubeNorth(int x = 0, int y = 0, int z = 0, int block = 0);
    public:void CubeEast(int x = 0, int y = 0, int z = 0, int block = 0);
    public:void CubeSouth(int x = 0, int y = 0, int z = 0, int block = 0);
    public:void CubeWest(int x = 0, int y = 0, int z = 0, int block = 0);
    public:void CubeBot(int x = 0, int y = 0, int z = 0, int block = 0);
    public:void Cube(FVector2D texturePos);
    public:void UpdateMesh(); 
    public:void ClearMeshData(); 
    public:void GenMesh();
    public:void EditMesh(int x = 0, int y = 0, int z = 0, int block = 0); //here

    public:int Chk(int x = 0, int y = 0, int z = 0);
    //     int* mBlock(int x = 0, int y = 0, int z = 0);
    };

这是我想调用 EditMesh() 的另一个脚本的 cpp 的一部分

        #include "Chunk.h"
        #include "VoxelWarsCharacter.h"
        #include "VoxelWarsProjectile.h"
        #include "Animation/AnimInstance.h"
        #include "Camera/CameraComponent.h"
        #include "Components/CapsuleComponent.h"
        #include "Components/InputComponent.h"
        #include "GameFramework/InputSettings.h"
        #include "HeadMountedDisplayFunctionLibrary.h"
        #include "Kismet/GameplayStatics.h"
        #include "MotionControllerComponent.h"
        #include "XRMotionControllerBase.h" // for 

    void AVoxelWarsCharacter::OnFire()
    {
        FVector StartLocation = GetActorLocation(); //your location 
        StartLocation.Z += 15; 
        FVector EndLocation = StartLocation + (FirstPersonCameraComponent->GetForwardVector() * 4000.f); //get forward dir
        FHitResult Hit;
        FCollisionQueryParams ColParms; //ignor stuff
        ColParms.AddIgnoredActor(this);//ignor hitting self

        GetWorld()->LineTraceSingleByChannel(Hit, StartLocation, EndLocation, ECC_Visibility, ColParms);//ECC_WorldDynamic will only hit Actors, ECC_Visibility is everything

        if (Hit.GetActor()) {
            //Hit.GetActor()->AChunk::EditMesh(0, 0, 0, 1);
            GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor(255,255,255), "Hit a actor");
            UKismetSystemLibrary::DrawDebugLine(GetWorld(), StartLocation, Hit.Location, FColor(255, 255, 0), 5, 1);//FColor::Red 

            Hit.GetActor()->FindComponentByClass<AChunk>()->EditMesh((int)Hit.Location.X, (int)Hit.Location.Y, (int)Hit.Location.Z, 0); 
           //How do I do this?????????
        }
    }

我遇到的错误

CompilerResultsLog:错误:D:\ProgramFiles\Epic Games\4.19\UE_4.19\Engine\Source\Runtime\Engine\Classes\GameFramework/Actor.h(2640):错误 C2338:FindComponentByClass 的“T”模板参数必须从 UActorComponent CompilerResultsLog 派生:

错误:C:\Users\nolan\Desktop\VoxelWarsUR\Project\VoxelWars\Source\VoxelWars\VoxelWarsCharacter.cpp(160):注意:请参阅函数模板实例化'T的引用 *AActor::FindComponentByClass(void) const' 正在编译

最佳答案

parameter to FindComponentByClass must be derived from UActorComponent

VoxelWarsCharacter.cpp 的 160 行:

FindComponentByClass<AChunk>

AChunk 不是组件。组件以 U 开头。

关于c++ - Unreal C++ 使用线条跟踪从 Actor 调用 void,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54175000/

相关文章:

c++ - 警告 C5246 : the initialization of a subobject should be wrapped in braces

c++ - 需要帮助理解 ns3 -> Random Walk2MobilityModel

css - 多个段落之间的水平线(CSS)

ruby - 使用正则表达式对散列进行行拆分

c++ - 在 C++ 中使用 ofstream 创建文本文件的问题

c++ - 算术运算符参数类型

java - 如何在 Java GUI 中添加一行?

c - 追踪大型源代码

Oracle - Autotrace 中的一致性获取与 SQL TRACE 中的一致性读取之间的区别

python - x 轴上的偏移绘图跟踪