KLingo Project Documentation 1.0.0
Unreal Engine 5.6 C++ Project Documentation
로딩중...
검색중...
일치하는것 없음
UCommonFunctionLibrary.cpp
이 파일의 문서화 페이지로 가기
1// Copyright (c) 2025 Doppleddiggong. All rights reserved.
2
8
9#include "Kismet/GameplayStatics.h"
10#include "Kismet/KismetMathLibrary.h"
11#include "Misc/DateTime.h"
12//
13// void UCoffeeCommonUtil::TestULog()
14// {
15// int32 Number = 100;
16// float Pi = 3.14f;
17// int64 LongValue = 10000;
18// double DoubleValue = 100000;
19// bool IsGood = false;
20// FString MyName = "MyName";
21//
22// // --- ULOG 기능 테스트 ---
23// UE_LOG( LogTemp, Warning, TEXT("Hello World"));
24// UE_LOG( LogTemp, Warning, TEXT("Number : %d"), Number);
25// // 결과: "9,999" (en-US 기준)
26// FText LocalizedNumber = FText::AsNumber(Number);
27// UE_LOG( LogTemp, Warning, TEXT("#,##0 : %s"), *LocalizedNumber.ToString());
28// UE_LOG( LogTemp, Warning, TEXT("pi : %f"), Pi);
29// UE_LOG( LogTemp, Warning, TEXT("longValue : %lld"), LongValue);
30// UE_LOG( LogTemp, Warning, TEXT("doubleValue : %f"), DoubleValue);
31// UE_LOG( LogTemp, Warning, TEXT("isGood : %d"), IsGood);
32// UE_LOG( LogTemp, Warning, TEXT("myName : %s"), *MyName);
33// UE_LOG( LogTemp, Warning, TEXT("myName : %s"), TEXT("배주백"));
34// }
35//
36// void UCoffeeCommonUtil::TestInBound()
37// {
38// // --- CoffeeLibrary 기능 테스트 ---
39// UE_LOG(LogTemp, Warning, TEXT("--- Testing CoffeeLibrary::CommonUtil::InBounds ---"));
40//
41// // MyArrayCount는 'InBounds' 함수를 테스트하기 위한 가상의 배열 크기입니다.
42// constexpr int32 MyArrayCount = 5;
43// constexpr int32 TestIndex1 = 3; // 유효한 인덱스
44// constexpr int32 TestIndex2 = 5; // 유효하지 않은 인덱스
45//
46// const bool bIsIndex1InBounds = InBounds(TestIndex1, MyArrayCount);
47// UE_LOG(LogTemp, Warning, TEXT("Is index %d in bounds [0..%d)? -> %s"),
48// TestIndex1, MyArrayCount, bIsIndex1InBounds ? TEXT("True") : TEXT("False"));
49//
50// const bool bIsIndex2InBounds = InBounds(TestIndex2, MyArrayCount);
51// UE_LOG(LogTemp, Warning, TEXT("Is index %d in bounds [0..%d)? -> %s"),
52// TestIndex2, MyArrayCount, bIsIndex2InBounds ? TEXT("True") : TEXT("False"));
53// }
54
55
56FString UCommonFunctionLibrary::GererateMD5(const FString& InText)
57{
58 FTCHARToUTF8 UTF8Text(*InText);
59
60 uint8 Digest[16];
61 FMD5 Md5;
62 Md5.Update((uint8*)UTF8Text.Get(), UTF8Text.Length());
63 Md5.Final(Digest);
64
65 FString HashString;
66 for (uint8 B : Digest)
67 HashString += FString::Printf(TEXT("%02x"), B);
68 return HashString;
69}
70
71bool UCommonFunctionLibrary::InBounds(const int32 Index, const int32 Count)
72{
73 return (Index >= 0) && (Index < Count);
74}
75
76int32 UCommonFunctionLibrary::GetRandomIndex(const TArray<int32>& TargetArray, bool& bIsValid)
77{
78 if (TargetArray.Num() == 0)
79 {
80 bIsValid = false;
81 return INDEX_NONE;
82 }
83 bIsValid = true;
84 return TargetArray[FMath::RandRange(0, TargetArray.Num() - 1)];
85}
86
87UAnimMontage* UCommonFunctionLibrary::GetRandomMontage(const TArray<UAnimMontage*>& Montages)
88{
89 const int32 Num = Montages.Num();
90 if (Num <= 0)
91 return nullptr;
92
93 const int32 Idx = FMath::RandRange(0, Num - 1);
94 return Montages[Idx];
95}
96
98{
99 const auto DataTime = FDateTime::UtcNow();
100 return DataTime.ToUnixTimestamp();
101}
102
104 UPrimitiveComponent* Target, int32 ElementIndex, FName OptionalName)
105{
106 if (!Target)
107 return nullptr;
108
109 const int32 Num = Target->GetNumMaterials();
110 if (ElementIndex < 0 || ElementIndex >= Num)
111 return nullptr;
112
113 UMaterialInterface* CurMat = Target->GetMaterial(ElementIndex);
114 if (!CurMat)
115 return nullptr;
116
117 if (UMaterialInstanceDynamic* AsMID = Cast<UMaterialInstanceDynamic>(CurMat))
118 return AsMID;
119
120 UMaterialInstanceDynamic* NewMID = UMaterialInstanceDynamic::Create(CurMat, Target, OptionalName);
121 if (NewMID)
122 Target->SetMaterial(ElementIndex, NewMID);
123
124 return NewMID;
125}
126
127void UCommonFunctionLibrary::PlayLocationSound(const AActor* Actor, USoundBase* Sound, const float RetriggerDelay)
128{
129 if (!IsValid(Actor) || !Sound)
130 return;
131
132 if (auto World = Actor->GetWorld())
133 {
134 if ( RetriggerDelay > 0.0f)
135 {
136 TWeakObjectPtr<const AActor> WeakActor = Actor;
137 FTimerHandle TimerHandle;
138 World->GetTimerManager().SetTimer(TimerHandle, [WeakActor, Sound]()
139 {
140 if (WeakActor.IsValid())
141 {
142 UGameplayStatics::PlaySoundAtLocation(WeakActor.Get(), Sound, WeakActor->GetActorLocation());
143 }
144 }, RetriggerDelay, false);
145 }
146 else
147 {
148 UGameplayStatics::PlaySoundAtLocation(Actor, Sound, Actor->GetActorLocation());
149 }
150 }
151}
152
153float UCommonFunctionLibrary::GetDistance(AActor* A, AActor* B)
154{
155 if ( !IsValid(A) || !IsValid(B) )
156 return 0;
157
158 return UKismetMathLibrary::Vector_Distance( A->GetActorLocation(), B->GetActorLocation() );
159}
160
161FString UCommonFunctionLibrary::RemoveLineBreaks(const FString& InText)
162{
163 FString CleanedText = InText;
164 CleanedText.ReplaceInline(TEXT("\r\n"), TEXT(""));
165 CleanedText.ReplaceInline(TEXT("\r"), TEXT(""));
166 CleanedText.ReplaceInline(TEXT("\n"), TEXT(""));
167 return CleanedText;
168}
169
170void UCommonFunctionLibrary::SetCollisionDebugVisible(UPrimitiveComponent* Target, bool bVisible)
171{
172 if (!Target)
173 return;
174
175 Target->SetHiddenInGame(!bVisible);
176 Target->SetVisibility(bVisible);
177}
178
180{
181 if (Word.IsEmpty())
182 return false;
183
184 // 한글 유니코드 범위 체크
185 // - 완성형 한글: AC00-D7A3 (가-힣)
186 // - 한글 자음/모음: 1100-11FF, 3130-318F
187 for (const TCHAR& Char : Word)
188 {
189 // 완성형 한글 (가-힣)
190 if (Char >= 0xAC00 && Char <= 0xD7A3)
191 continue;
192
193 // 한글 자음/모음
194 if ((Char >= 0x1100 && Char <= 0x11FF) || (Char >= 0x3130 && Char <= 0x318F))
195 continue;
196
197 // 공백 허용
198 if (Char == TEXT(' '))
199 continue;
200
201 // 한글이 아닌 문자 발견
202 return false;
203 }
204
205 return true;
206}
UCommonFunctionLibrary 클래스를 선언합니다.
static void PlayLocationSound(const AActor *Actor, USoundBase *Sound, const float RetriggerDelay)
지정된 위치에서 사운드를 재생한다.
static bool InBounds(const int32 Index, const int32 Count)
주어진 인덱스가 배열 범위 안에 있는지 확인한다.
static void SetCollisionDebugVisible(UPrimitiveComponent *Target, bool bVisible)
static int32 GetRandomIndex(const TArray< int32 > &TargetArray, bool &bIsValid)
정수 배열에서 랜덤 인덱스를 선택한다.
static int64 GetNowTimestamp()
현재 시간을 Unix Timestamp(초)로 반환한다.
static FString GererateMD5(const FString &InText)
static UAnimMontage * GetRandomMontage(const TArray< UAnimMontage * > &Montages)
몽타주 배열에서 랜덤 항목을 반환한다.
static FString RemoveLineBreaks(const FString &InText)
줄바꿈 문자들을 모두 공백으로 치환한 문자열을 반환
static class UMaterialInstanceDynamic * GetOrCreateMID(class UPrimitiveComponent *Target, int32 ElementIndex, FName OptionalName=NAME_None)
머티리얼 인스턴스 다이내믹을 얻거나 새로 생성한다.
static float GetDistance(AActor *A, AActor *B)
두 액터 사이의 거리를 계산한다.
static bool IsValidKoreanWord(const FString &Word)
한국어 단어 검증 (한글만 포함되어 있는지 확인)