0
\$\begingroup\$

How can I store dialogue so that it could be read for a kinetic (linear) Visual Novel type of game in XNA/MonoGame?

I'm currently using a .txt file, but I noticed that I can't read from a specific line using the System.IO.StreamReader class.

\$\endgroup\$
  • \$\begingroup\$ The irony. An Alex helping another Alex. \$\endgroup\$ – The Mattbat999 Jul 2 '18 at 14:33
0
\$\begingroup\$

You'll want to use TitleContainer.OpenStream in place of the stream reader.

The reason they do it differently in XNA / MonoGame is for cross platform compatibility. Essentially, the TitleContainer abstracts away the details about how to read the file on whatever platform you're targeting.

There's also two other things you'll need to be aware of:

  1. When you're loading raw content that isn't processed with the Content Pipeline tool, you'll need to set the properties of the file in Visual Studio to Content and Copy if newer.
  2. You'll need to use the path and file extension (not just the name like you would with Content.Load

For example:

using (var stream = TitleContainer.OpenStream("Content/data.txt"))
using (var reader = new StreamReader(stream))
{
    // Do your thing here as you normally would
}
\$\endgroup\$
  • \$\begingroup\$ Thanks! That explains a lot. I didn't know about the existence of TitleContainer beforehand. \$\endgroup\$ – Alexandre Sep 4 '17 at 14:45
  • \$\begingroup\$ Now I'm only curious if it's required to use the "using" keyword. \$\endgroup\$ – Alexandre Sep 4 '17 at 15:07
  • \$\begingroup\$ The using statement is shorthand for a try finally block. It's designed to safely dispose of the stream when it's no longer in use even if exceptions are thrown. In some circumstances you might want call Dispose yourself (e.g. if you want to hang on to a reference to the stream for more than one method call) but designing your code this way often makes things more complex. My advice, start with the simplest thing that works. \$\endgroup\$ – craftworkgames Sep 5 '17 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.