3

I have a project that I am building using Unreal Engine 4, and I am now on to localizing the strings; I have all of the data for each localization in JSON form in text files that I have added to the project's source.

The file structure looks like this in my Solution Explorer:

Games
    MyProject (Target)
        Source
            MyProject
                Localization
                    LOC_ES.txt
                    ...
                Accessor.h
                Accessor.cpp

I added the files to that directory by

  1. Right click on MyProject (the second one) and click add -> new filter
  2. Right click on the Localization filter and click add -> existing item
  3. Select all of the txt files from some other directory on my PC.

I'm not sure, first off, if that's the proper way to add files to a project.


I followed the instructions in this post, but I am getting this error from the engine when I attempt to run this code:

First-chance exception at 0x00007FFBF1B68B9C in UE4Editor.exe: Microsoft C++ exception: std::length_error at memory location 0x000000C38BB2A0C0.

Here is the code as I have entered it. I have tried referencing the text file both with "LOC_ES.txt" and "Localization/LOC_ES.txt" and both give me this error.

std::ifstream t(TCHAR_TO_UTF8(*("Localization/LOC_" + locale + ".txt")));
std::string str;

t.seekg(0, std::ios::end);
str.reserve(t.tellg());
t.seekg(0, std::ios::beg);

str.assign((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());

The actual contents of this file are just JSON. Here is an excerpt.

{
    "Single Player":    "Solo Jugador",
    "Multiplayer":      "Multijugador",
    "Options":      "Opciones",
    "Exit":         "Dejar",
    "Back":         "Volver",

    "Normal":       "Normal",
    "Hard":         "Duro",
    "Brutal":       "Extremo"
}

I have never read from a file this way before, so I am not sure what exactly is the problem.

Community
  • 1
  • 1
David
  • 21,070
  • 7
  • 57
  • 113
  • 1
    Have you debugged the code to see where the exception actually happens? – TobiMcNamobi May 19 '15 at 08:19
  • I'm not entirely sure how to do that with Visual Studio and Unreal Engine. – David May 19 '15 at 08:20
  • Set a breakpoint at the declaration of `std::ifstream t`. To do that click on the left border of the text editor (as an alternative you move the cursor to the declaration and choose Debug -> Toggle Breakpoint from the menu bar). Then in the menu bar select Debug -> Start Debugging. – TobiMcNamobi May 19 '15 at 08:31
  • @TobiMcNamobi Okay so I have determined that the problem is the file path. If I use the absolute file path to the file on my system, it works fine, but I need the file to be treated as a resource so that it is not dependent on my system's file system structure. How can I add the file directly to the project and access it so I don't have to use the actual `C:\path\to\file.txt` – David May 19 '15 at 08:39
  • BTW, the spanish translation is bad ;) – sbenitezb Jun 05 '15 at 16:18

2 Answers2

1

As you wrote in the comments the problem is the file path. My advice for correctly adding the file in Visual Studio is this:

  • First, right-click LOC_ES.txt and chose Remove. Take care that you do not delete the file from the file system.
  • Copy the file in question to a folder inside your project.
  • Add the file as an existing item as you have done before but now chose the file inside the project folder.

Then you should use a relative file path (depending on where the executable is built).

TobiMcNamobi
  • 4,305
  • 2
  • 29
  • 51
  • I figured it out! Basically I had to put it in the Content folder of the project itself, as that gets copied to the binary. I can then use the built in `FPaths::GameDir()` to get the relative path to the exe's root directory and then navigate directly to the file. Woohoo! – David May 19 '15 at 09:07
0

Probably you provide the stream constructor with a relative path that is not matching the location of your resource file. That's what the error message might say: "I find no stream, so I can not start seeking at position 0." Have a look at the build/debug target where your executable is located. Then find the relative path from the executable to the resource and use this. Hopefully this will work.

I answer this as a c++ programmer, but have no knowlegde about the unreal-engine4. It might be possible that the resource files (Localization/ LOC_ES.txt, ...) are not copied to the resulting runtime, i.e. the scope of the executable.

Peter Paul Kiefer
  • 2,042
  • 1
  • 9
  • 15