Idiots guide to importing CSV files

Hi folks - As it says on the tin, I’m having headaches.

I can’t code for toffee.

I would like to import some test data into my project and then work with it by calling the values stored in it using blueprint. I understand the theory, but not the execution. Here’s my .csv

The output.CSV is called “Example”

,XPtoLevel
1,100
2,200
3,300
4,400
5,500
6,600
7,700
8,800
9,900

And here’s the bit where you wonder “Does he dress himself, or does he get help?”

  1. I’ve added code to Project.
  2. I’ve Created an empty class
  3. My attempt at the code looks like this

/** Structure that defines a level up table entry */
USTRUCT(BlueprintType)
struct FLevelUpData : public FTableRowBase
{
	GENERATED_USTRUCT_BODY()

public:
	FLevelUpData()
		: XPtoLevel(0)
	{}

	/** XP to get to the given level from the previous level */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=LevelUp)
	int32 XPtoLevel;
};

Nothing. I still can’t import. I hate to ask, but is anyone patient enough to hold my hand through this? Once I get the rudiments, I can teach myself, I just can’t get my head around the basics.

Would you consider not using c++ at all?
I managed to load text files through blueprints using Rama’s Victory Plugin and Explode String node.

It’s a tad fiddly, though:

  1. Original data sits in Excel
  2. Excel’s macro adds appropriate separators (can be done manually, ofc)
  3. I then copy a cell range into a plain text file
  4. Load the text file via blueprint and explode it into strings
  5. Exploded strings are fed into variables that are exposed on spawn, creating objects
  6. Objects are added to an array for further use

If you can bear working with a plain *.txt file rather than fancier Excel cells, you may as well skip steps 1, 2 and 3 altogether. I’ve been working with files containing several hundreds of lines of up to 20 columns each and I’ve yet to run into trouble.

If you are interested, I can post the basic setup.

I think I love you.

In a manner that might be considered inappropriate.

It’s definitely worth a try - at least until I can get up to speed on c++.

You will need Rama’s plugin:

The node you are interested in: Load String Array From File

A. This is my Excel file. Vertical bar (|) is used a separator. Since I need commas in the sentences, I use ‘|’ instead of handling comma-separated values.

B. Once copied to a text file, it looks messy.

C. And the blueprint:

D. What happens here.

  1. Loop through the array and explode each line into 7 elements using ‘|’ as a delimiter.
  2. Limit will prevent reading further down the road, 8th element will be skipped in this instance.
  3. You can get one of the 7 strings directly from the array outputted by the explode node.

Let me know if it works as intended! :>

Brilliant!

I’ll post my findings.

  • Choose File > Add C++ to Project
  • From the classes list, choose ‘blueprint function library’ and name it.
  • Visual Studio 2013 opens that file
  • Look at the tabs for the .h file. That’s a header. Ignore the .cpp part.
  • Put in the .h header a struct as shown in the code snippet below.
  • The custom struct matches the columns in your .csv files.
  • Do some copy and pasting of column entries.
  • Press Build, wait, then Save when success.
  • Once you’ve added the struct, when you
    import your .csv it will be shown as
    a valid type to assign and you can call it from blueprints.

Example for a .csv detailing items

Name, Item, UIText, ReqSkill

0, Key, A shiny key!, 1

1, Lock, A rusty lock!, 0

[So far as I know, the column Name and the index numbers come for free!]

 #pragma once
    
    #include "Kismet/BlueprintFunctionLibrary.h"
    #include "**BPFLib**.generated.h" //BPFLib was my name for the Function Library added to the project in UE4
    
    /**
     * 
     */
    
    USTRUCT(blueprintType)
    struct FCSVData_Items : public FTableRowBase  //CSVData_Items is my name for the struct in this case
    {
    	GENERATED_USTRUCT_BODY()
    public:
    	// this is the identifier which allows items to be found in the csv table, must be unique
    	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item")
    		FText Item;
    
    // this is some text for the UIText column.  
    	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item")
    		FText UIText;

    // this is some text for the ReqSkill column.  repeat as required.
    	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item")
    		FText ReqSkill;
    };

Don’t forget you can define multiple structs to handle any number of CSVs.

I assume you’ve already seen the Data Driven Gameplay Elements guide given the name of that struct.

If you’ve got the code set up and building, you should be able to create a Data Table asset using that struct as outlined in that document, and then import your data table CSV data into it. Are you able to get as far as creating your data table?

One other thing that isn’t mentioned in that document is that you can create a User Defined Struct asset and use that as your row data, rather than have to create a struct in C++. This would only be accessible to Blueprints, but if you’re not planning on writing C++ then that won’t be an issue.

You can create a User Defined Struct via the Content Browser by going to “Add New” → “Blueprints” → “Structure”.

Hey, stumbled upon this and it’s really informative. One question though, what exactly are you doing from the “Loop Body” node in the ForEachLoop to get it to store all the information. Having a problem understanding how made that “Create Item”.

Create Item is just a custom function in which I spawn blueprint instances with exposed vars.

I can no longer suggest the above-mentioned method. Structs and data tables are fairly mature now and can efficiently communicate with external *.csv files.

Everynone, thank you for the great work here even if you feel it’s deprecated.

Is there a way to communicate with external csv files in real time while playing through blueprints like you have done here? Everything I have seen either has you import it manually in the editor or using custom c++ classes.

I just noticed this, and thought I would mention that if you QUOTE your strings, you don’t have to worry about using commas.
Ie: inside your data row “This,Line,Has,Commas” when you export in csv format, as long as the .txt says
“This,Line,Has,Commas”,32,24
the string will be parsed as one.