Array index out of bounds?

Blitz3D Forums/Blitz3D Beginners Area/Array index out of bounds?

CHALLENGER426(Posted 2008) [#1]
i made a machine gun in a game, and when i hold down the button to shoot, its shoots for about 10 secs, then "Array index out of bounds" appears, how can i make it not do this?


nrasool(Posted 2008) [#2]
The error messages suggest that at some point in your code, you are going beyond the array size. You may want to give a snip of your code on where the problem array is


Stevie G(Posted 2008) [#3]
Clearly you are declaring an array to hold a finite number of bullets and this is being exceeded.

I'd recomment using Types to create and delete bullets as required to avoid this issue. Better still, post your code so that we can have a better idea of what you're doing wrong.


CHALLENGER426(Posted 2008) [#4]
ok here it is...

;MACHINEGUN

Dim bullet(200)
For i=0 To 200
bullet(i)=CreateSphere()
ScaleEntity bullet(i),0.1,0.1,-0.1
EntityColor bullet(i),0,0,0
HideEntity bullet(i)
EntityType bullet(i),BULL
EntityRadius bullet(i),0
Next
........
........
......
If KeyDown(30)  Then
PositionEntity bullet(t) ,EntityX(car,4),EntityY(car,2),EntityZ(car,4)
ShowEntity bullet(t)
RotateEntity bullet(t) ,EntityPitch#(car,1),EntityYaw#(car,1),EntityRoll#(car,1)
t=t+1
PlaySound gunz

EndIf 
For q = 0 To 200
MoveEntity bullet(q), 0,0,.5

If CountCollisions (bullet(q))
crash=CollisionEntity (bullet(q),1)
PlaySound impact
HideEntity crash

EndIf
Next

it always highlights the positionentity part


CHALLENGER426(Posted 2008) [#5]
i fixed this part, but now it gets really slow, why, how can i fix this...too many bullets?


GfK(Posted 2008) [#6]
200 spheres is a LOT of polys.

I'd seriously read up on how to use Types. Bullets move fast and there is no reason at all that you should need more than a handful active at any one time.

Also, don't use spheres. Use pivots. Bullets move too fast to be seen.


Zethrax(Posted 2008) [#7]
Another option is to use a sprite for your bullet media, if you want the bullet to be visible. Sprites are only two polygons, so you can have a lot of them visible at once.

Have a look in the 3D samples that came with Blitz3D, for examples of bullet code. Mark Sibly's castle demo is a good one to look at ( C:\Program Files\Blitz3D\Samples\Blitz 3D Samples\mak\castle ).


andy_mc(Posted 2008) [#8]
if you're making an array of 200 items, then the items are numbered form 0 to 199, your loops go from 0 to 200, when you access bullet(200) you're out of bounds.


Stevie G(Posted 2008) [#9]

if you're making an array of 200 items, then the items are numbered form 0 to 199, your loops go from 0 to 200, when you access bullet(200) you're out of bounds.



Maybe I've misread this but his array goes from 0-200 so has 201 elements so when he accesses bullet 201 he's out of bounds?

Just noticed this line ..

PositionEntity bullet(t) ,EntityX(car,4),EntityY(car,2),EntityZ(car,4)


The second param for Entityx / y & z can only be 0 or 1 ( false or true ). What you have will be interpreted as true but I thought I'd mention it.


andy_mc(Posted 2008) [#10]
my bad, yes that's right.

You use the variable "t", at one point you increment it's value, which makes me think it's not the variable being used in a for loop. So could it be that t goes over 200 at some point?


CHALLENGER426(Posted 2008) [#11]
THANKS EVERYBODY


LedgerARC(Posted 2008) [#12]
ok, here it is, when you shoot it will only go up to 200, so what you have to do in the shootine part is simply this:

If KeyDown(30) Then
PositionEntity bullet(t) ,EntityX(car,4),EntityY(car,2),EntityZ(car,4)
ShowEntity bullet(t)
RotateEntity bullet(t) ,EntityPitch#(car,1),EntityYaw#(car,1),EntityRoll#(car,1)
t=t+1
PlaySound gunz


If t=200 then t=1
----------------------


EndIf

the underlined is what you need to put in, it simply says that if you reach the end of t (200) then go back to the begining of it (1) or you could put 0.

hope this helped, and it looks like you learned blitz from the same book I did, if you did then you know what I'm talking abou:)