ue4 动态多播代理

ue4 动态多播代理_第1张图片

 

1.首先在.h的class上边声明一个代理

//声明一个参数的代理
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMyDelegate,FString,Str);+

2.定义代理


    UPROPERTY(BlueprintAssignable)
    FMyDelegate  MyDelegateName;

3.在cpp文件中调用代理

MyDelegateName.Broadcast(FString("String From C++"));

 

 

 

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

#pragma once

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

//声明一个参数的代理
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMyDelegate,FString,Str);

UCLASS()
class HELLOHTTP_API ADelegates : public AActor
{
	GENERATED_BODY()

	//定义代理
	UPROPERTY(BlueprintAssignable)
	FMyDelegate  MyDelegateName;
	
public:	
	// Sets default values for this actor's properties
	ADelegates();

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

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



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


#include "Delegates.h"
#include "Engine.h"

// Sets default values
ADelegates::ADelegates()
{
 	// 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 ADelegates::BeginPlay()
{
	Super::BeginPlay();


	MyDelegateName.Broadcast(FString("String From C++"));
}

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

}


 

你可能感兴趣的:(C++,ue4)