Trouble with loading and saving a tile array

Advanced game related topics
User avatar
Fractalorangejuice
User
User
Posts: 14
Joined: Thu Jun 18, 2009 6:14 am
Location: Berlin,Germany

Trouble with loading and saving a tile array

Post by Fractalorangejuice »

I'm having trouble loading and saving an array.
I've been staring at it for some time and I'm getting a mental block
I keep getting a blank map1.txt file and of course a #sprite object not initialized error because there is nothing there.
Does anyone here know what I might be missing?

This is what I have:

Code: Select all

MapSize = 1024
Dim Map1.w(MapSize,MapSize)

;I used this to create an array filled completely with grass tiles only
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; ;Init Map
;  For Y = 1 To mapsize
;   For X = 1 To mapsize
;     Map1(X,Y) = 4
;   Next X
;  Next Y 
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

;Load Tile Array 
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If ReadFile(1,"map1")
 While Eof(1) = 0
   For Y = 1 To 1024
    For X = 1 To 1024
       Map1(X,Y) = ReadWord(1)
    Next X
   Next Y 
 Wend
 CloseFile(1)
Else
 MessageRequester("Information","Couldn't open the file!")
EndIf
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

;Save Array when CTRL S is pressed
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If KeyboardPushed(#PB_Key_S)
 If KeyboardPushed(#PB_Key_LeftControl) Or KeyboardPushed(#PB_Key_RightControl)
  If CreateFile(1,"map1")
  If OpenFile(1,"map1")
    While Eof(1) = 0
      For Y = 1 To 1024
        For X = 1 To 1024
          WriteWord(1,Map1(X,Y))
        Next X
      Next Y 
    Wend
    CloseFile(1)
  Else
    MessageRequester("Information","Couldn't open the file!")
  EndIf
  Delay(500)
 EndIf
EndIf
EndIf
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Trouble with loading and saving a tile array

Post by Demivec »

Code: Select all

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

;Save Array when CTRL S is pressed
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If KeyboardPushed(#PB_Key_S)
 If KeyboardPushed(#PB_Key_LeftControl) Or KeyboardPushed(#PB_Key_RightControl)
  If CreateFile(1,"map1")
  ;If OpenFile(1,"map1") ; <= unneeded, the file has already been opened with CreateFile()
    ;While Eof(1) = 0 ; <= this is also unneeded, the file was just created, this will always be true, which means nothing will be written.
      For Y = 1 To 1024
        For X = 1 To 1024
          WriteWord(1,Map1(X,Y))
        Next X
      Next Y 
    ;Wend
    CloseFile(1)
  Else
    MessageRequester("Information","Couldn't open the file!")
  EndIf
  Delay(500)
 EndIf
EndIf
EndIf
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The mental blocks have been commented in your code. :wink:
User avatar
Fractalorangejuice
User
User
Posts: 14
Joined: Thu Jun 18, 2009 6:14 am
Location: Berlin,Germany

Post by Fractalorangejuice »

Heh heh
You sir, have just made my night.

Loads and saves like a dream.

No aneurysm for me. :D
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

Ahhh Demivec, that takes me back!!! If fractal asks about tilemap editors you can hand him anything you and I worked on if you so desire!
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

Your welcome.

I'm also glad your brain is the better for it. :wink:

@Rook: That's good to know. It looks like he's got a handle on things at the moment, mostly. :wink:
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

here's some older code from me:
if you don't want to be stuck on writing the map editor first, you can use bmp2map to create tilemap data out of simple BMPs you can pixel in PAINT.
saves a lot of time and gets you started with game programming, you could care about a good editor later when you had some success with the game engine already.

http://www.purebasic.fr/german/viewtopic.php?t=2131
oh... and have a nice day.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

I have played with BMP2MAP before... link HERE:
http://user.txcyber.com/~si_slick/bmp2map/

I always used it for 3D MAP files...

I have also been playing with random level generation (and no so random with a seed value)

Dungeonmaker:
http://dungeonmaker.sourceforge.net/

The source code for something like this would be interesting:
http://www.bin.sh/gaming/tools/dungeon.cgi

and this was interesting as well:
http://www.purebasic.fr/english/viewtop ... random+map
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Post Reply