Jump to content


Photo

File I/O ?


  • Please log in to reply
6 replies to this topic

#1 OMGCarlos

OMGCarlos

    GMC Member

  • GMC Member
  • 329 posts
  • Version:GM:HTML5

Posted 24 September 2011 - 04:57 AM

I understand that for writing I would have to use PhP, but is there a native command(s) to read? I have a simple txt file on my server and would like to just read in a sequential list of strings.
  • 0

#2 Mayhem Games

Mayhem Games

    Proud Kiwi

  • GMC Member
  • 955 posts
  • Version:GM:Studio

Posted 24 September 2011 - 04:59 AM

Using the java tutorial i did reading and writing works fine with existing files on my server. The only downside to the build in file I/O functions is that they are saved into a sandbox as such.

Edited by Mayhem Games, 24 September 2011 - 05:00 AM.

  • 0

#3 OMGCarlos

OMGCarlos

    GMC Member

  • GMC Member
  • 329 posts
  • Version:GM:HTML5

Posted 24 September 2011 - 05:12 AM

Yea I was actually just taking a look at your tutorial, only issue I have with that is that as far as I know users have to hit the "Allow Java" button which is minor but something I would rather avoid. I'm not really sure what you mean by sandbox but I really just need reading only.
  • 0

#4 Mayhem Games

Mayhem Games

    Proud Kiwi

  • GMC Member
  • 955 posts
  • Version:GM:Studio

Posted 24 September 2011 - 05:21 AM

From what i have heard you can't read files on your computer/server because its not part of your games sandbox. Think of a sandbox as a "box" where every file you create goes to. Once the game is closed this "box" is destroyed along with all the files in it.

Edited by Mayhem Games, 24 September 2011 - 05:21 AM.

  • 0

#5 TGMG

TGMG

    G-java creator

  • New Member
  • 706 posts
  • Version:GM8

Posted 24 September 2011 - 05:31 AM

Create a new extension and add the following code to the javascript file:
readfromreadme=function() {
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "readme.txt", false);
txtFile.onreadystatechange = function() {
  if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
    if (txtFile.status === 200) {  // Makes sure it's found the file.
      allText = txtFile.responseText; 
      lines = txtFile.responseText.split("\n"); // Will separate each line into an array
        alert(lines);
    }
  }
}
txtFile.send(null);
}

Call the function readfromreadme() in your game.

Then create readme.txt next to index.html and it will alert the lines in the file :) It has to be on the same server!

Btw I got the file reading code from: http://social.msdn.m...25-8b3b9a078eab

Edited by TGMG, 24 September 2011 - 05:33 AM.

  • 0

#6 OMGCarlos

OMGCarlos

    GMC Member

  • GMC Member
  • 329 posts
  • Version:GM:HTML5

Posted 24 September 2011 - 05:32 AM

Oh ok, I understand. I have to keep myself from thinking like I'm on coding for a desktop. Thanks!

Whoa awesome TGMG, exactly what I was looking for.

Edited by OMGCarlos, 24 September 2011 - 05:34 AM.

  • 0

#7

  • Guests

Posted 24 September 2011 - 08:46 AM

Inlcude the text file in the games included resources, then use the text file API. This should work fine.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users