Download

[TUTORIAL] make own blueprint actor for C++ web message

Hello everybody!

my tutorial follow other that describe how to do web requests and compute the messages using UE4 HTTP and JSON modules, please look to the following reference materiels first https://wiki.unrealengine.com/UE4.10_How_To_Make_HTTP_GET_Request_in_C%2B%2B and https://wiki.unrealengine.com/Http-requests and https://www.youtube.com/watch?v=4UVdL4gHTOw then you will have easier time understanding

after reading the materiels here you should be ready to do a web request and use the message back in your game

  1. go to make the new project, basic code, mine is named tutorial

**

https://lh4.googleusercontent.com/vE4ksXRk2NB9iUw2BNlPnJGL-OF8HLeD04ynXBDdkZVM51Z8bqXkWJPzMbjEz0n8oScAnQd4srOOYtcZSc5yL-4zdum3IM-sfMXsgZPiECi2DMnq_fe2rocba1foFzKw7NFAlmC5

**

2. go to tutorial.Build.cs and make it from this

**

https://lh5.googleusercontent.com/yo1i7mWdcxLpQ0AwifGx9IRROUrz-vv_UGK6QIeLe3li0YTq1jDxMni2ZfFXch1j5qocG_d2rSGnq5k0VU-hrB310-9LnoPye1nqfmDwqxtTt6rcXYy4jIOejL4ZAqbHW4d_IqSS

**

into this

**

https://lh4.googleusercontent.com/-N4MZFQW3Jsmn0khhsu9QZjHZx1bOUEHo_veKoKYu2X_yWFZdBq6Xpi5pqPZW5r8vSRF0iznoIC2zvuLL1qfIN7OV0a_3xC_fXrD3P5pTfJ7ZHlxOi52hxQE4YMqnTbRsFWE0hVd

**

3. make tutorial.h to be like this

**

https://lh6.googleusercontent.com/9phw6hceX4jyKXouO56-sRmIVRZrERxuAtoSYmCZfe8T6F9bxjk-iDC1K2L-oSAqkcRVsGJE8sIRiKJ8RV6OulRTyCDOAVHJhW0vXjl0iwyB3DsuIqP3E-hSs2YulczQYnIZC7fz

**

4. do not make tutorial.cpp to be different!!

5. make your blueprint in UE4 like this C++ Classes>Right Click>New C++ Class>Actor, mine is named test

6. do error in C++ game mode Atomic.h "E0018 Expected a')' - UE4 AnswerHub

7. compile to test and see if no bugs, if there are bugs check materiels in beginning of this post

**8. make test.h to be like


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


**#pragma once**


**#include "Engine.h"**

**#include "GameFramework/Actor.h"**

**#include "Runtime/Online/HTTP/Public/Http.h"**

**#include "JsonObjectConverter.h"**

**#include "test.generated.h"**


**USTRUCT(BlueprintType)**

**struct FEstados**

**{**

**GENERATED_BODY()**


**UPROPERTY(BlueprintReadOnly)**

**FString id;**


**UPROPERTY(BlueprintReadOnly)**

**FString key;**


**UPROPERTY(BlueprintReadOnly)**

**FString name;**


**UPROPERTY(BlueprintReadOnly)**

**FString shortname;**


**FEstados()**

**{**

**id = "";**

**key = "";**

**name = "";**

**shortname = "";**

**}**

**};**


**USTRUCT(BlueprintType)**

**struct FMessage**

**{**

**GENERATED_BODY()**


**UPROPERTY(BlueprintReadOnly)**

**TArray<FEstados> states;**


**FMessage()**

**{**

**}**

**};**


**DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMessageBack, FMessage, messageResponse);**


**UCLASS()**

**class TUTORIAL_API Atest : public AActor**

**{**

**GENERATED_BODY()**


**public:**

**// Sets default values for this actor's properties**

**Atest();**

**FHttpModule* Http;**

**UPROPERTY(BlueprintAssignable)**

**FMessageBack Message;**

**UFUNCTION(BlueprintCallable, meta = (DisplayName = "test", CompactNodeTitle = "test", Keywords = "test"), Category = test)**

**void Test();**

**void TestMessage(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);**


**protected:**

**// Called when the game starts or when spawned**

**virtual void BeginPlay() override;**


**public:**

**// Called every frame**

**virtual void Tick(float DeltaTime) override;**


**};**

9. make test.cpp to be like


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


**#include "Public/test.h"**

**#include "tutorial.h"**


**// Sets default values**

**Atest::Atest()**

**{**

**// 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;**

**Http = &FHttpModule::Get();**

**}**


**// Called when the game starts or when spawned**

**void Atest::BeginPlay()**

**{**

**Super::BeginPlay();**



**}**


**// Called every frame**

