Is there a better way to loop through all elements of a datatable?

So after much experimentation, I found that I was unable to import a datatable from a CSV file and then use Blueprints to loop through all the elements that were read in through the table. This is because UnrealEngine provides the useful GetDataTableRow node, but I found that I was completely unable to use a ForEach loop node to get all the rows from the table. I WAS able to create a regular ForLoop node and iterate, but only if I hard coded in the number of elements into the loop, since currently there seems to be no way to query the DataTable for its total number of rows?.

I searched the forums for a while and found no information about how to do this.

After considering my dilemma for a bit, I invented the following way to successfully read in all rows of any DataTable read in from a CSV datatable node.
However, it would be nice if say, there was a DataTable node called: GetNumDataTableRows that would return the total number of elements in the table - but that doesn’t exist afaik.

Anyway, I show how to create your own WhileLoop which can easily get the results you need to read in all rows of the DataTable. I hope this helps anyone else out there who ran into the same issue as me. And by all means, if someone has a better way, please post! :stuck_out_tongue:

The ForEach node operates on arrays. DataTables are maps, not arrays, so that node won’t immediately work there.

Of course in C++, you can still use iterators to loop through std::map elements, so maybe we’d either need to alter the ForEach node to also accept maps in additions to arrays, or we could add a DataTable node that gets the table as an array.

The use case here is: I have a DataTable with an unknown number of elements coming in from the CSV file (and can change at any time), and then I’d like to populate a ScrollBox with all the elements to show them in a UI.

The way I set it up above works just fine for those purposes, but it’s less elegant than having some dedicated node functionality to handle iterating through.

STL is not used by any version of the engine. We do have our own iterators, though, so you could use those to implement the node you describe

Hi
First, thanks for your post. I was running into a similar problem with needing to read an element from every row.

I have made a modification to your graph for my own use. Instead of requiring the row name to be a variation of an INT, I have added a new column in my CSV labelled “Next row”. This is the name of the row following it. This is read from the row in each iteration and updates the row name to read next. The final one is filled with END. A String!= for the while loop check is done with each row.

I have also set the while loop “row not found” to break the loop; incase I forget to add the END when updating the table.

1 Like

Nice. That’s a decent solution. When I can find some time, I want to write a way to treat it as an array for automatic ForEach style support

8 Likes

Hi Joe,

Iterators for map still not supported in blueprint… These seem like very basic nodes to have ?

I’ve added the owner of Blueprints so he can weigh in on this

1 Like

Long dead topic, but should anyone else find themselves here in the future, it does look like newer versions of Unreal offer a Blueprint function “Get Data Table Column As String”, which actually gets it as an Array of Strings, which you could then loop over.

1 Like

Thanks!