x


Best way to load the position of my player?

So I have no trouble saving the position of my player to the text file and it comes out looking something like this:

-1256.434
24.50337
1644.17

But now I need to load that back into the game and spawn the player at that position, I know I need to convert each line into a float then assign it to x,y,z of the player but I'm not sure how to read it line by line.

function ReadFile(filepathIncludingFileName : String) {

    sr = new File.OpenText(filePath);

    input = "";
    while (true) {

       input = sr.ReadLine();

       if (input == null) 
       { 
       break; 
       }
    }
    sr.Close();
}

Thats the code being used to read the text file but from there I'm not sure how to read the first line, assign it, read the second line, assign it then read and assign the third line.

Any help would be appreciated.

Cheers.

more ▼

asked Aug 11 '11 at 08:22 AM

Tides gravatar image

Tides
31 15 19 21

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Golly gosh I need to think more before I post these - I must look like an idiot.

To read and assign the position all I needed to do was this:

   function ReadFile(filePath : String) {

    sr = new File.OpenText(fullPath);



    gameObject.Find("Player").transform.position.x = parseFloat(sr.ReadLine());

    gameObject.Find("Player").transform.position.y = parseFloat(sr.ReadLine());

    gameObject.Find("Player").transform.position.z = parseFloat(sr.ReadLine());



    sr.Close();

}

That reads each line and converts the string to a float then puts the player in that position.

more ▼

answered Aug 11 '11 at 08:32 AM

Tides gravatar image

Tides
31 15 19 21

(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:

x297
x133
x8
x3

asked: Aug 11 '11 at 08:22 AM

Seen: 609 times

Last Updated: Aug 11 '11 at 08:44 AM