strange Function behaviour?!

Blitz3D Forums/Blitz3D Programming/strange Function behaviour?!

FlagDKT(Posted 2008) [#1]
Graphics3D 640,480,32,0

Function Test(bCounter=0,Result=0)
	For i=1 To 10
		bCounter=bCounter+1
		If i=5 Then
			Result=i
			DebugLog "Found-->"+Result
		EndIf
		bCounter=Test(bCounter,Result)
	Next
	Return Result
End Function

DebugLog Test()

While Not KeyDown( 1 )
Wend
End


That function make Blitz explode immediately. :D


FlagDKT(Posted 2008) [#2]
Is that the reason why my little Function to get a bone Index by name(without using Globals) ,doesn't work?..How to get it work?

Function getBoneIndexByName(ent,BoneName$,bCounter=0,Result=0)
	For i=1 To CountChildren(ent)
		child=GetChild(ent,i)
		If Result=0 Then 
			bCounter=bCounter+1 ;stop the counter since we found the index
		EndIf
		If Lower(BoneName$)=Lower(EntityName(child)) Then 
			Result=bCounter
		EndIf
		bCounter=getBoneIndexByName(child,BoneName$,bCounter,Result)
	Next
	Return bCounter
End Function



GfK(Posted 2008) [#3]
Your function is exponentially calling itself, resulting in a stack overflow.


big10p(Posted 2008) [#4]
Yes, recursion meltdown.


FlagDKT(Posted 2008) [#5]
ops...that's true :)
And what about the second function ?

How to retrieve a bone index, by its name, without using globals outside the function?

edit:
Uhm...I think it's useless to find a bone index, since I cannot use getchild(mesh,boneIndex) to retrieve a bone handle.
I should use a recursive function, or findchild.. too slow.


Stevie G(Posted 2008) [#6]
Is there a reason you're not using the native FindChild function? It was designed for this very purpose so will be plenty fast enough.


FlagDKT(Posted 2008) [#7]
A StringCompare is very slow.


Bobysait(Posted 2008) [#8]
This code is extracted from my animation library, where vertice need BoneIndex to setup weightmaps , i think it must be the same kind of function you want

+> Return the Bone (using Index)
/!\ the function self-return <0 to get the number of bones in the branch else, if return is >0 then it is the Bone pointer
So, if the index is higher than the global countchildren, then, it will return <0
Eventually you will want to check:
Bone=GetSubChild(Entity,Index)
If Bone>0 ...

Function GetSubChild%(Entity%,BoneId%,Count%=0)
	If Count=BoneId	Return Entity
	Count=Abs(Count)+1
	For n=1 To CountChildren(Entity)
		Bone=GetSubChild(GetChild(Entity,n),BoneId,Count)
		If Bone>0 Return Bone
		If Bone<0 Count=-Bone
	Next
	Return -Count
End Function


If you pass BoneIndex=0 then it will return the Entity... not useless ^^, but that is probably the fastest way to do it without global variables


Stevie G(Posted 2008) [#9]

A StringCompare is very slow.



Clearly, if you want to get an index from a name then you've no option in the matter.

Are you not storing the entity references to the bones / children you need once you've found them anyway? Maybe I'm missing the point, can you explain?


FlagDKT(Posted 2008) [#10]
Bobysait:
We had the same idea, infact I created something similar to your function :)
Function getBoneIndexByName(ent,BoneName$,bCounter=0)
	For i=1 To CountChildren(ent)
		child=GetChild(ent,i)
		If bCounter>=0 Then bCounter=bCounter+1
		If Lower(BoneName$)=Lower(EntityName(child)) Then 
			bCounter=-bCounter
		EndIf
		bCounter=getBoneIndexByName(child,BoneName$,bCounter)
	Next
	Return bCounter
End Function


Stevie G:
I need to attach a particle emitter to a named bone. I see however that I cannot find the bone index by his name "on the fly". So, like you said, I decided to create a bone table array for each Entity of the same kind I create. It works.