OpenFL, how to check that file exist?

499 views
Skip to first unread message

Mandemon

unread,
Jul 24, 2013, 4:07:14 PM7/24/13
to haxe...@googlegroups.com
How do I check that given file does exist? Right now, I got following function:

public static function readCharacters(file:String = "")
{
   
var members:Array<Character> = [];
   
var root:Xml = Xml.parse(Assets.getText("init/" + file)).firstElement();
   
//trace(root);
   
return members;
 
}

As you can see, it gets the file from where ever it's called. Now, I know there is a danger here: What if the file does not exist? I tried looking over OpenFL/NME API but I can't find the answer and last time I tried it crashed my program.

So, what is the error return for getText? Is it null or an empty string? I want to know so that if the function gets an file that does not exist, it will return null, which will be interpreted as an error. Once I get that out of the way, I can focus on parsing the interiors and making sure that the file is legit.

Joshua Granick

unread,
Jul 24, 2013, 5:11:21 PM7/24/13
to haxe...@googlegroups.com
It should return null, and print a warning that the file was not found.

You can also use things like "Assets.id" to check for embedded assets, such as "Assets.id.exists ('init/' + file)"

The Haxe API to check if a file exists is FileSystem.exists, but if you're checking for embedded assets, then openfl.Assets should be a better way to go
--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



Mandemon

unread,
Jul 25, 2013, 3:50:12 AM7/25/13
to haxe...@googlegroups.com
Thanks for the answer
Reply all
Reply to author
Forward
0 new messages