Unreal c++ about how to use macro UFUNCTION

Unreal c++ about how to use macro UFUNCTION

First of all, what is UFUNCTION, the function is a function that can be input on the Unreal interface to run the function!

first! We want a function!

What function!

Just enter the command to run the function and print the log!

old rules!

MyGameModeBase.h

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "MyGameModeBase.generated.h"

/**
 * 
 */
UCLASS()
class CPP_OBEJCT_API AMyGameModeBase : public AGameModeBase
{
	GENERATED_BODY()

protected:

	virtual void BeginPlay() override;
	
	//这是一个指令函数,设置后,可以在虚幻窗口输入函数名字,就可以运行该函数了
	UFUNCTION(Exec)
	void SelfDestoryActors();

public:
	//直接新建一个全局变量!然后就可以在所有函数中使用了!
	AActor* MyActor;

};

MyGameModeBase.cpp

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


#include "MyGameModeBase.h"
#include "MyActor.h"



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

	UE_LOG(LogTemp, Log, TEXT("你好世界!"));

	//动态生成actor操作!
	//GetWorld()->SpawnActor<AMyActor>();

	MyActor = GetWorld()->SpawnActor(AMyActor::StaticClass());


}


//这是新建的一个函数,主要要用静态变量来引用!
void AMyGameModeBase::SelfDestoryActors() {
	GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Red, TEXT("ACB"));
	GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Red, TEXT("你好世界!"));
	UE_LOG(LogTemp, Log, TEXT("你好世界!"));

	if (MyActor) {
		MyActor->Destroy();
	}
}

Then remember, compile it with f7!

Run Unreal!

Enter the function name!

 

 

 Now it's out!

 

Guess you like

Origin blog.csdn.net/m0_46449592/article/details/128595985