Posted: 18th Jun 2007 21:58
Is there anyway to get the array dimensions of a file "Array.arr" or whatever?

E.g i save a 5*10 array, and before i load it i need to dim it, how can i know what dimensions to dim it to?
Posted: 19th Jun 2007 0:28
I just save the size as the first variable in the file

or

the other way to do it is to look for an end of file marker.

DBP arrays are variable so you don't have to set a definate size.

see F1/Help -> Principle -> Lists, Stacks and Queues
Posted: 20th Jun 2007 17:03
You have to dim the array to the exact size though or it just fills everything with 0's?

Here is some test load code which returns all zeros if x2 is <> 6 (the size of the array when i saved it)

+ Code Snippet
   
x2 = 6
Dim Object#(x2,10)
Load Array "test.arr",Object#(0)

For x = 1 to x2
   For y = 1 to 10
   set cursor y*30,x*20
      Print int(Object#(x,y))
   next y
next x
Posted: 20th Jun 2007 17:16
I forget the exact syntax I think it's something like this

dim myarray (0)
empty array (myarray)
do
inc position
line$ = load_item_line(position) 'you need to write your own line reader function
if line$ <> "end marker"
ARRAY INSERT AT BOTTOM myarray()
SampleArray(position) = line$
endif
loop

arraySize = position
'or use
arraySize = array count(array)
Posted: 20th Jun 2007 19:27
The problem is though, unless i dim it to the exact size it will return a load of 0's.

E.g I save this array
+ Code Snippet
1
2
5
8
3


Then if run this code
+ Code Snippet
Dim array(x)
For i = 1 to x
Print Array(i)
Next i


It will return x zeros, unless x was 5 when it will work.
Posted: 20th Jun 2007 20:30
I've updated my syntax (above) to make it more correct

sorry I don't know how to do it reading a whole array I only know how to get it to work reading in a line at a time.
Posted: 20th Jun 2007 20:58
Use get file size(), and divide it by the size of each element in the array. If you're using integers or dwords, this would be 4. Doubles are 8, words are 2, and bytes and booleans are 1.
Posted: 20th Jun 2007 21:27
Hmm i'm still having problems with that gatorhex, thanks anyway

Diggsey that works great, if anyone was wondering, the size of integers is 12+(Spaces in array)*8

Here is my working code for anyone else struggling with this
+ Code Snippet
   
ArraySize = (File Size(CurrentWorld$+".arr")-12)/8
   ArrayWidth = ArraySize/11
   NumObjects = ArrayWidth-1
   Dim Object#(NumObjects,10)
   Dim Object$(NumObjects,10)
   Dim NPCChat$(NumObjects,10)
   Load Array CurrentWorld$+".arr",Object#(0)
   Load Array CurrentWorld$+"$.arr",Object$(0)


Remeber not to get caught out that ArrayName(0) counts as a space!

Thanks very much for your help guys/girls
Posted: 21st Jun 2007 3:48
LOL, just saw your example, if your concidering a complete RPG scripting system you can save yourself a lot of hassel by using LUA instead
Posted: 21st Jun 2007 12:14
Yeah, I agree wholeheartedly with that. You'll be saving and loading arrays all day. You'll essentially have to create a full scripting system, and if you don't get it right in the beginning you'll regret it later, in the form of scrapping your project.

LUA is great. I use it for so many things.
Posted: 21st Jun 2007 15:31
Sure.

Don't bother with file size, bother with number of lines. This permits you to load whatever kind of files without bothering with maths.

+ Code Snippet
a = GetNumberOfLines("myfile.txt")

DIM myarray(a)

IF a&lt;&gt;0
   Load Array "myfile.txt",myarray(0)
ENDIF





` If you have the FASTFILE DLL (it's free)
FUNCTION GetNumberOfLines(f$ AS STRING)

   return_number AS DWORD = 0

   ReadFileF f$, 1
   return_number = LineSF(1)
   CloseF 1

ENDFUNCTION return_number



` If you do not have it

FUNCTION GetNumberOfLines(f$ AS STRING)

   return_number AS DWORD = 0

   ` File not Found
   IF FILE EXIST(f$) = 0
      EXITFUNCTION 0
   ENDIF

   ` Find free file id
   REPEAT
      INC n
      IF n = 33
         EXITFUNCTION 0
      ENDIF
   UNTIL FILE OPEN(n) = 0

   ` Start File
   OPEN TO READ n,f$

   ` Check File Status
   IF FILE OPEN(n) = 0 OR FILE END(n) = 1
      EXITFUNCTION 0
   ENDIF

   ` Count number of lines
   WHILE FILE END(n) = 0
      READ STRING n,s$
      INC return_number
   ENDWHILE

   ` To keep array/float counter consistent,
   ` (arrays count from element 0, we count from element one)
   DEC return_number 

   ` Close File
   CLOSE FILE n


ENDFUNCTION return_number
Posted: 21st Jun 2007 23:14
LUA? I'm just going to do a search . It's not a full blown rpg anyway, more like an adventure puzzle game so i may be okay? I've got a fully working level editor and stuff done so hopefully i won't have to scrap too much
Posted: 28th Jun 2007 20:00
Okay i've look at LUA but i'm confused... Do you make the files in another program and then just load them in DBP? I've downloaded the plugin, but the commands don't seem to turn blue when i type them...
Posted: 29th Jun 2007 16:20
bump
Posted: 29th Jun 2007 16:21
You need either a free or commerical LUA plug-in for the commands to work.

http://darkbasicpro.thegamecreators.com/?f=lua
Posted: 29th Jun 2007 19:51
I downloaded this one http://forum.thegamecreators.com/?m=forum_view&t=74095&b=5
Posted: 29th Jun 2007 23:18
he commands don't seem to turn blue when i type them


I don't have LUA but I think this is because you need to put a keywords .ini file in the Editor\Keywords folder.