UE4做配置文件+查找场景actor几种方式
目录
一、目的
1、想:UE4中制作配置文件
2、想:UE4创建单独的C++类,专门作为配置文件,这样其余对象访问配置文件对象里面的数据成员就可以了。
二、参考
1、ue4 c++ 查找场景actor几种方式
- 总结:good:知道自己C++类,通过代码直接访问别的C++类。
三、注意
1、自定义类,包含头文件时候,需要放在这里面
2、获取的路径是指定位置
四、操作
1、UE4中创建Actor的C++类(什么类基本都是可以的)
1、配置文件类:MyActor_config.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Paths.h" //配置文件:FPaths需要的头文件
#include "../../ThirdParty/Fast/include/Fast_Config.h" //一定写在这里!
#include "MyActor_config.generated.h"
struct T_FLASH
{
char path[255];
};
UCLASS()
class RSXB_API AMyActor_config : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor_config();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
public:
//读取配置文件
void ReadConfig();
T_FLASH mFlash;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Damage")
bool bIsOkReadConfig;//是否成功读取配置文件
};
1、配置文件类:MyActor_config.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor_config.h"
// Sets default values
AMyActor_config::AMyActor_config()
{
// 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;
//读取配置文件
//ReadConfig();
}
// Called when the game starts or when spawned
void AMyActor_config::BeginPlay()
{
读取配置文件
ReadConfig();
Super::BeginPlay();
}
// Called every frame
void AMyActor_config::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AMyActor_config::ReadConfig()
{
bIsOkReadConfig = true;
FString str = FPaths::GeneratedConfigDir();
char path[255];
sprintf_s(path, "%sconfig.ini", TCHAR_TO_UTF8(*str));
int a = 0;
//速度
a = FastReadIniValue(path, "Flash", "path", &mFlash.path, 0);
if (a < 0)
bIsOkReadConfig = false;
}
1、UE4场景设置,将创建的配置文件类放到场景中,方便其他类通过场景访问这个配置文件类
1、别的类访问自定义类:MyHUD.h
1、别的类访问自定义类:MyHUD.cpp
#include "Kismet/GameplayStatics.h"
void AMyHUD::BeginPlay()
{
Super::BeginPlay();
//场景中找到AMyActor_config类的对象
TArray<AActor*> Actors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AMyActor_config::StaticClass(), Actors);
myActor_config = static_cast<AMyActor_config*>(Actors[0]);
}
1、文件夹设置:config.ini
[Flash]
path=F:/Project/yinTang/renShiXiBao/Scripts/NEW4.21/RSXB100/Flash/RSXB100/RSXB.swf