Author Topic: Using a variable to determin index size  (Read 4187 times)

0 Members and 1 Guest are viewing this topic.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Using a variable to determin index size
« on: January 10, 2019, 09:46:07 pm »
Hi guys, back from YUK!!! jury duty

anyways.. (still totally addicted to qb64 )

I notice this does not seem to be working

Code: QB64: [Select]
  1. DIM SHARED room(totalRooms) AS tRoom  

var totalRooms is pulled from an ini file. I am created a text engine, so some parts of it are user controlled for run time.

But I seem to be getting a subscript out of range error.

Is this not a valid method?
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Using a variable to determin index size
« Reply #1 on: January 11, 2019, 12:33:23 am »
Need to define tRoom type like this:
Code: QB64: [Select]
  1. _TITLE "DIM SHARED room(totalRooms) AS tRoom"
  2. CONST xmax = 800
  3. CONST ymax = 600
  4. SCREEN _NEWIMAGE(xmax, ymax, 32)
  5. TYPE tRoom
  6.     x AS SINGLE
  7.     y AS SINGLE
  8.     w AS SINGLE
  9.     h AS SINGLE
  10.  
  11. totalRooms = 10
  12. DIM SHARED room(totalRooms) AS tRoom '<<<<<<<<<<<<<<<<<<<<<<<< check this for error
  13.  
  14. FOR i = 1 TO totalRooms
  15.     room(i).x = RND * (xmax - 130)
  16.     room(i).y = RND * (ymax - 130)
  17.     room(i).w = RND * 100 + 30
  18.     room(i).h = RND * 100 + 30
  19.  
  20. FOR i = 1 TO totalRooms
  21.     LINE (room(i).x - 1, room(i).y - 1)-STEP(room(i).w + 8, room(i).h + 16), , B
  22.     _PRINTSTRING (room(i).x + RND * room(i).w, room(i).y + RND * room(i).h), "T"
  23.  

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Using a variable to determin index size
« Reply #2 on: January 11, 2019, 09:02:24 am »
Hi!!

THIS DOES WORK
Code: QB64: [Select]
  1. totalRooms = 10
  2. DIM SHARED room(totalRooms) AS tRoom '<<<<<<<<<<<<<<<<<<<<<<<< check this for error
  3.  

....AND THIS DOES WORK
Code: QB64: [Select]
  1. DIM SHARED room(10) AS tRoom '<<<<<<<<<<<<<<<<<<<<<<<< check this for error

Which part  of the "check this for error" are you referring to. (thats not supposed to be snooty LOL!!!!, its 5am when i wrote this)
the TYPE is fine - that works OK

seems to be a prob with the data pulled from the ini file - here is what I have

Code: QB64: [Select]
  1. LINE INPUT #fh, aline: totalRooms = VAL(iniRead$(aline))    

and the function for iniread$
Code: QB64: [Select]
  1. FUNCTION iniRead$ (aline AS STRING)
  2.     iniRead = _TRIM$(MID$(aline, INSTR(aline, "=") + 1, LEN(aline)))

ini file
Code: QB64: [Select]
  1. [gamenfo]
  2. ;SNIP
  3. totalrooms = 64
  4. ;SNIP

Hope that sheds a little light


ps
Went back to a program I wrote in VB1(DOS version) and trying to convert this over to QB64
« Last Edit: January 11, 2019, 09:04:25 am by xra7en »
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

FellippeHeitor

  • Guest
Re: Using a variable to determin index size
« Reply #3 on: January 11, 2019, 09:16:16 am »
Hi guys, back from YUK!!! jury duty

anyways.. (still totally addicted to qb64 )

I notice this does not seem to be working

Code: QB64: [Select]
  1. DIM SHARED room(totalRooms) AS tRoom  

var totalRooms is pulled from an ini file. I am created a text engine, so some parts of it are user controlled for run time.

But I seem to be getting a subscript out of range error.

Is this not a valid method?

A "subscript out of range error" won't happen when you define an array, so that line isn't to blame. You'll be getting that error if you try to access an index in that array that's greater than totalRooms. Look for other instances in your code where you access the room() array and make sure the index you're using doesn't go beyond the value of totalRooms.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Using a variable to determin index size
« Reply #4 on: January 11, 2019, 09:24:15 am »
Agreed however here is there puzzle (love puzzles)

x = 64
dim room(64) <-- no compile error
dim room(x) <-- no compile error

runtime:
room(64)=2 <-- no error
room(x) = 2 <-- subscript out of range error

It's an old fashion error, so very familiar with it. but the above puzzle should not error out - Im gonna keep searching... I know the harder these are  - the simpler the solution is.



I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Using a variable to determin index size
« Reply #5 on: January 11, 2019, 09:36:14 am »
I think the core issue is my database. I am using a makeshift flat-file cvs style.
I LOVE!!! SQL, but seems tricky to implement into qb64.

However, I think I am going to start there. I think that would solve most my issues, as they are revolving around geting data from the ini file, then manipulating it.
There is a pretty nice section on SQL with qb64,
https://qb64.org/wiki/SQL_Client

So gonna learn how to use that with bq64, and i'll post what I did, when I get it. or at least a working pice :-)

