# UE doesn't like my TOptionals...

**URL:** <https://forums.unrealengine.com/t/ue-doesnt-like-my-toptionals/1166248>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, CPP, question\
**Created:** [May 15, 2023, 8:24am UTC](https://forums.unrealengine.com/t/ue-doesnt-like-my-toptionals/1166248 "2023-05-15T08:24:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scatter](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scatter/32/637809_2.png) [@Scatter](https://forums.unrealengine.com/u/Scatter)\
**Post date:** [May 15, 2023, 8:24am UTC](https://forums.unrealengine.com/t/ue-doesnt-like-my-toptionals/1166248/1 "2023-05-15T08:24:17Z")

</div>

The code atm:

```auto
    UFUNCTION(BlueprintCallable, Category = "SQLite")
        static void UpdateTileID_Row(
            int32 TileQ,
            int32 TileR,
            int32 TileS,
            TOptional<float> VecX,
            TOptional<float> VecY,
            TOptional<float> VecZ,
            AActor* Store,
            TOptional<int32> Row,
            TOptional<int32> Col
        );

```

```auto
void USQLiteDB::UpdateTileID_Row
    (
        int32 TileQ,
        int32 TileR,
        int32 TileS,
        TOptional<float> VecX,
        TOptional<float> VecY,
        TOptional<float> VecZ,
        AActor* Store,
        TOptional<int32> Row,
        TOptional<int32> Col
    )
{
    // Open the database
    sqlite3* db;
    FString dbPath = FPaths::ProjectContentDir().Append("Database/Current.btf");
    int rc = sqlite3_open(TCHAR_TO_UTF8(*dbPath), &db);
    if (rc != SQLITE_OK)
    {
        // Handle error
        return;
    }

    // Construct the SET clause
    FString setClause;
    if (VecX.IsSet())
        setClause.Append(FString::Printf(TEXT("VecX = %f,"), VecX.GetValue()));
    if (VecY.IsSet())
        setClause.Append(FString::Printf(TEXT("VecY = %f,"), VecY.GetValue()));
    if (VecZ.IsSet())
        setClause.Append(FString::Printf(TEXT("VecZ = %f,"), VecZ.GetValue()));
    if (Store != nullptr)
        setClause.Append(FString::Printf(TEXT("Store = '%s',"), *Store->GetName()));
    if (Row.IsSet())
        setClause.Append(FString::Printf(TEXT("Row = %d,"), Row.GetValue()));
    if (Col.IsSet())
        setClause.Append(FString::Printf(TEXT("Col = %d,"), Col.GetValue()));
    setClause.RemoveFromEnd(",");

    // Prepare the UPDATE statement
    FString sql = FString::Printf(TEXT("UPDATE TileIdentity SET %s WHERE TileQ = %d AND TileR = %d AND TileS = %d"), *setClause, TileQ, TileR, TileS);
    sqlite3_stmt* stmt;
    rc = sqlite3_prepare_v2(db, TCHAR_TO_UTF8(*sql), -1, &stmt, nullptr);
    if (rc != SQLITE_OK)
    {
        // Handle error
        sqlite3_close(db);
        return;
    }

    // Execute the UPDATE statement
    rc = sqlite3_step(stmt);
    if (rc != SQLITE_DONE)
    {
        // Handle error
        sqlite3_finalize(stmt);
        sqlite3_close(db);
        return;
    }

    // Finalize the statement and close the database
    sqlite3_finalize(stmt);
    sqlite3_close(db);
}

```

Gives the following compile error in the editor: _Error: Unable to find ‘class’, ‘delegate’, ‘enum’, or ‘struct’ with name ‘TOptional’_ for each line where the TOptional is declared…

I did some searching of the internets and added:

```auto
#include "Optional.h"

```

to my header. File unknown, so even further searches had me adding to my build.cs:

```auto
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; //existing

		PublicIncludePaths.Add("Runtime/Core/Public"); //new-line

		PublicDependencyModuleNames.AddRange(new string[] //existing
		{ //etc

```

I’m now at an absolute loss… any ideas?

---

<div class="post-metadata">

**Author:** ![Extrone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/extrone/32/906554_2.png) [@Extrone](https://forums.unrealengine.com/u/Extrone)\
**Post date:** [May 15, 2023, 10:28am UTC](https://forums.unrealengine.com/t/ue-doesnt-like-my-toptionals/1166248/2 "2023-05-15T10:28:21Z")

</div>

> [@Scatter](#):
>
> `#include "Optional.h"`

according to the [Documentation](https://docs.unrealengine.com/5.2/en-US/API/Runtime/Core/Misc/TOptional/) it’s

```auto
#include "Misc/Optional.h"

```

---

<div class="post-metadata">

**Author:** ![Scatter](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scatter/32/637809_2.png) [@Scatter](https://forums.unrealengine.com/u/Scatter)\
**Post date:** [May 15, 2023, 11:06am UTC](https://forums.unrealengine.com/t/ue-doesnt-like-my-toptionals/1166248/3 "2023-05-15T11:06:44Z")

</div>

Well that fixed the file unknown error… 👍

However the main error still remains - it doesn’t like the TOptionals (or at least, how I’ve used them).

---

<div class="post-metadata">

**Author:** ![Extrone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/extrone/32/906554_2.png) [@Extrone](https://forums.unrealengine.com/u/Extrone)\
**Post date:** [May 15, 2023, 11:41am UTC](https://forums.unrealengine.com/t/ue-doesnt-like-my-toptionals/1166248/4 "2023-05-15T11:41:21Z")

</div>

Does the error remain if you remove UFUNCTION()? if it doesn’t, it’s most probably caused by Blueprints not supporting TOptionals.

[EDIT]  
Just did some testing, it’s only showing an error if I mark the function as UFUNCTION().  
So yeah, Blueprints doesn’t support TOptional.

---

<div class="post-metadata">

**Author:** ![Scatter](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scatter/32/637809_2.png) [@Scatter](https://forums.unrealengine.com/u/Scatter)\
**Post date:** [May 15, 2023, 11:52am UTC](https://forums.unrealengine.com/t/ue-doesnt-like-my-toptionals/1166248/5 "2023-05-15T11:52:39Z")

</div>

Serious? That’s pretty limiting…

Appreciate your effort @Extrone

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [May 16, 2023, 11:52am UTC](https://forums.unrealengine.com/t/ue-doesnt-like-my-toptionals/1166248/6 "2023-05-16T11:52:50Z")

</div>

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

---

<div class="post-metadata">

**Author:** ![Hourences](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hourences/32/1057442_2.png) [@Hourences](https://forums.unrealengine.com/u/Hourences)\
**Post date:** [May 18, 2023, 7:52am UTC](https://forums.unrealengine.com/t/ue-doesnt-like-my-toptionals/1166248/7 "2023-05-18T07:52:25Z")

</div>



---

<div class="post-metadata">

**Author:** ![Scatter](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scatter/32/637809_2.png) [@Scatter](https://forums.unrealengine.com/u/Scatter)\
**Post date:** [May 18, 2023, 10:22am UTC](https://forums.unrealengine.com/t/ue-doesnt-like-my-toptionals/1166248/8 "2023-05-18T10:22:36Z")

</div>

So, I’ve done some testing and mucking around and it turns out you _can_ use TOptionals with callable Blueprints, it’s just unecessarily hacky and long winded.

The solution is either a nested private function or sentinel variables. Here’s the blueprint and the returned setclause statement in yellow, bottomleft:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/e/7/ee782dc44534f03746e10063be461903c2058d03.png)

The header code:

```auto
	UFUNCTION(BlueprintCallable, Category = "Database")
	static void UpdateReferenceRow
	(
		UPARAM(ref) FIntVector& TileID,
		int32 EmpireID = -1,
		int32 IncidentID = -1,
		int32 NebulaID = -1,
		int32 StellarID = -1
	);

```

Sauce:

```auto
void UQDS_ReferenceTable::UpdateReferenceRow(UPARAM(ref) FIntVector& TileID, int32 EmpireID, int32 IncidentID, int32 NebulaID, int32 StellarID)
{
	// Pass on the IntVec values
	int32 TileQ = TileID.X;
	int32 TileR = TileID.Y;
	int32 TileS = TileID.Z;

	TOptional<int32> E_ID_Value;
	TOptional<int32> I_ID_Value;
	TOptional<int32> N_ID_Value;
	TOptional<int32> S_ID_Value;

	if (EmpireID != -1)
	{
		E_ID_Value = EmpireID;
	}
	
	if (IncidentID != -1)
	{
		I_ID_Value = IncidentID;
	}

	if (NebulaID != -1)
	{
		N_ID_Value = NebulaID;
	}

	if (StellarID != -1)
	{
		S_ID_Value = StellarID;
	}

	// Construct the SET clause
	FString setClause;
	if (E_ID_Value.IsSet())
		setClause.Append(FString::Printf(TEXT("EmpireID = %d,"), E_ID_Value.GetValue()));
	if (I_ID_Value.IsSet())
		setClause.Append(FString::Printf(TEXT("IncidentID = %d,"), I_ID_Value.GetValue()));
	if (N_ID_Value.IsSet())
		setClause.Append(FString::Printf(TEXT("NebulaID = %d,"), N_ID_Value.GetValue()));
	if (S_ID_Value.IsSet())
		setClause.Append(FString::Printf(TEXT("StellarID = %d,"), S_ID_Value.GetValue()));
	setClause.Append(FString::Printf(TEXT("TileQ = %d,"), TileQ));
	setClause.Append(FString::Printf(TEXT("TileR = %d,"), TileR));
	setClause.Append(FString::Printf(TEXT("TileS = %d,"), TileS));
	setClause.RemoveFromEnd(",");

	UE_LOG(LogTemp, Warning, TEXT("Set Clause: %s"), *setClause);
	UE_LOG(LogTemp, Warning, TEXT("Yes, you are here."));
}

```

It really should be much more straightforward to see if a blueprint node input pin has had a value passed through it, but this definitely works (and has a multitude of uses)…

Oh, and a shoutout to @Hourences for reopening the thread. 😃

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [June 17, 2023, 10:22am UTC](https://forums.unrealengine.com/t/ue-doesnt-like-my-toptionals/1166248/9 "2023-06-17T10:22:45Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
