How To Read From A Local File ?

Monkey Targets Forums/HTML5/How To Read From A Local File ?

semar(Posted 2013) [#1]
Hi all,

I would like to make a quiz game for a Mac computer.

Initially I thought to use BMax for this purpose, but since I have a PC with Windows XP, I can't compile a BMax source for it, can I ?

So I though, I can do it with Monkey, and let the game run locally on a browser.

But !

I need to read a text file with questions and answers, so that the game can challenge the player.

Question: how can I read such a text file from within a .monkey program, (target HTML5) ?

The LoadState does not really work with a real file; by the way, where is the text saved when SaveState is used ? In a cookie ?

Back to the question: which tool do we have to let a monkey program read from a text file ?

Any help would be greatly appreciated.

Alternatively: do Java compiled programs (.class files) run on a Mac by default ? Or should I re-compile the source code on it - that is, on the Mac ?

Sergio.


MikeHart(Posted 2013) [#2]
Use LoadString to read from a textfile.

SaveState doesn't store in a cookie but in the local storage of your browser.


semar(Posted 2013) [#3]
Use LoadString to read from a textfile


The documentation states that LoadString does not work with HTML5.
Are you sure, that we can use LoadString on a HTML5 target ? Because that is exactly what I need, just read text from an external file.

in the local storage of your browser

Where is this local storage exactly ?


semar(Posted 2013) [#4]
By the way, there is here on the forum a link to an html5 example demo which uses Diddy combobox, inputbox, etc.

Question 1: where is that demo link ?
Question 2: does the Diddis input box accept text from a clipboard ? In other terms, if I copy a text from somewhere, can I then paste it on a Diddy inputbox ?

Thanks for the attention,
Sergio.


MikeHart(Posted 2013) [#5]
Use mojo.Loadstring, not os.loadsring.

About localstorage, google is your friend. I just looked into this once, but forgot about it.. I I needed an extra tool to look through the db where it was stored.


semar(Posted 2013) [#6]
Thanks Mike, I'll try the mojo.LoadString as soon as I can.


diemar(Posted 2016) [#7]
Did anybody try to make a script extension that uses Browser cookies to read and save data on the client machine?


ImmutableOctet(SKNG)(Posted 2016) [#8]
@diemar: Maybe have a look at the HTML5 file APIs? I put this together a while back. Here's the related thread.

You can take a look at WebCC and 'regal.virtualos' if you want to see how you can do some crazy stuff. As a means of storing data local to the browser, 'regal.virtualos' is pretty simple. Examples can be found here. It's best to install the 'virtualos' module as a sub-folder in a new folder called "modules/regal". Alternatively, it should be independent of the 'regal' prefix, so you should be able to use it separately.

To download 'virtualos', either clone it from GitHub, or click here for a direct download.


diemar(Posted 2016) [#9]
Thank you very much!


diemar(Posted 2016) [#10]
actually, it turned out to be extremly simple to access cookies and webstorage in html5 as read/write when using additional javascript function in monkeygame.html and access them from within monkey after declaring them as dummy Extern functions in monkey. The actual javascript cookie or webstorage reading/writing code is really just 1 line of code, as demonstrated in various tutorials.


diemar(Posted 2016) [#11]
here's some code. add this to your monkey source:
Extern
Function myExternalFunction()
Function myExternalFunction2()
Public

eg. just before the line "Function Main()".
Then in your build folder edit monkeygame.html (copy paste from notepad to wordpad to notepad to convert to windows linebreaks if neccessary) and add your function at the end in the HEAD's script section, eg.:
var myVar="";
Function myExternalFunction()
{
  myStorage=setItem("lastname","Smith");
  myVar=myStorage.getItem("lastname");
}

to test this, add an input button of a form to the html code, like:
<input type="button" value="clickme" onclick="alert(myVar)"></input>

Obviously, both cookies and Webstorage use Strings, so one may need to convert eg. binary data, I would suggest a simple hex encoding, requiring twice the space of the binary data, eg. A3FFC7 ... and each pair represents one byte of the string, which may be rather fast.

This is primitive, but simple :)

oh, BTW. you can declare a global variable in monkey, like:
Import mojo
Global myVar:String=""

and then access it from within monkeygame.html by adding a certain prefix:
alert(bb_monkeysourcename_myVar);

where monkeysourcename is the name of your monkey sourcefile, without the ".monkey" part.


diemar(Posted 2016) [#12]
I wrote a simple emulation of BASIC sequential file io using the local webstorage in html5:
http://www.monkey-x.com/Community/posts.php?topic=12667