« Last Edit: January 11, 2019, 10:25:44 am by xra7en »
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: Using a variable to determin index size
« Reply #6 on: January 11, 2019, 11:21:46 am »
Agreed however here is there puzzle (love puzzles)

x = 64
dim room(64) <-- no compile error
dim room(x) <-- no compile error

runtime:
room(64)=2 <-- no error
room(x) = 2 <-- subscript out of range error

It's an old fashion error, so very familiar with it. but the above puzzle should not error out - Im gonna keep searching... I know the harder these are  - the simpler the solution is.

These type errors are usually solved by simply adding a PRINT x to the code in multiple places.  See what value x has, and when/where it gets corrupted in your code.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Using a variable to determin index size
« Reply #7 on: January 11, 2019, 11:53:04 am »
Agreed however here is there puzzle (love puzzles)

x = 64
dim room(64) <-- no compile error
dim room(x) <-- no compile error

runtime:
room(64)=2 <-- no error
room(x) = 2 <-- subscript out of range error

It's an old fashion error, so very familiar with it. but the above puzzle should not error out - Im gonna keep searching... I know the harder these are  - the simpler the solution is.

These type errors are usually solved by simply adding a PRINT x to the code in multiple places.  See what value x has, and when/where it gets corrupted in your code.
this I did, all came up correct. Even when the error popped up, i just kept hitting yes and it was displaying everything correctly.

BUUUUUUUUUUUUUTTTTTTTTT

I just got an idea.  You guys got my gears going again..

One of the oldest projects (like back in the 80's) I always wanted to make since Zork came out was my own custom simple text-adventure game/editor. It became huge obsession after I wrote Eliza(David h. Ahl), But I just never had the experience back in those days.

SOLUTION:
I was focusing on writing a "Player"... I just realized, I'll just write an "Editor" , save all that is needed in records. then pull those records up in the player
See
simple solution...

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: Using a variable to determin index size
« Reply #8 on: January 11, 2019, 12:13:49 pm »
Agreed however here is there puzzle (love puzzles)

x = 64
dim room(64) <-- no compile error
dim room(x) <-- no compile error

runtime:
room(64)=2 <-- no error
room(x) = 2 <-- subscript out of range error

It's an old fashion error, so very familiar with it. but the above puzzle should not error out - Im gonna keep searching... I know the harder these are  - the simpler the solution is.

These type errors are usually solved by simply adding a PRINT x to the code in multiple places.  See what value x has, and when/where it gets corrupted in your code.
this I did, all came up correct. Even when the error popped up, i just kept hitting yes and it was displaying everything correctly.

BUUUUUUUUUUUUUTTTTTTTTT

I just got an idea.  You guys got my gears going again..

One of the oldest projects (like back in the 80's) I always wanted to make since Zork came out was my own custom simple text-adventure game/editor. It became huge obsession after I wrote Eliza(David h. Ahl), But I just never had the experience back in those days.

SOLUTION:
I was focusing on writing a "Player"... I just realized, I'll just write an "Editor" , save all that is needed in records. then pull those records up in the player
See
simple solution...

Then you might want to take a look at this little text engine: http://qb64.freeforums.net/thread/44/basic-text-adventure-engine

A game file contains all that’s needed for the game, and you just let the “engine” run it.  Here’s a sample game “room”:

Code: QB64: [Select]
  1. @ROOM 1: Starting out
  2. You wake up cold and naked.  It's cold, and you're naked.
  3.  
  4. Do you want to:
  5. @GOTO 2: Freeze to Death?
  6. @GOTO 3: Open your eyes so you can actually see WHY you're cold and naked?
  7. @GOTO 4: Bite your tongue and commit suicide?
  8.  
  9.  
  10. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  11. @ROOM 2: Freeze to Death
  12.  
  13. You freeze to death.  Game over
  14. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Using a variable to determin index size
« Reply #9 on: January 11, 2019, 12:34:27 pm »
Code: QB64: [Select]
  1. @ROOM 1: Starting out
  2. You wake up cold and naked.  It's cold, and you're naked.
  3.  
  4. Do you want to:
  5. @GOTO 2: Freeze to Death?
  6. @GOTO 3: Open your eyes so you can actually see WHY you're cold and naked?
  7. @GOTO 4: Bite your tongue and commit suicide?
  8.  
  9.  
  10. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  11. @ROOM 2: Freeze to Death
  12.  
  13. You freeze to death.  Game over
  14. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

that is another style - Mine is more of a Zork style, where not only do you have to follow the story and puzzles that block your way, but you also have to figure out two word combos that make the action happen. NOT quite as elaborate as ZORK (I worked with that engine, it is very interesting), but it is a simple one.
The one you shared, is more of a choose your own adventure style (which I have a large collection of books like that - the "turn to page x,y,z ")

thanks for the share, I saved that to check it out.

Nice thing is, i have my story done, (sorta comical haunted house), just want to apply it to an engine. and part of the fun is the coding part. - mods, may have to move this part of the thread to another section - going off my own topic LOL
« Last Edit: January 11, 2019, 12:36:48 pm by xra7en »
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!