how to determine number of level-files in directory??

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

Re: how to determine number of level-files in directory??

Postby BurningHand » Fri Apr 27, 2012 2:10 pm

Being honest, your requirements seem at odds with the Internal FileType. Unless you plan to repackage all of your users' level packs into the release, you probably want to look at using the local or external type (and then download level packs or allow the user to download and place them into the right spot). The directory operations should work just fine on local and external files (even on Android).

At that point you can decide if you expect all items in the levelpacks folder to be subfolders, then you can just list() the levelpacks folder to get them (otherwise you'd have to iterate over them and see if they are directories).
IRC: nexsoftware / mobidevelop; GitHub: MobiDevelop;
BurningHand
 
Posts: 2262
Joined: Mon Oct 25, 2010 4:35 am

Re: how to determine number of level-files in directory??

Postby wolfieee » Sun May 20, 2012 2:00 am

Code: Select all
String dirBase="sounds/animals";
      String animals = Gdx.files.internal(dirBase).readString();
      ArrayList<String> list=new ArrayList<String>();
      list.addAll(Arrays.asList(animals.split("\n")));
      System.out.println("Found "+list.size()+" files");
      for (int ix=0; ix<list.size(); ix++) {
         System.out.println(ix+") "+list.get(ix));
      }
      
wolfieee
 
Posts: 156
Joined: Sun May 20, 2012 1:59 am

Re: how to determine number of level-files in directory??

Postby just4phil » Sun May 20, 2012 8:03 am

great man!
thankx!! that works....

and what about directories? how can i determine the number and names of directories?

(i would like to show a list of directories and if the user clicks on a directory the app shows the contained levels)

best regards
phil
just4phil
 
Posts: 801
Joined: Fri Feb 03, 2012 10:07 pm
Location: Berlin

Re: how to determine number of level-files in directory??

Postby wolfieee » Sun May 20, 2012 7:42 pm

just4phil wrote:great man!
thankx!! that works....

and what about directories? how can i determine the number and names of directories?

(i would like to show a list of directories and if the user clicks on a directory the app shows the contained levels)

best regards
phil


I don't know about diffing between directories and non=directories for internal files, but I had to adjust the code to operate on both Android and Desktop as follows: (The android call on my test device gives no files back when reading a directory!)

Code: Select all
String dirBase="sounds/animals";
      ArrayList<FileHandle> list=new ArrayList<FileHandle>();
      
      switch (Gdx.app.getType()) {
      case Android:
         list.addAll(Arrays.asList(Gdx.files.internal(dirBase).list()));
         break;
      case Applet:
         break;
      case Desktop:
         try {
            String animals = Gdx.files.internal(dirBase).readString();
            
            for (int ix=0; ix<animals.split("\n").length; ix++){
               list.add(Gdx.files.internal(dirBase+"/"+animals.split("\n")[ix]));
            }
         } catch (Exception e) {
            
         }
         break;
      case iOS:
         break;
      case WebGL:
         break;
      default:
         break;
      }
      

      System.out.println("Found "+list.size()+" files");
      for (int ix=0; ix<list.size(); ix++) {
         System.out.println(ix+") "+list.get(ix).nameWithoutExtension());
      }
wolfieee
 
Posts: 156
Joined: Sun May 20, 2012 1:59 am

Re: how to determine number of level-files in directory??

Postby just4phil » Wed May 23, 2012 7:21 pm

shit.... the code to determine the number of levels is ok under desktop version

but on android this crashes

Code: Select all
   public static int getNumberOfLevels() {   // ----- crasht unter android!!!!!!
   
      String dirBase="data/world/level1";
      String levelfiles = Gdx.files.internal(dirBase).readString();
      ArrayList<String> list = new ArrayList<String>();
      list.addAll(Arrays.asList(levelfiles.split("\n")));
      int numberOfLevels = list.size();
      Gdx.app.log("Found " + numberOfLevels, " levels");
      
      return numberOfLevels;
   }
just4phil
 
Posts: 801
Joined: Fri Feb 03, 2012 10:07 pm
Location: Berlin

Re: how to determine number of level-files in directory??

Postby wolfieee » Wed May 23, 2012 10:52 pm

just4phil wrote:shit.... the code to determine the number of levels is ok under desktop version

but on android this crashes

Code: Select all
   public static int getNumberOfLevels() {   // ----- crasht unter android!!!!!!
   
      String dirBase="data/world/level1";
      String levelfiles = Gdx.files.internal(dirBase).readString();
      ArrayList<String> list = new ArrayList<String>();
      list.addAll(Arrays.asList(levelfiles.split("\n")));
      int numberOfLevels = list.size();
      Gdx.app.log("Found " + numberOfLevels, " levels");
      
      return numberOfLevels;
   }


You have to use a switching routing for the two different environments according to my tests:

Code: Select all
String dirBase="sounds/animals";
      ArrayList<FileHandle> list=new ArrayList<FileHandle>();
      
      switch (Gdx.app.getType()) {
      case Android:
         list.addAll(Arrays.asList(Gdx.files.internal(dirBase).list()));
         break;
      case Applet:
         break;
      case Desktop:
         try {
            String animals = Gdx.files.internal(dirBase).readString();
            
            for (int ix=0; ix<animals.split("\n").length; ix++){
               list.add(Gdx.files.internal(dirBase+"/"+animals.split("\n")[ix]));
            }
         } catch (Exception e) {
            
         }
         break;
      case iOS:
         break;
      case WebGL:
         break;
      default:
         break;
      }
      

      System.out.println("Found "+list.size()+" files");
      for (int ix=0; ix<list.size(); ix++) {
         System.out.println(ix+") "+list.get(ix).nameWithoutExtension());
      }
wolfieee
 
Posts: 156
Joined: Sun May 20, 2012 1:59 am

Re: how to determine number of level-files in directory??

Postby just4phil » Thu May 24, 2012 1:33 pm

ok, thankx!
i will try that.
just4phil
 
Posts: 801
Joined: Fri Feb 03, 2012 10:07 pm
Location: Berlin

Re: how to determine number of level-files in directory??

Postby mzechner » Fri May 25, 2012 4:38 pm

I looked into this, some clow^H^H^H nice gentleman commented out some code in AndroidFileHandle, preventing it to be able to list directories (NAAATTEEE :D). I'll discuss with the team, i assume there was a reason for it.
mzechner
Site Admin
 
Posts: 4709
Joined: Sat Jul 10, 2010 3:50 pm

Re: how to determine number of level-files in directory??

Postby BurningHand » Fri May 25, 2012 5:03 pm

He claimed it was too slow. :roll:
IRC: nexsoftware / mobidevelop; GitHub: MobiDevelop;
BurningHand
 
Posts: 2262
Joined: Mon Oct 25, 2010 4:35 am

Re: how to determine number of level-files in directory??

Postby mzechner » Fri May 25, 2012 7:09 pm

I put that back in, that will make AndroidFileHandle#exist() work again. Listing internal files seems to work fine for me on Android. On the desktop you will run into problems if you link against your Android's asset folder instead of having a copy. That's because in that case the assets are in the classpath and will be fetched from there. One can't list contents of the classpath i'm afraid.
mzechner
Site Admin
 
Posts: 4709
Joined: Sat Jul 10, 2010 3:50 pm

PreviousNext

Return to General Development

Who is online

Users browsing this forum: No registered users and 2 guests