create and write into a file

Monkey Forums/Monkey Beginners/create and write into a file

Ashmoor(Posted 2016) [#1]
I am working on a level editor and would like create and save a file for each level, or even save all levels in a single file, how do I do that?

I searched the forum but unfortunately I don't understand much and cant get the code to work. I might be missing something but I don't know what I don't know.


Steve Ancell(Posted 2016) [#2]
This other thread may pave the way.

http://www.monkey-x.com/Community/posts.php?topic=12669


Ashmoor(Posted 2016) [#3]
@Steve Ancell, thanks I checked that thread before but it took me a while to understand that some stuff does not work with HMTL 5 as target so I had to dig through the docs and find out what each target can do. I will check it again and hopefully will be able to understand how things work.

I managed to create a file but I have no idea how to set the location. I can create make new folders in the default location but cant save it on the desktop or somewhere else. Is it possible to create it anywhere on the hard drive if target is glfw?


Steve Ancell(Posted 2016) [#4]
For HTML5 you can use SaveState and LoadState. I know those methods don't deal with a file as such. but you could use markers in the string that could be kind of accessed like files.


Shinkiro1(Posted 2016) [#5]
I have a level editor working in html5 which saves my level files into the data folder.
Writing files in HTML5 alone won't work, therefore I use NWJS which gives you a window with a webview in it.
Then I use this js function:

var native = new Object();

native.WriteFile = function(filePath, content) {
	var fs = require('fs');
	if (!fs) return;
	fs.writeFileSync(filePath, content, "utf-8");
}

PS: There is a thread around here how to setup NWJS with monkey.


Steve Ancell(Posted 2016) [#6]
Shinkiro1:
PS: There is a thread around here how to setup NWJS with monkey.


Does NWJS work with the first incarnation of monkey or is it a monkey-2 thing?

I searched the site for NWJS, other than this thead and the other one (titled: File I/O - very simple), there was only one other thread that contains NWJS: (Linkage!)


Shinkiro1(Posted 2016) [#7]
Ok that thread had the full title:
http://www.monkey-x.com/Community/posts.php?topic=8059

NWJS works with monkey 1. I don't know about monkey2.
Keep in mind that you can not use this functionality within a browser window.
What I do is to have a preprocessor variable, like this:
#USE_EDITOR = True

#If USE_EDITOR
Import nativefunctions
#End


Then when I want it to work inside a normal browser (without NWJS) I just set USE_EDITOR to false.
I assume you need the editor only for development.