Jump to content

modifying and saving a text file with typescript?


coolwhip
 Share

Recommended Posts

loading a text file in phaser with typescript is trival with a simple:
this.game.load.text("reference", "url")
however attempting to write or modify external text files appears to be way more complicated.

I'm guessing this is for security reasons, but still don't see why there can't be a "game.write.text()" method or something similar?

Link to comment
Share on other sites

Not sure what would be your use case? When you publish your game "url" will be remote http server. In theory you could write a method that will send HTTP POST or PUT request to server and then server will write (if you program it to do so) the content to file. But once your game will have more than one player it will probably lead to data corruption. Better say what you want to achieve and then maybe someone will be able to point you toward proper solution. 

Link to comment
Share on other sites

If you have your data in an array you could do it in that way:

let textarray = ["text1", "text2", "text3"];

let txtContent = "data:text/csv;charset=utf-8,";
textarray.forEach(function (rowArray)
{
   let row = rowArray.join(",");
   txtContent += row + "\r\n";
});
let encodedUri = encodeURI(txtContent);
let link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "my_data.csv");
document.body.appendChild(link); // Firefox
link.click();

 

Link to comment
Share on other sites

  • 7 months later...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...