一、序言
GameInstance:正在运行的游戏实例的高级管理器对象。在游戏创建时生成并且在游戏实例关闭之前不会被销毁。作为独立游戏运行,将会有其中之一。在 PIE(play-in-editor)中运行将为每个 PIE 实例生成其中之一。
GameInstance是UE4中的全局类,只有一个实例,
- 保证一个类只有一个实例
- 提供一个访问该实例的全局节点,可以视为一个全局变量
- 仅在首次请求单例对象时对其进行初始化。\
二、GameInstance的作用
- “Init”与“ShutDown”:引擎初始化与关闭时执行的逻辑;
- “NetWorkError”与“TravelError”:网络和跨关卡时的错误;
- 与LocalPlayer相关:
- 为游戏保存全局数据:比如上一个关卡的信息需要在下一个关卡使用时;只是临时数据,游戏结束则消失;(持久保存数据需要用SaveGame)
- UI界面的切换:UE4中的UI比较独特,是通过独立的Widget来产生,使用时需要Create此Widget然后show出来。复杂的并且重用的UI界面切换逻辑可以在此实现,因为GameInstance在各个类中都可以获得,方便各个地方调用;
- Network、Session相关的事务;管理Session;
- 自定义的需要全局使用的逻辑:比如TCP、UDP通信,因为绑定本机IP和端口、并且发送和接受的方法实际上只定义一次,因为跨关卡的特殊性,不可能在每个level中使用一个Actor每次都设置一次,所以应该写在GameInstance中;
三、创建GameInstance并且实例化
实例化全局变量
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "MyGameInstance.generated.h"
/**
*
*/
USTRUCT()
struct FMyStruct
{
GENERATED_USTRUCT_BODY()
UPROPERTY()
FString GameInstanceName;
UPROPERTY()
float GameInstanceID;
public:
};
UCLASS()
class NEWOBJECT_API UMyGameInstance : public UGameInstance
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable)
static UMyGameInstance* GetMyInstance();
UPROPERTY(EditAnywhere,BlueprintReadWrite,Category = "GameInstance")
float MyHealth = 1.0f;
int32 Number = 2;
float CurrentHealth = 0.5;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyGameInstance.h"
UMyGameInstance* UMyGameInstance::GetMyInstance()
{
UMyGameInstance* instance = nullptr;
if (GEngine)
{
FWorldContext* context = GEngine->GetWorldContextFromGameViewport(GEngine->GameViewport);
instance = Cast<UMyGameInstance>(context->OwningGameInstance);
}
return instance;
}
创建Actor类创建对象
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyObject.h"
#include "MyGameInstance.h"
#include "MyActor.generated.h"
UCLASS()
class NEWOBJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY()
TSubclassOf<UMyObject> MyObjectInstance;
UPROPERTY()
UMyObject *MyObject;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
// 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 spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
MyObjectInstance = UMyObject::StaticClass();
MyObject = NewObject<UMyObject>(GetWorld(),MyObjectInstance);
if (MyObject)
{
UE_LOG(LogTemp, Warning, TEXT("MyobjectName is: %s"), *MyObject->GetName());
UE_LOG(LogTemp, Warning, TEXT("CharactreID is: %d"), MyObject->GameDataBase.CharacterID);
UE_LOG(LogTemp, Warning, TEXT("Character is: %s"), *MyObject->GameDataBase.Character);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("MyobjectName : %s"), *MyObject->GetName()));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red,FString::Printf(TEXT("CharactreID : %d"), MyObject->GameDataBase.CharacterID));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Character : %s"), *MyObject->GameDataBase.Character));
UMyGameInstance* MyGameInstancePtr = UMyGameInstance::GetMyInstance();
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("GameInstance : %s"), *MyGameInstancePtr->GetName()));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("GameInstanceId : %d"), MyGameInstancePtr->Number));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("GameInstanceId : %f"), MyGameInstancePtr->CurrentHealth));
}
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
结果如下