// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "UObject/Object.h"
#include "Net/OnlineBlueprintCallProxyBase.h"
#include "Interfaces/OnlineSessionInterface.h"
#include "FindSessionsCallbackProxy.h"
#include "OculusFindSessionsCallbackProxy.generated.h"
/**
* Exposes FindSession of the Platform SDK for blueprint use.
*/
UCLASS(MinimalAPI)
class UOculusFindSessionsCallbackProxy : public UOnlineBlueprintCallProxyBase
{
GENERATED_UCLASS_BODY()
// Called when there is a successful query
UPROPERTY(BlueprintAssignable)
FBlueprintFindSessionsResultDelegate OnSuccess;
// Called when there is an unsuccessful query
UPROPERTY(BlueprintAssignable)
FBlueprintFindSessionsResultDelegate OnFailure;
// Searches for matchmaking room sessions with the oculus online subsystem
UFUNCTION(BlueprintCallable, Category = "Oculus|Session", meta = (BlueprintInternalUseOnly = "true"))
static UOculusFindSessionsCallbackProxy* FindMatchmakingSessions(int32 MaxResults, FString OculusMatchmakingPool);
// Searches for moderated room sessions with the oculus online subsystem
UFUNCTION(BlueprintCallable, Category = "Oculus|Session", meta = (BlueprintInternalUseOnly = "true"))
static UOculusFindSessionsCallbackProxy* FindModeratedSessions(int32 MaxResults);
// UOnlineBlueprintCallProxyBase interface
virtual void Activate() override;
// End of UOnlineBlueprintCallProxyBase interface
private:
// Internal callback when the session search completes, calls out to the public success/failure callbacks
void OnCompleted(bool bSuccess);
private:
// The delegate executed by the online subsystem
FOnFindSessionsCompleteDelegate Delegate;//デリゲート宣言
// Handle to the registered OnFindSessionsComplete delegate
FDelegateHandle DelegateHandle;//デリゲートハンドル宣言
// Object to track search results
TSharedPtr<FOnlineSessionSearch> SearchObject;
// Maximum number of results to return
int MaxResults;
// Optional: if searching within a matchmaking pool
FString OculusPool;
bool bSearchModeratedRoomsOnly;
};
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Http.h"
#include "Json.h"
#include "GameFramework/Actor.h"
#include "CurlApiTest.generated.h"
UCLASS()
class FURCRAEAUEOPENAI_API ACurlApiTest : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ACurlApiTest();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintCallable, Category = "Http")
void SendChatCompletionRequest();
void ProcessResponseSimple(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
//void ProcessResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
};
.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "CurlApiTest.h"
#include "CurlRequest.h"
#include "Http.h"
#include "Json.h"
#include "Misc/Paths.h"
#include "Misc/ConfigCacheIni.h"
// Sets default values
ACurlApiTest::ACurlApiTest()
{
// 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 ACurlApiTest::BeginPlay()
{
Super::BeginPlay();
SendChatCompletionRequest();
}
// Called every frame
void ACurlApiTest::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ACurlApiTest::SendChatCompletionRequest()
{
FString ApiKey = TEXT("sk-から始まる秘密鍵");
TSharedRef<IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest();
HttpRequest->SetURL("https://api.openai.com/v1/chat/completions");
HttpRequest->SetVerb("POST");
HttpRequest->SetHeader("Content-Type", "application/json");
HttpRequest->SetHeader("Authorization", "Bearer " + ApiKey);
// JSON Body
FString Content = TEXT(R"(
{
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": "Say this is a test!"}],
"temperature": 0.7
}
)");
HttpRequest->SetContentAsString(Content);
HttpRequest->OnProcessRequestComplete().BindUObject(this, &ACurlApiTest::ProcessResponseSimple);
/*
HttpRequest->OnProcessRequestComplete().BindLambda([this](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
ProcessResponse(Request, Response, bWasSuccessful);
});
*/
HttpRequest->ProcessRequest();
}
void ACurlApiTest::ProcessResponseSimple(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (bWasSuccessful && Response.IsValid())
{
FString ResponseContent = Response->GetContentAsString();
TSharedPtr<FJsonObject> JsonObject;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(ResponseContent);
if (FJsonSerializer::Deserialize(Reader, JsonObject) && JsonObject.IsValid())
{
FString MessageContent = JsonObject->GetArrayField("choices")[0]->AsObject()->GetObjectField("message")->GetStringField("content");
UE_LOG(LogTemp, Warning, TEXT("Response Message: %s"), *MessageContent);
}
else
{
UE_LOG(LogTemp, Error, TEXT("Failed to parse JSON response"));
}
}
else
{
UE_LOG(LogTemp, Error, TEXT("Request failed"));
}
}
// Some copyright should be here...
using UnrealBuildTool;
public class furcraeaUEOpenAI : ModuleRules
{
public furcraeaUEOpenAI(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
PublicIncludePaths.AddRange(
new string[] {
// ... add public include paths required here ...
}
);
PrivateIncludePaths.AddRange(
new string[] {
// ... add other private include paths required here ...
}
);
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core","HTTP", "Json",
// ... add other public dependencies that you statically link with here ...
}
);
PrivateDependencyModuleNames.AddRange(
new string[]
{
"CoreUObject",
"Engine",
"Slate",
"SlateCore",
// ... add private dependencies that you statically link with here ...
}
);
DynamicallyLoadedModuleNames.AddRange(
new string[]
{
// ... add any modules that your module loads dynamically here ...
}
);
}
}
C++クラス作成で
でActorを選択
パブリック押して名前入力
.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CurlApiTest.generated.h"
UCLASS()
class FURCRAEAUEOPENAI_API ACurlApiTest : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ACurlApiTest();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintCallable, Category = "Http")
void CurlSendRequest3();
};
.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "CurlApiTest.h"
// Sets default values
ACurlApiTest::ACurlApiTest()
{
// 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 ACurlApiTest::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ACurlApiTest::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ACurlApiTest::CurlSendRequest3()
{
FString ApiKey = TEXT("sk-からはじまるKEY");
TSharedRef<IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest();
HttpRequest->SetURL("https://api.openai.com/v1/chat/completions");
HttpRequest->SetVerb("POST");
HttpRequest->SetHeader("Content-Type", "application/json");
HttpRequest->SetHeader("Authorization", "Bearer " + ApiKey);
// JSON Body
FString Content = TEXT(R"(
{
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": "Say this is a test!"}],
"temperature": 0.7
}
)");
HttpRequest->SetContentAsString(Content);
//
HttpRequest->OnProcessRequestComplete().BindLambda([](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (bWasSuccessful && Response.IsValid())
{
FString ResponseContent = Response->GetContentAsString();
UE_LOG(LogTemp, Warning, TEXT("%s"), *ResponseContent);
}
else
{
UE_LOG(LogTemp, Error, TEXT("Request failed"));
}
});
HttpRequest->ProcessRequest();
}
クリーンが失敗するけど対策わからん ========== Clean: 47 succeeded, 0 failed, 1 skipped ========== ========== Clean completed at 1:51 and took 01.955 seconds ==========
スキップされたプロジェクトのクリーンが失敗した理由は、ログのメッセージに「Project not selected to build for this solution configuration」とあります。これは、そのプロジェクトが現在のソリューション構成ではビルド対象として選択されていないためです。