Author Topic: load "map"  (Read 2402 times)

Offline XaMMaX

  • Mr. Drawsprite
  • **
  • Posts: 67
    • View Profile
load "map"
« on: 2008-Jan-06 »
I wonder if glbasic can save\load matrix in files(dat for example) as it can fenix? I need to load map like this:

1 1 1 1 1
2 1 1 2 1
2 1 2 1 2
...

but a lot of bigger.

Or i only can save it in text file one by one?

Thanks in advance and sorry for my english.
Sry for my english

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10753
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
load "map"
« Reply #1 on: 2008-Jan-06 »
Code: (glbasic) [Select]
FUNCTION WriteArray: a[]
OPENFILE 1, "out.bin", false
writelong 1, len(a[])
writelong 1, bounds(a[], 1)
for x=0 to len(a[])-1
   for y=0 to bounds(a[],1)-1
      writeieee 1, a[x][y]
   next
next
closefile 1
endfunction

Offline XaMMaX

  • Mr. Drawsprite
  • **
  • Posts: 67
    • View Profile
load "map"
« Reply #2 on: 2008-Jan-06 »
It says: syntax error on second string of function =\
EDIT:
Now i see, not
OPENFILE 1, "out.bin", false
,but
OPENFILE (1, "out.bin", false)
Sry for my english

Offline bricky91

  • Mc. Print
  • *
  • Posts: 47
    • View Profile
load "map"
« Reply #3 on: 2008-Feb-06 »
Quote from: GernotFrisch
Code: (glbasic) [Select]
FUNCTION WriteArray: a[]
OPENFILE 1, "out.bin", false
writelong 1, len(a[])
writelong 1, bounds(a[], 1)
for x=0 to len(a[])-1
   for y=0 to bounds(a[],1)-1
      writeieee 1, a[x][y]
   next
next
closefile 1
endfunction
hi, could you please tell me how it works? i am a noob in glbasic, so i didn't understand really well this code... but i think it could be a useful function.

thanks a lot! bye :)

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1790
    • View Profile
    • Homepage
load "map"
« Reply #4 on: 2008-Feb-06 »
I'll try to explain :-)

Code: (glbasic) [Select]
FUNCTION WriteArray: a[]
OPENFILE 1, "out.bin", false  // Open a file to write to
writelong 1, len(a[])  // write the size of the array to the file so we can open it up later and know
writelong 1, bounds(a[], 1) // how big the array is supposed to be to contain the data
for x=0 to len(a[])-1  // Run thru the array and write each number to the file
   for y=0 to bounds(a[],1)-1 // incrementing x and y in turn so we walk thru the whole array
      writeieee 1, a[x][y]  // write the number at x, y to the file
   next
next
closefile 1
endfunction