Array problem

BlitzPlus Forums/BlitzPlus Beginners Area/Array problem

I'm getting an error of "array index out of bounds" and I have no idea what it means. I'm trying to draw 10 aliens on the screen with this code:

If gfxAlienOnScreen(x) = True Then
For x = 1 To AlienCount

DrawImage gfxAlienOnScreen(x),Alien_x(x),Alien_y(x)

Next
EndIf

Ok, the DrawImage is actually in front in Blitz. My understanding of arrays, types and their use is still very primitive if you can't tell. Any help will be appreciated.

Thanks

If you declare an array with Dim a(5) then the array has six elements: a(0) a(1) ... a(5).

When you refer to a(x) the index variable x must be in the range 0 to 5.
Anything else is an 'out of bounds' error.

Thanks Floyd.

The program will compile now but no aliens will draw to the screen. Any tips?

Here is the new code:

If gfxAlienOnScreen(AlienCount) = True Then
For x = 0 To AlienCount

DrawImage gfxAlienOnScreen(x),Alien_x(x),Alien_y(x) ;Draw alien on screen
Next

EndIf

Thanks

I have a suggestion for you: scratch the whole array usage here. In a case like this, using Types can produce much better results. The built-in Blitz tutorials have some Type coverage, and if you need any more help, I'll be happy to do what I can!

I think the first line should be changed to:
If gfxAlienOnScreen(x) Then Drawimage etc.
and then be placed in the loop

ADAM, I guess now will be as good of a time as any to try my hand with types. I'll work with those for a bit and post back if I have any trouble. Thanks

b32: I'll try that and see what happens but I believe only 1 alien would be drawn on the screen since the array would not be completely filled if drawimage was used before the loop. I'll post back in a bit and let you know what happens. Thanks

edited to add: No aliens were drawn with drawimage in front of the for...next loop.:(

You're right about that drawimage should be in the loop. I thought you should move the If..Then inside the loop:
For x = 0 To AlienCount
If gfxAlienOnScreen(x) Then DrawImage gfxAlienOnScreen(x),Alien_x(x),Alien_y(x) ;Draw alien on screen
Next 

But why are you checking gfxAlienOnScreen() ? Maybe you could use this:
For x = 0 To AlienCount
DrawImage gfxAlienOnScreen(x),Alien_x(x),Alien_y(x) ;Draw alien on screen
Next 

?

I was checking gfxAlienOnScreen() so it would only draw the aliens that have not been killed. How do you post code in blue like that?

Thank you b32. Moving the if..then and taking the "= true" off the end works. I removed the "= true" from "If gfxAlienOnScreen()" in two places and everything seems perfect now. I wonder why it wasn't working with "If gfxAlienOnScreen() = true"?

Thanks again!

Here's an interesting bit of info about using If statements with True and False operators. Coding something like:
If variable = False
Is the same as saying:
If variable = 0
Or:
If Not variable
Basically, False is equal to zero. True, on the other hand, can represent two different things. If you use it in this fashion:
variable = True
Then True will equal 1, and that value will be stored in variable. If, however, you use it like this:
If variable = True
Then True represents any value greater than 0. How you "phrase" your code doesn't usually make a difference to the output, it just makes it more or less compact. So, instead of:
If variable = False
Consider:
If Not variable
And instead of:
If variable = True
Consider:
If variable
That's all. Happy coding!

How do you post code in blue like that?

Heh .. the code is green here .. guess we have a different 'theme' enabled. But for posting code like that, use the forum codes. You can find them here: http://www.blitzbasic.com/faq/faq_entry.php?id=2
Using =true would make no difference, as Adam explained, so it was moving the instruction inside the loop that solved it, and offcourse using (x) instead of (aliencount).
Outside the loop, 'x' is undefined. Say, after a loop "FOR x=0 to 10", normally 'x' would be 11. However, I would just use 11 instead of 'x' to be sure.

[quote]as Adam explained[/qoute]
AAAAARGH!!!!!!! It's not Adam, it's ADAM!!! IT SAYS THAT IN MY SIGNATURE, FOR PETE'S SAKE!!! LOOK, RIGHT THERE!!! NOT THERE, THERE!!!!! UNDER MY EMBLEM!!!!! WHY CAN'T PEOPLE UNDERSTAND THE ALL-CAPS THEME?!?!?! AAAAAAAGH!!!!!!!!!

o_0

Ahem. Sorry. If you haven't already figured it out, I'm just slightly insane. Slightly. ^_^

lol @ ADAM.


Thanks for the help guys. I just discovered the insectoids sample game in blitz and I've been trying to study the use of types there. The code isn't commented very well but I have been able to gain a little bit of insight into them.And of course I'm already confused and having some trouble but I'm going to play around with it a bit more before bugging you all for more help.

Thanks again!

Here's an example for you... If you like kitties! lol

;Keep in mind that every variable of type kitty and person isn't ACTUALLY the kitty.  It's only a container variable (a.k.a. pointer) 
;that holds the kitty/person that exsits SOMEWHERE ELSE.  However, many variables can also hold the SAME kitty at the same time.  

SeedRnd(MilliSecs())
Type person ;Tell the computer what variables a container of type person holds
	Field kitties.kitty[99] ;this is just another way to declare an array, but it can be used inside of types.  The .kitty means that
							;this variable can hold other containers of type kitty (it can hold 99 kitties).  
	Field firstname$
	Field lastname$
	Field age
End Type

Type kitty ;Tell the computer what variables a container of type kitty holds
	Field colorofkitty$
	Field weightofkitty
	Field nameofkitty$
	Field id
	Field ownerofkitty.person ;this container can hold another container of type person.  
End Type

oldlady.person = New person ;create a new person that is held by oldlady
	;Tell the computer about oldlady.  Right now, all of the variables held by oldlady are empty.  
	oldlady\firstname$="Martha"
	oldlady.person\lastname="Love"
	oldlady\age=Rand(70,120)

For ICountkitties = 0 To 49 ;count to 50 kitties
	kitten.kitty=New kitty ;Create a new kitty
		kitten\Colorofkitty$=getrandomcolor$() ;get a random color for the kitty
		kitten\weightofkitty=Rand(3,20) ;get a random weight for the kitty
		kitten\nameofkitty=getrandomname$() ;random name
		kitten\ownerofkitty=oldlady ;set the owner of the kitty (which is another container) to oldlady
		oldlady\kitties[ICountKitties]=kitten ;Set one of the old ladies containers in the kitties type to the kitty we just made
		kitten\id=ICountKitties
Next

For thiskitty.kitty = Each kitty ;go through each and EVERY kitty that we have made so far, setting the container this kitty
										;to each as we go through them
	If thiskitty <> Null;Check to make sure the kitty exsits
		If thiskitty\ownerofkitty <> Null Then Print "Some old lady named " + thiskitty\ownerofkitty\firstname + " " + thiskitty\ownerofkitty\lastname + " has a(n) " + thiskitty\Colorofkitty + " kitty named " + thiskitty\ownerofkitty\kitties[thiskitty\id]\nameofkitty+"."
		;check to make sure that the kitty has an owner.  
		;after that notice that we can access the same variables as the 
			;oldlady variable used above by adding a \(variable) To thiskitty\ownerofkitty.  
		;We can take this a step further by adding \kitties[id] which is a pointer to other kitties.  That makes 
			;thiskitty\ownerofkitty\kitties[thiskitty\id] INTERCHANGABLE with thiskitty.  And just to prove this...
		If thiskitty=thiskitty\ownerofkitty\kitties[thiskitty\id] Then 
			Print "Print They're equal!  I win!"
		Else
			Print "Welp, I guess I'm just stupid, and wrong!"
		End If
	End If
Next

WaitKey()

Function getrandomcolor$()
	Select Rand(10)
	Case 0
		Return "Black"
	Case 1
		Return "Red"
	Case 2
		Return "Orange"
	Case 3
		Return "Green"
	Case 4
		Return "Blue"
	Case 5
		Return "Yellow"
	Case 6
		Return "Indigo"
	Case 7
		Return "Violet"
	Case 8
		Return "White"
	Case 9
		Return "Invisible"
	Case 10
		Return "Grey"
	End Select
	Return "*Non-existant*";This will never happen because all of the possibillities rand could put out have already been handled
							; and each of them returned (jumped out of) the function
End Function

Function getrandomname$()
	Select Rand(25)
	Case 0
		Return "Maggie"
	Case 1
		Return "Max"
	Case 2
		Return "Molly"
	Case 3
		Return "Lady"
	Case 4
		Return "Sadie"
	Case 5
		Return "Lucy"
	Case 6
		Return "Daisy"
	Case 7
		Return "Ginger"
	Case 8
		Return "Abby"
	Case 9
		Return "Sasha"
	Case 10
		Return "Sam"
	Case 11
		Return "Rocky"
	Case 12
		Return "Buster"
	Case 13
		Return "Casey"
	Case 14
		Return "Cody"
	Case 15
		Return "Duke"
	Case 16
		Return "Charlie"
	Case 17
		Return "Jack"
	Case 18
		Return "Harley"
	Case 19
		Return "Rusty"
	Case 20
		Return "Toby"
	Case 21
		Return "Murphy"
	Case 22
		Return "Shelby"
	Case 23
		Return "Sparky"
	Case 24
		Return "Barney"
	Case 25
		Return "Winston"
	End Select
	Return "BAD KITTY";Once Again, this will never happen
End Function


ow i'm sorry adam i must have missed it./

S'okay, don't sweat it. By the way, have you ever been run over by a fifty-foot eighteen-wheeler? I could arrange for it, if you'd like. jk lol ;)

18-wheels ? :) ok, ok, that is convincing enough. i give up, Gigamaster, you are right.