Loading data from File objects

16 posts

Flag Post

I’m working on a new game based on a project I am helping someone with in Java. It’s a word-based game, and requires loading a large dictionary. In Java, I put the dictionary in a .txt file, create a File object pointing to it, and load it up with FileReaders/Scanners to put it into a data structure in my file.

I’m re-creating the project in actionscript, and there is an important difference between that project and the java one. The Java project only had to run locally, so I had some amount of control over where to put the txt file and where to find it to access it. I have no idea how to do that, and Kong only takes an .swf file. How can I get my dictionary loaded so that I have a list of words to use in my game?

 
Flag Post

The easiest way would be to embed the file into your .swf
You can use the embed meta tag for that.

If you want to keep the file external, you have to host it somewhere on the interwebs and load it at runtime.

 
Flag Post

So you can embed files the same way you can embed images? I did not know that; I’m still getting used to actionscript. Thank you!

 
Flag Post
Originally posted by trashantboy:

So you can embed files the same way you can embed images? I did not know that; I’m still getting used to actionscript. Thank you!

images are files ;)

 
Flag Post

Problem #2: I can’t import flash.filesystem so that I can get access to the File object. I’m using actionscript 3 in FlashDevelop. I have no idea why I can’t import this particular object.

So, is there a way to either import this even though it isn’t showing up in my library? Or, barring that, is there a way to access the information from a text document without using a File?

 
Flag Post
Originally posted by trashantboy:

Problem #2: I can’t import flash.filesystem so that I can get access to the File object. I’m using actionscript 3 in FlashDevelop. I have no idea why I can’t import this particular object.

This package is only available for the Air runtime. So if you were to publish for desktop applications, you could use this.
But as you target FlashPlayer, because you want to publish on kong, this is not accessible to you.

I’m not sure why you want to use that package now.

Originally posted by trashantboy:

So, is there a way to either import this even though it isn’t showing up in my library? Or, barring that, is there a way to access the information from a text document without using a File?

Yes, I mentioned it alread: embed the file with the embed meta tag.

 
Flag Post

Honestly, I just thought that I had to use it to access a file. That’s what we have to do in Java, and I’m developing this program concurrently in both languages for practice.

How do I access the information from a text file if it’s embedded? With images, I have to create a BitMap variable and assign the image to it. I thought I would have to do something similar with the text file, and I just assumed it would have to be a File object. Can I just assign it to a String, or a String array, or something?

Edit: After a bit more research, it looks like I can use a URLLoader? How does that work with an embedded file?

 
Flag Post
Originally posted by trashantboy:

Edit: After a bit more research, it looks like I can use a URLLoader? How does that work with an embedded file?

No. Loaders (down)load external stuff. rtfm please:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLLoader.html

Originally posted by trashantboy:

I have to create a BitMap variable and assign the image to it. I thought I would have to do something similar with the text file


You do.
You instantiate the class you associate with the embedded asset with and read the entire UTF bytes from this ByteArray.

 
Flag Post

You definitely want to go with embedding the file. It embeds at compile-time, so you won’t need to upload a separate file to Kong. The formats that would work best for you are either XML or JSON. Here’s some sample code for embedding an XML file (JSON would be a little different):

[Embed(source="atlas.xml", mimeType="application/octet-stream")]
public static const AtlasXml:Class;

var xml:XML = XML(new AtlasXml());

 
Flag Post

Thanks for the answer, both of you. Just to be clear, I’m going to need to convert the text document into xml (or JSON), then embed the file as xml. Then I can use the file directly, because actionScript has an xml object type that I can use to access it directly. Is that correct?

If so, is there an easy way to convert a text file (strings separated by newline characters) into an xml file?
Edit: It looks like Microsoft Word has a way to convert .txt files directly into xml, so that should work nicely.

 
Flag Post

Actually, it looks like I don’t even have to bother. According to this page it’s possible to embed the entire .txt file, which I can then parse out based on newline byte characters. Even with 60,000 words, the .txt file is only .5mb, which means it shouldn’t take up too much room in my final .swf, even after parsing and storing as strings. I hadn’t realized that embedded it as a ByteArray which can be accessed directly. No need for File objects like you would in Java, or even xml (65mb for the same list)

 
Flag Post

Final update: I didn’t even have to loop through the ByteArray. Final code to include a text file consisting of 60000+ words into my swf looks like this:

var test:String = new Assets.Dictionary().toString();
var words:Array = test.split(“\n”);

This gives me easy and quick access to the full list of words. Storing the entire list of 60000 words and accessing one of them in a trace took less than a second. The compressed .swf file is only 200kb at this point. Granted, that will increase when it’s more than just storing an array of strings, but this ended up being MUCH easier than I was making it.

Thank you both for all the help. It was what you said that got me pointed in the right direction. I really appreciate it.

 
Flag Post

It sounds like your solution will work even better than using XML, glad you were able to figure it out. Good luck on your game!

 
Flag Post
Originally posted by trashantboy:

Actually, it looks like I don’t even have to bother. According to this page it’s possible to embed the entire .txt file

Secrets of the Programming Industry: .xml files are actually text files, except with a different file extension and with the text formatted in a specific way. If you can embed a .xml file, you can embed a .txt file.

Just don’t call the XML() function on something that isn’t formatted as an xml file.

 
Flag Post

Could you not just copy the contents of the text file and paste it into a new class as a String? Saving you all the effort to load/embed/parse it.

 
Flag Post

That wouldn’t save you the effort of parsing it.

And if you wanted to edit it, you’d have to make sure the strings were properly formatted. If you wanted a newline, you’d have to type out \n, end the string, add a +, and start a new string on the next line. And then you’d have to escape any quotes or backslashes.

On the other hand, if you’re using OpenFL, this is as easy as Assets.getText("assets/text/Dictionary.txt").split("\n").