**void Atest::Tick(float DeltaTime)**

**{**

**Super::Tick(DeltaTime);**


**}**


**void Atest::Test()**

**{**

**TSharedRef<IHttpRequest> Request = Http->CreateRequest();**

**Request->OnProcessRequestComplete().BindUObject(this, &Atest::TestMessage);**

**Request->SetURL("https://raw.githubusercontent.com/dantaex/ciudades_mexico/master/states_mexico.json");**

**Request->SetVerb("GET");**

**Request->SetHeader(TEXT("User-Agent"), "X-UnrealEngine-Agent");**

**Request->SetHeader("Content-Type", TEXT("application/json"));**

**Request->ProcessRequest();**

**}**


**void Atest::TestMessage(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)**

**{**

**TSharedPtr<FJsonObject> JsonObject;**

**TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());**

**if (FJsonSerializer::Deserialize(Reader, JsonObject))**

**{**

**FMessage message;**

**FJsonObjectConverter::JsonObjectStringToUStruct(Response->GetContentAsString(), &message, 0, 0);**

**Message.Broadcast(message);**

**}**

**}**

10. build again, now actor is ready to use!! Put into scene

**

https://lh4.googleusercontent.com/3vjGWrLRgGqJ1L114mOh9do3B3ANNVN9hMd3re3fSYL120zZ6BkPOxozi9l04kZrrYdvj2dx1eoQf14ar9zLK428yjnTqD2jC3LOG-VFufIKvVm26TQckYsCymFsniAnCjwGVRpR

**

11. then use in blueprint like

**

https://lh5.googleusercontent.com/uqG9k2SUTAtQtQg-XpGk34P4zeObgfZluAG54cMCdisl8GU-zQAAexnJMKxnpTLylJRbrMzTx5eRx6toYAqZfEqA8a61gT5qTZx4bZXPaJyrxKDNcyzdyc2JRsoGUkrO9fYXC4Lp

**

OUTPUT


**LogBlueprintUserMessages: [test_C_17] 0, Todo México**

**LogBlueprintUserMessages: [test_C_17] 1, Aguascalientes**

**LogBlueprintUserMessages: [test_C_17] 2, Baja California**

**LogBlueprintUserMessages: [test_C_17] 3, Baja California Sur**

**LogBlueprintUserMessages: [test_C_17] 4, Campeche**

**LogBlueprintUserMessages: [test_C_17] 5, Coahuila de Zaragoza**

**LogBlueprintUserMessages: [test_C_17] 6, Colima**

**LogBlueprintUserMessages: [test_C_17] 7, Chiapas**

**LogBlueprintUserMessages: [test_C_17] 8, Chihuahua**

**LogBlueprintUserMessages: [test_C_17] 9, Distrito Federal**

**LogBlueprintUserMessages: [test_C_17] 10, Durango**

**LogBlueprintUserMessages: [test_C_17] 11, Guanajuato**

**LogBlueprintUserMessages: [test_C_17] 12, Guerrero**

**LogBlueprintUserMessages: [test_C_17] 13, Hidalgo**

**LogBlueprintUserMessages: [test_C_17] 14, Jalisco**

**LogBlueprintUserMessages: [test_C_17] 15, México**

**LogBlueprintUserMessages: [test_C_17] 16, Michoacán de Ocampo**

**LogBlueprintUserMessages: [test_C_17] 17, Morelos**

**LogBlueprintUserMessages: [test_C_17] 18, Nayarit**

**LogBlueprintUserMessages: [test_C_17] 19, Nuevo León**

**LogBlueprintUserMessages: [test_C_17] 20, Oaxaca**

**LogBlueprintUserMessages: [test_C_17] 21, Puebla**

**LogBlueprintUserMessages: [test_C_17] 22, Querétaro**

**LogBlueprintUserMessages: [test_C_17] 23, Quintana Roo**

**LogBlueprintUserMessages: [test_C_17] 24, San Luis Potosí**

**LogBlueprintUserMessages: [test_C_17] 25, Sinaloa**

**LogBlueprintUserMessages: [test_C_17] 26, Sonora**

**LogBlueprintUserMessages: [test_C_17] 27, Tabasco**

**LogBlueprintUserMessages: [test_C_17] 28, Tamaulipas**

**LogBlueprintUserMessages: [test_C_17] 29, Tlaxcala**

**LogBlueprintUserMessages: [test_C_17] 30, Veracruz de Ignacio de la Llave**

**LogBlueprintUserMessages: [test_C_17] 31, Yucatán**

**LogBlueprintUserMessages: [test_C_17] 32, Zacatecas**

IT WORKS!

Thank you and I hope you can understand, this taken me a long time to find out and really is helping in my game hope you can ask any questions here :slight_smile: :slight_smile: :slight_smile: