x


How do I write a line of text to a file?

How do I write a line (or four) to a text file? I've looked all over the place but everything I can find is either in C# or is too specific. The documentation has next to nil on the subject.

What I'm trying to do is to take the output of four separate text boxes that specify the stats for an enemy and write them into a file. Then, I'd like to load the stats for that enemy at a later point. How can I do this?

Thanks in advance - Elliot B.


EDIT: I'd like the output to look like this:

Troll 14 5 5 50

I won't go into the specifics, but I need the name, strength, health, and whatnot. I'd also like to be able to read that back in. NOTE: This is not a 'write-my-code' problem. I've seen the warm welcome those receive and would not like to be facing the business end of some of my own comments. I just need a way to do it, because I can't find any documentation. Thanks again.


EDIT 2: I guess I'd better go ahead and explain what I'm trying to do. Okay. So, I've been playing some paper RPG games, and thought it would be cool if I could create a sort of 'fighting calculator'. So I went ahead and made one. Then I thought: "What if I could make it so the user could create his own monsters instead of me hardcoding each one?" Which looks like:

if (enemyName == "Common Orc") {
    ea1 = 5;
    ea2 = 6;
    enemyDefense = 8;
    enemyHealth = 35;
}

For each enemy. So I'm working on a way to get the player input for when he hits 'create new monster' and then types in the variables for it. My program would, theoretically, write the variables to a file, and then read them out again if I needed to use (fight) that monster. Does that make sense?

That's why I don't know if PlayerPrefs would work for this. Would it?

more ▼

asked Jul 28 '10 at 03:01 PM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

and what is the specific problem with c#?

Jul 28 '10 at 03:18 PM sovalopivia

I don't do C#. I'm a javascriptonian, through and through.

Jul 28 '10 at 03:19 PM e.bonneville

you're limiting yourself :(

Jul 28 '10 at 03:22 PM sovalopivia

Have you done javascript?

Jul 28 '10 at 03:27 PM e.bonneville
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You can use System.IO.File.WriteAllText(filePath, yourString); ( http://msdn.microsoft.com/en-us/library/system.io.file.writealltext.aspx ) but honestly I'd stay clear of file IO unless you have to

Edit - you'll more likely want AppendAllText instead of WriteAllText if you're going to be adding them

Specific to your question:

System.IO.File.AppendAllText(yourPath, System.String.Format("{0} {1} {2} {3} {4}", enemyName, ea1, ea2, enemyDefense, enemyHealth));

more ▼

answered Jul 28 '10 at 03:20 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 253

Not really. It might work, but really what I'm trying to do is... well, I'll edit my Question again. Hang on.

Jul 28 '10 at 03:27 PM e.bonneville

Ah, thanks. Hang on, let me give that a shot. Sorry - one more n00b question: What's the {0} {1} {2} {3} {4} for?

Jul 28 '10 at 03:38 PM e.bonneville

it formats the order the passed variables will appear in the string, in this case it would be in the order they appear, if you did {4} {3} {2} {1} {0} it would be reversed.

You can also format it like this "Name '{0}', Ea1 '{1}' etc

Jul 28 '10 at 03:50 PM sovalopivia

You may need to add a trailing newline onto it to make it work (i.e. {0} {1} {2} {3} {4}n ), but that's a minor issue which will be obvious if it happens ;)

Jul 28 '10 at 03:54 PM Mike 3

Alright. I plopped the code in. Now it's giving me this error: Assets/Custom Assets/Scripts/FightingCalculator.js(146,73): BCE0044: expecting :, found '}'.

Jul 28 '10 at 03:56 PM e.bonneville
(comments are locked)
10|3000 characters needed characters left

I belierve theres JS specific info about this here

http://forum.unity3d.com/viewtopic.php?t=15313

more ▼

answered Jul 28 '10 at 03:19 PM

sovalopivia gravatar image

sovalopivia
393 2 6 15

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3462
x242

asked: Jul 28 '10 at 03:01 PM

Seen: 10785 times

Last Updated: May 07 '12 at 04:44 PM