QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: xra7en on December 03, 2018, 06:46:19 pm

Title: Arrays not allowed in user defined TYPE
Post by: xra7en on December 03, 2018, 06:46:19 pm
writing an app, and need two arrays to add to a TYPE records, for saving purposes (game)

array_one(x,y) and array_two(x,y)

I know I cannot do
Code: QB64: [Select]
  1. TYPE tPLAYER
  2.     array_one(x,y) as double
  3.     array_two(x,y) as double
  4.     [more vars]

Is there a way to do this? Many languages can, but not qb64.
I may be missing it, as I am working on other parts of the app, just got stumped
Title: Re: Arrays not allowed in user defined TYPE
Post by: SMcNeill on December 03, 2018, 07:17:58 pm
There's ways to work around the issue, but none are really simple to implement.   Oftentimes, the best solution is multiple calls to PUT/GET to save your game data.

Type a
   X as integer
   Y as double
   Z as string * 10
END TYPE

Dim example AS a
DIM array(1 to whatever, 1 to whatever2)

Then when saving the data, save it with:

PUT #1, , example 'save the main type data
PUT #1, , array() 'save the array info directly

To get the data back, just do the same with GET instead of PUT
Title: Re: Arrays not allowed in user defined TYPE
Post by: xra7en on December 05, 2018, 11:07:33 am
KLUDGE way. I am using this for now - works. Later when my game is done, I'll revisit it

I am looking into the PHP (UN/SERIALIZE) version of this, but for right now I simplified it (CUZ Im in a hurry LOL)

I save it to a comma delimited string:

Code: QB64: [Select]
  1.     FOR I = 1 TO 16
  2.         FOR J = 1 TO 10
  3.             BC = BC + STR$(BASE_CLICK(I, J)) + ","
  4.             BI = BI + STR$(BASE_IDLE(I, J)) + ","
  5.         NEXT
  6.     NEXT
  7.  
  8. '//BC & BI are now a single string, and can be added to the TYPE record to save.
  9.  

Then to "LOAD(game)" I restore the global arrays with the save "string" in the TYPE record.


 
Code: QB64: [Select]
  1.   FOR I = 1 TO 16
  2.         FOR J = 1 TO 10
  3.             IDLE_TALLY(I, J) = VAL(LEFT$(IT, INSTR(IT, ",") - 1))
  4.             WEAPON_TALLY(I, J) = VAL(LEFT$(WT, INSTR(WT, ",") - 1))
  5.             IT = SHRINK_STR$(IT)
  6.             WT = SHRINK_STR$(WT)
  7.  
  8.         NEXT
  9.     NEXT
  10.  
  11.  
  12.  


quick and dirty string manipulation, I think there is a qb64 function for this, but didnt take time to look it up.

Code: QB64: [Select]
  1. FUNCTION SHRINK_STR$ (TXT AS STRING)
  2.     DIM DELIM AS INTEGER '          FIRST COMMA
  3.     DELIM = INSTR(TXT, ",")
  4.     SHRINK_STR$ = MID$(TXT, DELIM + 1, LEN(TXT))
  5.  
  6.  

Messy? Yes, but obviously this can be cleaned up. but for something temporary for testing, this works well. opinions? suggestions? Love to hear them..Thanks!!
Title: Re: Arrays not allowed in user defined TYPE
Post by: Pete on December 05, 2018, 01:17:45 pm
Speaking of QB64 functions, did you know you could keep the string intact while parsing it by adding a seed to the INSTR statement?

Code: QB64: [Select]
  1. DIM text AS STRING, parse AS STRING
  2. text = "123,456,abcdefg,ZXCV,end"
  3.     parse = MID$(text, seed + 1, INSTR(seed + 1, text, ",") - seed - 1)
  4.     IF parse = "" THEN
  5.         IF text <> "" THEN PRINT MID$(text, seed + 1)
  6.         EXIT DO
  7.     ELSE
  8.         PRINT parse
  9.     END IF
  10.     seed = seed + LEN(parse) + 1
  11.  

Nice work around in your method. That's part of the fun of programming for me, trying to make the near impossible nearly possible!

Pete