File I/O

Monkey Forums/Monkey Programming/File I/O

BigAnd(Posted 2011) [#1]
I am looking for a way to read a binary file into an array.

In Max I would have just opened a stream and used a ReadInt(stream) but I cant find any info on doing such a thing within monkey.

At the moment I am having to store the level data in a fixed array. With all the levels in the game, this comes out to be a couple of thousand lines of code so it REALLY slows compilation right down (and also crashes Jungle).

I looked at the work the guys had done on async streaming but its beyond me and I think its really not on the right track for what I need.

Am I being thick and just not seeing it in the docs/examples or is it something that's just not been implemented yet?


Canardian(Posted 2011) [#2]
I think you could make a C++ function which reads an int from a file and returns it to Monkey. You would probably have to have a OpenFile() and CloseFile() function too, and the file handle would be a global variable in C++.


BigAnd(Posted 2011) [#3]
I thought about doing this but then would I have to do a java and other version to make it cross platform compatible?


Canardian(Posted 2011) [#4]
I don't think you need Java, but only C++ and javascript (and actionscript). I'm writing a C++ demo now, hang on.
Android should work with pure C++ since NDK5, but I'm not sure if Monkey is using NDK5 yet. Before NDK5 Android needed some Java code also.

EDIT: I just downloaded NDK5, and it seems it's still bloated with Java as it needs JRE, which doesn't even install on my PC. I need to buy an Android phone and try if I can install Debian on it, or maybe wait for Android 4.0 which is hopefully finally free from Java. This article says that Debian works actually pretty nice on an Android phone, so there's no need for the Android OS at all: http://www.talkandroid.com/android-forums/android-development/1091-install-debian-android.html


marksibly(Posted 2011) [#5]
Hi,

There is currently no 'filesystem' in Monkey. Mojo provides the following commands for loading data and loading/saving app state:

LoadString
LoadState
SaveState

These will work on ALL targets.

A future posix-style module is planned (and probably quite soon), but it will not work on all targets, just those that provide a posix-style interface - initially that will probably mean only the GLFW target.


ziggy(Posted 2011) [#6]
By the way, a fix on Jungle has been commit and jungle handle this kind of files easily now. No crash or the like any more :D


degac(Posted 2011) [#7]
Can someone explain me what is SaveState?
What exactly is 'saved'?


matt(Posted 2011) [#8]
A string containing the state of the application as you see it.

AFAIK you are responsible for crafting the string and contents and then parsing it again after reading it back with LoadState.

The example at: docs/modules/index.html#/mojo.app/LoadState
shows how to load and save state (state here being the number of times the app has been run, stored as a number in a string)


Corum(Posted 2011) [#9]
This string thinghy reminds me the C=64 times, when we used to save a game session by transcribing a sequence of chars and numbers. :)
Perhaps is time to go back to the roots? :)


degac(Posted 2011) [#10]

The example at: docs/modules/index.html#/mojo.app/LoadState
shows how to load and save state (state here being the number of times the app has been run, stored as a number in a string)


Tried but I cant' see any 'previous' state loaded... (tested with HTML5 and GLFW) - I get only 'state not found - creating initial state!'.

If I understood correctly I create my 'own-app-state' - saving it in a string (so something like 'life=3;score=1000;time=123;level=2' - I suppose - and this is saved (somewhere...)...

ps: just discovered that the 'index.html' page of monkey help documentation support a search function. Unfortunately with the IDE this doesnt' work...


matt(Posted 2011) [#11]
@degac:
first time you run the LoadState example it will say 'state not found - creating initial state!' as there is no saved state, obviously. Subsequent runs will find the saved state and say 'state found - updating state!' and the state=X message on the app canvas will increment by one with each run of the app.

ps: search function does not work in Windows IDE due to issues with Internet Explorer.


degac(Posted 2011) [#12]
Of course I executed many times the app to 'see' the value: but the message is still the same...

Still testing the .exe created in GLFW: I started (from the vc2010/release folder) the app many times (at the moment I have 4 windows opened...) but the message is 'state not found - creating initial state!' for each one.


EDIT:

I tried it in FF4 and of course it doesnt' work! (just tested now in Chrome and in the new tab I see the message 'state found...'

Probabily a bug in FF?


matt(Posted 2011) [#13]
I'm not sure about all of the targets, but in HTML5 the data store is localStorage. Not sure if you can switch that off in FF4? Worth trying Chrome and/or Safari to check if it works (or not)

HTML5, Android and iOS work for me

GLFW looks like a bug