UE5 c++ 小白备忘录

蓝图可调用

UFUNCTION(BlueprintCallable, Category = "MyFunction")

// Fill out your copyright notice in the Description page of Project Settings.
//GameMode.h


#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameMode.h"
#include "MyGameMode.generated.h"

/**
 * 
 */
UCLASS()
class UE5CPP01_API AMyGameMode : public AGameMode
{
	GENERATED_BODY()
public:
	AMyGameMode();

	virtual	void BeginPlay() override;
	
};
// Fill out your copyright notice in the Description page of Project Settings.
//GameMode.cpp

#include "MyGameMode.h"
#include "MyGameState.h"
#include "myhud.h"
#include "mypawn.h"
#include "MyPlayerController.h"
#include "myplayerstate.h"

AMyGameMode::AMyGameMode()
{
	DefaultPawnClass = AMyPawn::StaticClass();
	GameStateClass = AMyGameState::StaticClass();
	HUDClass = AMyHUD::StaticClass();
	PlayerControllerClass = AMyPlayerController::StaticClass();
	PlayerStateClass = AMyPlayerState::StaticClass();

}

void AMyGameMode::BeginPlay()
{
	Super::BeginPlay();



}

// Fill out your copyright notice in the Description page of Project Settings.
//Actor.h


#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

UENUM()
namespace MyTestEnum
{
	enum MyEnum
	{
		mytype1,
		mytype2,
		mytype3,
	};
}

USTRUCT(BlueprintType)
struct MyStruct
{
	GENERATED_USTRUCT_BODY()

		UPROPERTY(EditAnywhere, BlueprintReadOnly)
		float health;

	UPROPERTY(EditAnywhere, BlueprintReadOnly)
		int32 max;

	UPROPERTY(EditAnywhere, BlueprintReadOnly)
		FString Myname;
};


UCLASS()
class UE5CPP01_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;


public:
	//蓝图可写
	UPROPERTY(EditDefaultsOnly, Category = "MyInt")
		int32 editonly;

	//蓝图可见
	UPROPERTY(VisibleDefaultsOnly, Category = "MyInt")
		int32 visibilityonly;

	//蓝图可读可写(get,set)
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyInt")
		int32 editanywhereRW;

	//蓝图可读(get)
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "MyInt")
		int32 redonly;

	//蓝图可调用函数
	UFUNCTION(BlueprintCallable, Category = "Myfunction")
		void Myfunc();

	//蓝图重载函数,c++调用蓝图函数(事件形式),BlueprintImplementableEvent c++声明,不做定义
	UFUNCTION(BlueprintImplementableEvent, Category = "Myfunction")
		void Myfunctionoverride();

	//蓝图重载函数,有返回值,c++调用蓝图函数,BlueprintImplementableEvent c++声明,不做定义
	UFUNCTION(BlueprintImplementableEvent, Category = "Myfunction")
		int32 Myfunctionoverridereturn();

	//蓝图重载函数,BlueprintNativeEvent, c++声明,c++本地定义,但是需要在定义的时候加上_Implementation,蓝图重写的时候调用蓝图,如果没有重新调用C++本地定义
	UFUNCTION(BlueprintNativeEvent, Category = "Myfunction")
		void Myfunctionoverride01();

	//蓝图重载函数,BlueprintNativeEvent, c++声明,c++本地定义,但是需要在定义的时候加上_Implementation,蓝图重写的时候调用蓝图,如果没有重新调用C++本地定义
	UFUNCTION(BlueprintNativeEvent, Category = "Myfunction")
		int32 Myfunctionoverride02();

	//声明枚举变量
	UPROPERTY(EditAnywhere, Category = "MyEnum")
		TEnumAsByteMyEnum;

	//声明结构体变量
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mystruct")
		MyStruct Data;
};
// Fill out your copyright notice in the Description page of Project Settings.
//Actor.cpp

#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();


	float f1 = 20.0f;
	int f2 = 2;

	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(1, 60.f, FColor::Cyan, FString("hello world"));
		GEngine->AddOnScreenDebugMessage(1, 60.f, FColor::Cyan, FString::Printf(TEXT("string %f"), f1));
		GEngine->AddOnScreenDebugMessage(1, 60.f, FColor::Cyan, FString::Printf(TEXT("int %d"), f2));
	}

	FString mystring = TEXT("myis string");

	FName myname = FName(*mystring);

	FText mytext = FText::FromString(mystring);

	mystring = myname.ToString();

	float f = 5.0f;

	mystring = FString::SanitizeFloat(f);

	int32 i = 2;
	mystring = FString::FromInt(i);
	
}

// Called every frame
void AMyActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void AMyActor::Myfunc()
{
	//Ctrl+Shift+Space
	GEngine->AddOnScreenDebugMessage(1, 60.f, FColor::Cyan, FString("this is BlueprintCallable Function"));
}

void AMyActor::Myfunctionoverride01_Implementation()
{

}

int32 AMyActor::Myfunctionoverride02_Implementation()
{
	return 0;
}

你可能感兴趣的:(c++,ue5,开发语言)