File size: 5,201 Bytes
b315adb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "AsyncMessageHandle.h"
#include "AsyncMessageId.h"
#include "Components/ActorComponent.h"
#include "GameFramework/Actor.h"
#include "GameplayTagContainer.h"
#include "StructUtils/InstancedStruct.h"
#include "ShooterTestAsyncMessageTestActor.generated.h"

struct FAsyncMessage;
class UStaticMeshComponent;

USTRUCT(BlueprintType)
struct FColorChangingMessage

{
	GENERATED_BODY()

	UPROPERTY(BlueprintReadOnly, EditAnywhere, Category="Test")
	FLinearColor DesiredColorToChange = FLinearColor::Red;
};

/**

 * A test actor which will spawn a task outside of the game thread to

 * broadcast different color messages

 */
UCLASS(BlueprintType)
class AAsyncColorChangeBroadcastActor : public AActor
{
	GENERATED_BODY()
public:

	AAsyncColorChangeBroadcastActor(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());
	
	virtual void BeginPlay() override;

	void SpawnBackgroundTask();
	
	void SpawnGameplayTagBroadcaster();

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Test")
	TInstancedStruct<FColorChangingMessage> ColorChangeData;

	UPROPERTY(EditAnywhere, Category="Test")
	FAsyncMessageId ColorMessageToBroadcast;

	UPROPERTY(EditAnywhere, Category="Test")
	FGameplayTag GameplayTagToBroadcastA;

	UPROPERTY(EditAnywhere, Category="Test")
	FGameplayTag GameplayTagToBroadcastB;

	UPROPERTY(BlueprintReadOnly, Category="Test")
	int32 NumBroadcasts = 0;

	UPROPERTY(EditAnywhere, Category="Test")
	float ColorMessageBroadcastFrequency = 3.0f;

	UPROPERTY(EditAnywhere, Category="Test")
	float GameplayTagBroadcastFrequency = 3.0f;

	FTimerHandle StartBackgroundTaskTimerHandle;

	FTimerHandle GameplayTagSpawner;
};

/**

 * An actor which will change the color of its static mesh

 * in response to an async message that it listens to

 */
UCLASS(BlueprintType)
class AColorChangingTestListener : public AActor
{
	GENERATED_BODY()
public:
	
	AColorChangingTestListener(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());

	virtual void BeginPlay() override;
	virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
	
	void HandleColorChange(const FAsyncMessage& Message);

	void HandleAnyColorChange(const FAsyncMessage& Message);

	const FAsyncMessageId GetColorChangeToListenTo();

	void SetupColorListener();
	void ResetListenerToColor();
	void SetupNamedThreadListener();
	
	UPROPERTY(EditAnywhere, Category="Test")
	TObjectPtr<UStaticMeshComponent> MeshComp;

	UPROPERTY(EditAnywhere, Category="Test")
	FName MaterialColorParamName = "ChangeColorParam";

	UPROPERTY(EditAnywhere, Category="Test")
	FAsyncMessageId OverrideMessageBinding;

	UPROPERTY(EditAnywhere, Category="Test")
	FGameplayTag TagToBindTo;
	
	UPROPERTY(EditAnywhere, Category="Test")
	bool bListenForParentTag = false;

	// Test receiving and binding messages on another thread
	UPROPERTY(EditAnywhere, Category="Test")
	bool bShouldSpawnTaskOnOtherThread = false;
	
	FTimerHandle BackgroundQueueMessageHandle;
	
	FAsyncMessageHandle BoundHandle_Color;

	FAsyncMessageHandle BoundHandle_ColorParent;
	
	int32 PreviousColorBoundTo = -1;
};


USTRUCT(BlueprintType)
struct FTestHeavyMessage

{
	GENERATED_BODY()

	UPROPERTY(BlueprintReadOnly, EditAnywhere, Category="Test")
	FLinearColor DesiredColorToChange = FLinearColor::Red;

	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category="Test")
	FRotator AmountToRotate;
};


/**

 * This is an actor which will queue a message for broadcast every single tick.

 */
UCLASS(BlueprintType)
class AHeavyPerformanceBroadcastor : public AActor
{
	GENERATED_BODY()

	AHeavyPerformanceBroadcastor(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());
	
public:
	virtual void Tick(float DeltaSeconds) override;

	void QueueMessage();
	
	UPROPERTY(EditAnywhere, Category="Test")
	TObjectPtr<UStaticMeshComponent> MeshComp;
	
	UPROPERTY(EditAnywhere, Category="Test")
	FAsyncMessageId MessageToQueue;

	UPROPERTY(EditAnywhere, Category="Test")
	FInstancedStruct MessagePayloadToQueue;

	UPROPERTY(EditAnywhere, Category="Test", meta=(UIMin=1))
	int32 NumberOfTimesToQueue = 1;

	UPROPERTY(EditAnywhere, Category="Test")
	TEnumAsByte<enum ETickingGroup> CustomTickGroup = TG_PrePhysics;
};


UCLASS(BlueprintType)
class AHeavyPerformanceListener : public AActor
{
	GENERATED_BODY()

	AHeavyPerformanceListener(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());
	
public:

	virtual void BeginPlay() override;
	virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
	
	UPROPERTY(EditAnywhere, Category="Test")
	TObjectPtr<UStaticMeshComponent> MeshComp;

	UPROPERTY(EditAnywhere, Category="Test")
	FAsyncMessageId MessageToListenFor;

	UPROPERTY(EditAnywhere, Category="Test")
	TEnumAsByte<ETickingGroup> GroupToListenFor = ETickingGroup::TG_PostPhysics;

private:

	void HandleMessage(const FAsyncMessage& Message);
	void SetupListener();
	void RemoveListener();
	
	FAsyncMessageHandle ListenerHandle;
};