Is there a way to create a file from within code? Other languages have somethng like createfile etc.
I need them for my map editor.
Ta!
Try OPENFILE - can open a file for reading or writing...
I have this:
file$ = OPENFILE(1,"map1.txt",FALSE)
FOR xi2 = 0 TO mapwidth - 1
FOR yi2 = 0 TO mapheight - 1
WRITEIEEE 1,map[xi2][yi2]
NEXT
NEXT
It dunneh work!
Can you help?
OPENFILE is indeed what is required, and it does work -
GLOBAL map[100][100]
//
GLOBAL lvl // level number
//
// Save data
FUNCTION save_data:
//
LOCAL file$="Media/data/"+lvl+".dat"
//
LOCAL n%
//
OPENFILE (1,file$,FALSE)
//
FOR y=0 TO 22
FOR x=0 TO 22
WRITEBYTE 1,map[x][y]
NEXT
NEXT
//
CLOSEFILE 1
//
ENDFUNCTION
//
//
// Load data
FUNCTION load_data:
//
LOCAL file$="Media/data/"+lvl+".dat"
//
LOCAL n%, ok
//
ok=DOESFILEEXIST(file$)
//
IF ok
//
OPENFILE (1,file$,TRUE)
//
FOR y=0 TO 22
FOR x=0 TO 22
READBYTE 1,n
map[x][y]=n
NEXT
NEXT
//
CLOSEFILE 1
//
ENDIF
//
ENDFUNCTION
This code also includes error trapping to ensure the file exists or not (don't load a file that isn't there!)
[EDIT] Argghhhhh the spacing is all fecked on this forum. I've used // to space it out a bit - it looks quite long as a result, but it isn't really.
I don't know how GLBasic handles it but many other languages don't create a file unless you actually close the file when you are done writing.
GLBasic create a file, and then write into it!
Thanks guys but I'm still getting a problem. When I remove this code from my game it works fine but when I uncomment it the game shuts down whenever I press F1 to access the savemap() function.
Can anyone spot what's going on?
INC level,1
LOCAL file$ = "Media/Data/"+level+".map"
OPENFILE(1,file$,FALSE)
FOR xi2 = 0 TO mapwidth - 1
FOR yi2 = 0 TO mapheight - 1
WRITEBYTE 1,map[xi2][yi2]
NEXT
NEXT
CLOSEFILE 1
Are you sure that your map[][] array isn't going out of bounds (using numbers larger than the max array size)? I presume you have actually created the array already.
Try using a fixed low number in the FOR/NEXT loop
ie instead of
FOR xi2 = 0 TO mapwidth - 1
use
FOR xi2 = 0 TO 5
and likewise for the yi2.
If you still get an error then it's likely not an array problems (unless your maps are smaller than 6x6!), but I'm presuming that it is an OOB. Usually the obvious reason for unexpected exit.
Hi Ian, Just tried what you suggested and I still get the shutdown. Yes the map array is already created and used in the game without problems.
Stupidly I didnt think to turn the debugger on and this is what it spits out aftr the shutdown.
Start debug session.
_______________________________________
Injection started
Error:
No file
That helps a bit. Usually it's not a case of the file not existing, but the directory. Ensure you have a "Data" directory where it should be. If not, create one and it should be ok. :)
Hi Ian, yep the directorey is there. I always double check these things. The data directory is in the Media folder (case wise spelt properly) also.
Might this be a Win7 issue? I'm running windows 7 home premium.
I'm running Windows7 Home Premium too.
My example definitely works, as I've used it in lots of projects (including my current one).
Are you sure that you've created GLOBAL variables for mapwidth/height and that they are spelled correctly? Also remember that GLBasic is case sensitive. If you've mis-spelled (or altered case) either then the array will be a negative integer.
[EDIT]
I've just tested your code in a new file and it works fine.
DIM map[100][100]
GLOBAL mapwidth=10
GLOBAL mapheight=10
GLOBAL level
INC level,1
LOCAL file$ = "Media/Data/"+level+".map"
OPENFILE(1,file$,FALSE)
FOR xi2 = 0 TO mapwidth - 1
FOR yi2 = 0 TO mapheight - 1
WRITEBYTE 1,map[xi2][yi2]
NEXT
NEXT
CLOSEFILE 1
PRINT "DONE",10,10
SHOWSCREEN
KEYWAIT
hmmm! The directories exist. When I just try and create the file out of the media folder it works perfectly. I get my saved map and can load it back in. When it's set to the data folder it's a no go. :S
Folder structure is like this:
game.app/Media/Data << That's the folder I try to save to but it just quits out saying no file.
if I save though to game.app it all works fine.
By the way I'm running the 64bit version of Win7. Surely that can't be the issue?
Yep 64bit here too.
Try removing the "game.app/" bit - just save to "Media/Data/"
Did you try my/your code (above post) as a brand new project?
If you want to send your complete .app to me (or attach it here), I can try and work out what's going on - iprice AT supanet DOT com
Hi Ian, just sent my project folder with imges to you. Thanks for your help. :)
Does the file already exist ? If so, you will need to delete the file first.
Quote from: MrTAToad on 2010-Feb-14
Does the file already exist ? If so, you will need to delete the file first.
No you don't. You can save over a file at any time. I've made multiple map/level editors and never had to delete previously saved data.
Not received email :( Can you try mrijprice AT googlemail DOT com or iprice AT supanet DOT com again.
Hi, Just sent the project again. for some reason it won't let me send the zip file so i renamed the extension. Just rename the extension to zip when received.
Thanks for all the help. :)
Ok. Got the file and i see exactly where the problem lies.
You've used
SETCURRENTDIR("Media") // seperate media and binaries?
at the start of your program, but when saving you use -
LOCAL file$ = "..Media/data"+level+".map"
Remove the "...Media/" bit, as you've already specified the current folder.
Use -
LOCAL file$ = "data"+level+".map"
and it works fine :)
Arrrggggghhhh! :)
Thanks so much. So simple, yet drove me so mad. :D :booze:
BTW Separate post for separate assistance
You can replace this whole section of code
FOR xi = 0 TO mapwidth-1
FOR yi = 0 TO mapheight-1
IF map[xi][yi] = 0 THEN DRAWANIM 0,0,xi * 50, yi*50
IF map[xi][yi] = 1 THEN DRAWANIM 0,1,xi * 50, yi*50
IF map[xi][yi] = 2 THEN DRAWANIM 0,2,xi * 50, yi*50
IF map[xi][yi] = 3 THEN DRAWANIM 0,3,xi * 50, yi*50
IF map[xi][yi] = 4 THEN DRAWANIM 0,4,xi * 50, yi*50
IF map[xi][yi] = 5 THEN DRAWANIM 0,5,xi * 50, yi*50
IF map[xi][yi] = 6 THEN DRAWANIM 0,6,xi * 50, yi*50
IF map[xi][yi] = 7 THEN DRAWANIM 0,7,xi * 50, yi*50
IF map[xi][yi] = 8 THEN DRAWANIM 0,8,xi * 50, yi*50
IF map[xi][yi] = 9 THEN DRAWANIM 0,9,xi * 50, yi*50
IF map[xi][yi] = 10 THEN DRAWANIM 0,10,xi * 50, yi*50
IF map[xi][yi] = 11 THEN DRAWANIM 0,11,xi * 50, yi*50
IF map[xi][yi] = 12 THEN DRAWANIM 0,12,xi * 50, yi*50
IF map[xi][yi] = 13 THEN DRAWANIM 0,13,xi * 50, yi*50
IF map[xi][yi] = 14 THEN DRAWANIM 0,14,xi * 50, yi*50
IF map[xi][yi] = 15 THEN DRAWANIM 0,15,xi * 50, yi*50
NEXT
NEXT
With this -
FOR xi = 0 TO mapwidth-1
FOR yi = 0 TO mapheight-1
DRAWANIM 0,map[xi][yi],xi * 50, yi*50
NEXT
NEXT
That's a nice tip, thanks! :)
Really do appreciate it. Now to get on with my game. :) Ta!