Author Topic: Arrays not allowed in user defined TYPE  (Read 3166 times)

0 Members and 1 Guest are viewing this topic.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Arrays not allowed in user defined TYPE
« 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
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Arrays not allowed in user defined TYPE
« Reply #1 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
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Arrays not allowed in user defined TYPE
« Reply #2 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!!
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Arrays not allowed in user defined TYPE
« Reply #3 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/