Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Subscript out of range?
#1
Ok now that I have some time to code and play around, I'm trying to get back into it.
Thus I find my self in a situation that I'm not sure what is going on. I have an array with 16 elements (0-15) and I have a RND statement that I think is supposed to give me results of 0-15(RND * 16) But every so often I am receiving a Subscript out of range error.
am I just goofing or is something else going on? What am I forgetting here?

Line 41 is listed as the error line, which is the RND * 16 line.
I've attached the needed files to run it , if your curious just what it does.
Steve, I would be interested in your thoughts on how it is setup and what it puts out.

Code: (Select All)
TYPE Player_Stat
Name AS STRING * 16
Title AS STRING * 24
Strength AS _BYTE
Dexterity AS _BYTE
Constitution AS _BYTE
Intelligence AS _BYTE
Wisdom AS _BYTE
Charisma AS _BYTE
HP AS _BYTE
MP AS _BYTE
Max_HP AS _BYTE
Max_MP AS _BYTE
Xp AS LONG
Nxp AS LONG
Level AS _BYTE
Age AS _UNSIGNED _BYTE
Race AS _BYTE
END TYPE

SCREEN _NEWIMAGE(800, 600, 32)
RANDOMIZE TIMER
DIM SHARED Layer(8) AS LONG, Font(4) AS LONG, P AS Player_Stat
DIM SHARED Titles(15) AS STRING, Race(4) AS STRING
DATA "Novice","Aspirant","Battler","Fighter","Adept","Chevalier","Veteran","Warrior","Swordman","Hero","Soldier","Myrmidon","Champion","Superhero","Paladin","Lord"
DATA "Mystery","Human","Zombie","Dwarf","Elves"
FOR i% = 0 TO 15: READ Titles(i%): NEXT
FOR i% = 0 TO 4: READ Race(i%): NEXT
Layer(0) = _DISPLAY
Layer(1) = _COPYIMAGE(Layer(0))
Layer(2) = _LOADIMAGE("8x6CharSheet.bmp", 32)
Layer(3) = _LOADIMAGE("portraits2.jpg", 32)
Font(1) = _LOADFONT("ComicRunes.otf", 40, "monospace")
Font(2) = _LOADFONT("ComicRunes.otf", 24, "monospace")
Font(3) = _LOADFONT("DwarvenStonecraftCyr.otf", 24, "monospace")
_FONT Font(1)
_PRINTMODE _KEEPBACKGROUND
_SCREENMOVE 10, 10
COLOR _RGB32(0)
P.Name = "Ryo Seaba"
P.Title = Titles(RND * 16)
Stat_Maker

x%% = INT(RND * 10)
y%% = INT(RND * 10)
_PUTIMAGE , Layer(2), Layer(0)
_PRINTSTRING (293 - 18 * (LEN(RTRIM$(P.Name)) / 2), 28), P.Name
_PUTIMAGE (590, 80), Layer(3), Layer(0), (160 * x%%, 120 * y%%)-STEP(159, 119)
Fill_Stats

'LINE (319, 20)-STEP(2, 40), _RGB32(255, 0, 0), BF
'LINE (128, 40)-STEP(329, 2), _RGB32(255, 0, 0), BF
'160,120
SUB Stat_Maker
P.Strength = INT(RND * 15) + 1
P.Dexterity = INT(RND * 15) + 1
P.Constitution = INT(RND * 15) + 1
P.Intelligence = INT(RND * 15) + 1
P.Wisdom = INT(RND * 15) + 1
P.Charisma = INT(RND * 15) + 1
P.HP = INT(RND * P.Strength) + INT(RND * P.Constitution) + P.Constitution \ 2
P.MP = INT(RND * P.Intelligence) + INT(RND * P.Wisdom) + P.Wisdom \ 2
P.Max_HP = P.HP
P.Max_MP = P.MP
XpNeed%% = P.Strength + P.Dexterity + P.Constitution + P.Intelligence + P.Wisdom + P.Charisma
' P.Xp = XpNeed%%
P.Nxp = (INT(RND * XpNeed%% * 2) * 2 + 10) * (P.HP / 12) 'int((P.HP + P.MP)/10)*10
P.Level = 0
P.Age = INT(RND * 6) + 16
P.Race = INT(RND * 5)
END SUB

SUB Fill_Stats
_FONT Font(2)
_PRINTSTRING (192, 206), LTRIM$(STR$(P.Strength))
_PRINTSTRING (192, 238), LTRIM$(STR$(P.Dexterity))
_PRINTSTRING (192, 270), LTRIM$(STR$(P.Constitution))
_PRINTSTRING (192, 302), LTRIM$(STR$(P.Intelligence))
_PRINTSTRING (192, 334), LTRIM$(STR$(P.Wisdom))
_PRINTSTRING (192, 366), LTRIM$(STR$(P.Charisma))
_PRINTSTRING (600, 276), LTRIM$(STR$(P.HP))
_PRINTSTRING (600, 302), LTRIM$(STR$(P.MP))
_PRINTSTRING (664, 276), LTRIM$(STR$(P.Max_HP))
_PRINTSTRING (664, 302), LTRIM$(STR$(P.Max_MP))
_PRINTSTRING (600, 330), LTRIM$(STR$(P.Xp))
_PRINTSTRING (600, 356), LTRIM$(STR$(P.Nxp))
_PRINTSTRING (316, 294), "Bare Fists"
_PRINTSTRING (316, 490), "Thin Cloth"
_PRINTSTRING (624, 384), LTRIM$(STR$(P.Level))
_PRINTSTRING (624, 410), RTRIM$(Race(P.Race))
_PRINTSTRING (624, 438), LTRIM$(STR$(P.Age))

_FONT Font(3)
_PRINTSTRING (293 - 19 * (LEN(RTRIM$(P.Title)) / 2), 144), P.Title
END SUB


Attached Files
.rar   StatMaker.rar (Size: 1.08 MB / Downloads: 43)
Reply
#2
You're better off putting ```INT()``` around the ```RND * 16```. Because the C++ compiler code is forced to do a type-casting or something else to handle a float value to fit it as integer and then rounding errors could happen. (shrugs)

Code: (Select All)
P.Title = Titles(INT(RND * 16))

Also you might lose some information assigning an element from ```Titles()``` array which is an array of VLS's, while "Title" field from UDT could hold only 24 characters.
Reply
#3
Code: (Select All)
Dim totals(16)
For i = 1 To 1000 'roll the dice 1000 times!
x = Rnd * 16
totals(x) = totals(x) + 1
Next
For i = 0 To 16
Print i, totals(i)
Next

Run the above and you'll quickly see the glitch -- values ROUND to the nearest integer. Anytime you get 15.5 or higher, it tries to increment the total for the 16th element in my array.

Fix??

Simply make that (INT(RND * 16)) in your program instead.
Reply
#4
Just a flyer here, should it be

P.Title = Titles(INT(RND * 16))

?
It's not the having, it's the doing.
Reply




Users browsing this thread: 2 Guest(s)