FindChild not working within For loop

Blitz3D Forums/Blitz3D Programming/FindChild not working within For loop

jhocking(Posted 2006) [#1]
I am currently beating my head against a very annoying problem. In one place in my code I am using FindChild, and it isn't finding the child. Thing is, the command works outside the For loop, but inside it doesn't work. Also, it works the first time through the loop, it's on the second pass that an error happens. Does anyone have any idea why this would be?

This is pissing me off, because I'm pretty sure I'm just missing something obvious. If anyone would like me to send you the code to look at, just email me.


Dreamora(Posted 2006) [#2]
Perhaps you are mixing something with the parent you search the child on.

Normally you need recursion for doing hierarchy research.
Without it you can only find the childs of 1 parent, not the child of a child (or only the first child of each child you find which is quite pointless after all)

Without a code that shows your error, I assume that you assign some bogus to your entity on which you search for childs. (or you search for childs on something you loaded through loadmesh instead of loadanimmesh or on a bone etc)


Naughty Alien(Posted 2006) [#3]
try this by skn3[ac]..its really handy..
at begin of program put this
;This value is used to size the buffer bank below. If the data needs more space,
;it will resize the bank in blocks of the amount below. (in bytes, 1k = 1024 bytes)
Const recursive_resize=1024

;This bank is used in each call to the search function. It is outside the function,
;as creating and deleting over and over from memory, can cause fragmentation, not...
;to mention slow downs.
Global recursive_bank=CreateBank(recursive_resize),recursive_size=recursive_resize

;These are misc values, having them defined as global speeds up the function as...
;they don't need to be created/destroyed each time the function is called
Global recursive_entity,recursive_parent,recursive_id,recursive_start,recursive_total,recursive_offset



;The function
;It will return the entity if found, or 0 if not.
;MyChild=findchildentity(entity,"child name")
Function findchildentity(entity,name$)
name$=Lower$(name$)
recursive_parent=entity
recursive_start=1
recursive_offset=0
.recursive_label
recursive_total=CountChildren(recursive_parent)
For recursive_id=recursive_start To recursive_total
recursive_entity=GetChild(recursive_parent,recursive_id)
If name$=Lower$(EntityName$(recursive_entity))
Return recursive_entity
Else
If recursive_offset+8 > recursive_size-1
ResizeBank(recursive_bank,recursive_size+recursive_resize)
recursive_size=recursive_size+recursive_resize
End If
PokeInt(recursive_bank,recursive_offset,recursive_id+1)
PokeInt(recursive_bank,recursive_offset+4,recursive_parent)
recursive_offset=recursive_offset+8
recursive_start=1
recursive_parent=recursive_entity
Goto recursive_label
End If
Next
If recursive_offset=0
Return 0
Else
recursive_start=PeekInt(recursive_bank,recursive_offset-8)
recursive_parent=PeekInt(recursive_bank,recursive_offset-4)
recursive_offset=recursive_offset-8
Goto recursive_label
End If
End Function


jhocking(Posted 2006) [#4]
No dude, the need for recursion doesn't apply to FindChild. As I said, it works fine outside the loop, and even works the first time through the loop, it's on the second pass that it fails. Specifically, I put a FindChild for testing immediately before For, and then again immediately after, and throw in a Stop so that I can watch the variables changing.

This would obviously mean that I must be doing something to the parent entity somewhere in the body of the loop, but I cannot find it. I must be going blind.

EDIT: Whew, okay after venting a bit I was able to look at my code with calmer eyes, and saw the problem. Within the loop, I was using EntityParent to reassign the child. When I commented out that line, FindChild works fine. Now to figure out how to accomplish what I was trying to do with EntityParent, but in a different way...