Is there a better way to search within a data table?

Hi all!
I’m new to unreal, but not to programming.
I’m building this text-based adventure where the player can freely type into a text box and get a response in a different one.
I have a data table to hold my possible inputs and responses.

I’ve built this function that would take the input from the user, and - if it’s not an empty string - loop through my possible inputs and if it finds a match, it breaks the loop, grabs the row number of the proper response and set the text in the response window.
If it cannot find the input, it shows the default ‘I don’t know what you mean’ message.

Here is a screen shot of my function:

This works, but it feels a bit clumsy.
I guess I’m thinking of SQL, but if I had to write this in a rational DB, I wouldn’t want to loop through everything.
My data table contains both possible inputs and possible responses, distinguished by a column called “speaker”.
Is there a way for me to tell unreal to only look through those rows where the column “speaker” equals “user”?
Or should I just create two different data tables for possible inputs and corresponding responses?

Or is my strategy completely wrong and there’s a better way of doing it?

Any word of wisdom or advice from the more experienced would be greatly appreciated! :slight_smile:

Thanks!

Hi razkoller,

As you are new to unreal but not to programming, as a general advice you might have a better time wrapping your head around the Unreal C++ codebase, and then implementing functionality like this in C++. The majority of blueprint functionality is just C++ functionality that has been exposed to blueprints.

In the case of the UDataTable, as with most classes in Unreal, the blueprint functionality exposed from C++ to blueprints can be limited. In C++, there is the template function FindRow(), which would avoid you having to loop through the elements of a DataTable yourself.

If your Input->Response Text is a 1:1 relationship, you perhaps don’t need a DataTable, and could instead use a TMap<FText (UserInputCase), (NameOfYourResponseStruct)> (in blueprints create a Text variable, and there should be an icon next to it to allow you to change it to a map).
This means you could directly use your User Input as the search Key for the map, and the returning Value would contain what the response should be to this User Input.

Underneath the hood, A UDataTable is essentially a TMap<FName (RowName), T (RowStructType)>. This means while you could do a similar thing with checking RowName matches User Input, if you plan to localize your game this method will no longer work (An FName is not localizable).

Hope this helps :slight_smile:

2 Likes

Thank you @KyleRyanBlowfish for that detailed response and suggestions!

I’m a web developer, and I was hoping Blueprints would make it so I could develop in Unreal without C++, but I guess there’s no escaping it if you want more control and flexibility…
Time for a C++ course! :grin:

There are also plugins to utilize MySQL for example, if that would be better.

1 Like

Thanks @Astaraa!
I did look into it, but most plugins I could see on the marketplace were designed to connect to an online SQL/MYSQL server, which means the game has to be played online.
I really want an offline solution.

If you know a plugin like that please do let me know. I’m much more comfortable speaking in SQL than in Blueprints… :laughing: