Array Search Function Problem

BlitzPlus Forums/BlitzPlus Programming/Array Search Function Problem

Mordax_Praetorian(Posted 2004) [#1]
The purpose of the following Function is to search the 25x1 array A$ (with a 0 in the second array dimension) for the entry specified in on$, and return a 1 if it is there, and a 0 if it isnt

Function SearchAnims(on$)
While Not (TNum = 26 And oncheck$ = on$)
oncheck$ = A$(TNum, 0)
TNum = TNum + 1
Wend
If TNum = 26 Then
Return 0
Else
Return 1
EndIf
End Function

The function doesnt do that however, it used to return a 0 weather the entry was present or not until I added the $ signs to on$ and oncheck$, now it complains about the array element being out of bounds

Any help would be much apreciated


skidracer(Posted 2004) [#2]
I don't know if it will fix it but

While Not (TNum = 26 And oncheck$ = on$)

should be

While Not (TNum = 26 Or oncheck$ = on$)


Grey Alien(Posted 2004) [#3]
Try this for a smaller function ...

Function SearchAnims(on$)
For TNum = 0 To 25
If A$(TNum, 0) = on$ Then Return 1
Next
Return 0
End Function


Mordax_Praetorian(Posted 2004) [#4]
Thx for the help but somethings still wrong, it could be one of the other asociated functions, guess I'll have to do some more testing

Thx anyway


Mordax_Praetorian(Posted 2004) [#5]
OK, its time to give a larger description of whats going on

I have 4 functions aimed at managing animations, for this particular animation I am testing with I have to use individual image variables for each frame, there is no getting around that

The details of an animation are stored in 2 arrays, A$(25,1) for 2 strings allowing me to identify the animation and AN(25, 2) in which the second dimension is set out as follows:

0: Millisecs between frame updates
1: Millisecs() on last update
2: Current frame number

To add an animation to the arrays I call this finction:

Function SetAnim(on, what, speed)
A$(ANum, 0) = on
A$(ANum, 1) = what
AN(ANum, 0) = speed
AN(ANum, 1) = MilliSecs
ANum = 0
While Not A$(ANum, 0) = ""
ANum = ANum + 1
Wend
End Function

The test animation is a simple 4 frame walking animation for when the player is walking south, when this happens the above function is called with:

SetAnim("Char1", "S", 2000)

the last number is large for testing purposes, but how big or small it is doesnt seem to make a difference

Another function checks which animations are stored in the array, updates them if neccessary, and draws them to the screen, the function is called at the end of a loop that runs constantly and is present for the express purpose of running check functions, other routines in the loop all work as they should, currently only the walking animation is present in the function

Function PlayAnims()
For CNum = 0 To 25
If Not A$(CNum,0) = ""
If MilliSecs() > AN(CNum,0) + AN(CNum,1) Then
AN(CNum,2) = AN(CNum,2) + 1
AN(CNum,1) = MilliSecs()
EndIf
If A$(CNum,0) = "Char1" Then
If A$(CNum,1) = "S" Then
DrawRoom
If AN(CNum, 2) = 0 Then DrawImage Orient40, 32*LOCX+129, 32*LOCY+65
If AN(CNum, 2) = 1 Then DrawImage Orient4, 32*LOCX+129, 32*LOCY+73
If AN(CNum, 2) = 2 Then DrawImage Orient41, 32*LOCX+129, 32*LOCY+81
If AN(CNum, 2) = 3 Then DeleteAnim(CNum)
EndIf
EndIf
EndIf
Next
End Function

The other functions called here are:
Drawroom:- Draws the current background to the screen and the players sprite if he is stationary
DeleteAnim:- Removes an animation from the arrays

Function DeleteAnim(Num)
For I = 0 To 1
A$(Num,I) = ""
Next
For I = 0 To 2
AN(Num,I) = 0
Next
End Function

In order to be useful the stationary sprites are not drawn if a "Char1" animation is present in the array, this is done with SearchAnim, thanks greyalien for the smaller code

Function SearchAnims(on$)
For TNum = 0 To 25
If A$(TNum, 0) = on$ Then Return 1
Next
Return 0
End Function

In Drawroom, SearchAnims is called like this:

K = SearchAnims("Char1")
If K = 0
Select Orientation
Case 1
DrawImage Orient1, X, Y
Case 4
DrawImage Orient4, X, Y
Case 2
DrawImage Orient2, X, Y
Case 3
DrawImage Orient3, X, Y
End Select
Else
Stop
EndIf

And here lies the problem, SearchAnim NEVER returns a 1, if it did the Else statement would be called and the program would stop, the strange thing is that even with it returning 0s the stationary image Orient4 is not drawn

Assuming that SearchAnims works, it would mean that the animation was removed from the arrays before being searched for, but I dont see how thats possible

Any help at all would be immensely apreciated


Grey Alien(Posted 2004) [#6]
Sorry for the delay, I think the problem is your function
Function SetAnim(on, what, speed)
because you have not specified the variable types Blitz assumes they are integers. You should put a $ sign after the strings as follows:
Function SetAnim(on$, what$, speed)
Without them your array won't have "Char1" in it, it'll have some number (I don't know what). You could put a STOP in your code and run it in debug mode, this would tell you what the duff values are, but why bother if my suggestion fixes it!

Good Luck


Mordax_Praetorian(Posted 2004) [#7]
Thanks for the help

It works now :)