Reading Text File From Webserver

I realize this has been discussed in several locations. Believe me, I’ve read them all… twice.

I’m trying to build a webplayer that reads text files and displays the text.

I have the text files saved on my webserver.
I have the webplayer files saved on my webserver.
I have a crossdomain xml file saved on my webserver.

I then have this:

tw.dataFile = LoadTextData("www.pillar4games.com/MK_Data/Data/SpeciesData.txt");

(I hard coded the file paths when Application.dataPath wasn’t working.)

the LoadTextData looks like this:

public string LoadTextData(string url)
    {
        www = new WWW(url);
        while (!www.isDone)
        {
            Debug.Log("Loading...");
        }
        return www.text;
    }

This all works just fine in the editor, but when I run the webplayer (both from my computer, dropbox and from the webserver) it freezes and then crashes the webplayer.

NOTE: I have also tried having “http://” in front of the filepath, but got the same result.

Can anyone lend me a hand with this? Thanks!

What is tw.dataFile? A string?

Yes. Its a string variable in a script called TitleWindow.

Tried putting it into a coroutine?

…no? Why?

Like I said, it works in the editor. The only issue I’m running into is trying to retrieve the files from the webserver.

Frustrated bump. Anyone have experience with this?

Seems strange that it cannot load it really.

Another approach could be to use Resources. Load(textfile). Then you get the file as a TextAsset. Then you can set tw.dataFile = TextAsset.text;
Not sure if this will work, but guess it should.

It may be because of the while loop. If it takes too long to load it can crash your player. I think that is why BFGames suggested a Cououtine. Its essentially a Thread.

I think I had originally done that, but that was while using streamreader. Had to do away with that when I realized streamreader couldn’t be used on the web. Not looking forward to rewriting all of that, only to discover it doesn’t fix it… :confused:

I’ll keep tinkering. Anyone else with some thoughts on why I would be hitting this brick wall?

Ah, I see. It shouldn’t take long though. In the editor, it takes 1 frame. They’re small text files, which should load almost instantly (even if you were on a dial-up connection).

EDIT: Of course, the moment I say this, I start getting the error “Could not connect to host” when running in the editor…

Maybe its my crappy ISP? Someone do me a favor, and check out this link. Tell me what you see.

This really should be put in a coroutine. I assume you’re calling this from within a Start or Awake method somewhere. The editor is a special environment and should not be treated as “if it works here, it should work there”, especially when dealing with a different platform such as a web player.

What happens is if it is in a Start or Awake method, the whole start up is blocked by your while loop. And if there is an error (such as missing the protocol portion of the URL, or some other error) your while never exits.

Put it in a coroutine, yield the www and then check if there is an error or not. Almost exactly as it is in the script reference.

Haha. Isn’t that just the epitome of being a programmer though? I would definitely put it in a coroutine, then see it that works. Which it should :slight_smile:

Edit:
Btw, when I try and play the player it stops responding and a popup asks if I want to end the process. So that means it is getting stuck in a loop I presume.

Ok, I’ll put it all into a coroutine. My only concern is that a large portion of the program depends on that content being there. If I allow the rest of the program to execute before the content is loaded, I’m afraid it will cause all kinds of problems.

Of course. The coroutine “couldn’t be started”. FML

What I would do is use a public boolean on the script. Make the rest of your program wait for the boolean to be true or maybe execute all of them after it loads? I personally would use the boolean method.

Put them in coroutines. Same result.

It should also be noted that running it as a PC standalone (exe) works just fine (still pulling the text files from the site).

This is really confusing because I used this code:

	// Use this for initialization
	void Start () {
		float t_n = Time.time;
		string txt = LoadTextData("www.pillar4games.com/MK_Data/Data/SpeciesData.txt");
		Debug.Log("Took about : " + (Time.time - t_n) + "ms to get text");
		Debug.Log(txt);
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public string LoadTextData(string url)

    {

        WWW www = new WWW(url);

        while (!www.isDone)

        {

            Debug.Log("Loading...");

        }
        return www.text;

    }

And it worked perfectly fine in my scene.

The only other thing I can think of is the fact that you are trying to put it into a TextAsset? Cause it works fine with a string.

Did you run it in a webplayer though? It all works perfectly fine for me from the editor and from an exe, just not the webplayer. Also, I’m not using TextAsset, its all just string variables. :frowning:

Hmmm… this is really weird. I did this code:

using UnityEngine;
using System.Collections;

public class textget : MonoBehaviour {
	
	string txt = "getting text...";
	
	// Use this for initialization
	void Start () {
		float t_n = Time.time;
		txt = LoadTextData("www.pillar4games.com/MK_Data/Data/SpeciesData.txt");
		Debug.Log("Took about : " + (Time.time - t_n) + "ms to get text");
		Debug.Log(txt);
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public string LoadTextData(string url)

    {

        WWW www = new WWW(url);

        while (!www.isDone)

        {

            Debug.Log("Loading...");

        }
        return www.text;

    }
	
	public void OnGUI()
	{
		GUI.Label(new Rect(0,0,500,500),txt);
	}
}

and it did the same thing when I built it in a webplayer. Is there like some weird way to do WWW on a webplayer or something?

Also tried to build with the code in a webplayer and crashed… Something in the policy not allowing a used port or something? Im out of ideas now. Try the TextAsset though

Check: http://lasseknudsen.dk/WebPlayer/WebPlayer.html

Works fine…

   string teststring = "";
    bool loaded = false;

    void Start()
    {
        TextAsset ta = (TextAsset)Resources.Load("Text");
        teststring = ta.text;
        if (teststring != "")
        {
            loaded = true;
        }
    }

    void OnGUI()
    {
        if (loaded)
        {
            GUI.Label(new Rect(0, 0, Screen.width, Screen.height), teststring);
        }
    }