Download

How to access child objects inside a Blueprint?

Hi there.

First off, apologies in advance for what I know must be a n00b question but it is exactly for that reason why I need your assistance, so again, please forgive my n00bness.

I come from a decade of using Unity and am finding the transition to Unreal incredibly hard. Quite literally anything I could do in Unity in under 30 seconds each take me hours or days to figure out in UE4… I am in desperate need of help (since all those 'Turn a box green"or “Pickup and drop this item here” courses are clearly not cutting it… :frowning: )

So what I would have done in Unity is to create my base projectile prefab (the one I would move and which contains the collider), add my 4 projectile meshes as disabled child objects of the prefab then add them into a GameObject] array. Then, when I spawn the prefab, I would call an init(int) function that determines it’s speed, strength and mesh and with that being the case I could then simply do this:


meshes[index].SetActive(true);

Done!

What I have done so far in UE4 is I created the base BP, added the projectile meshes to it as static meshes, added arrays for movement speed and whatnot to be applied to the parent BP, configured the game’s input to spawn the BP and then passing an int value to the init function to determine all it’s properties. So far everything is working perfectly fine, the prefab spawns, it moves in the scene, all is well. Only one step remains: Show the correct mesh (right now I show all 4).

So I create a function called DisplayRelevantMesh, I created an array of type StaticMesh and then… then I find I can’t drag child objects of the BP into the array. As soon as I click on the mesh it deselects the array… When I try to select elements from the drop down it seems to want me to select something new to spawn instead of finding something that is already inside the BP…

So after about an hour of struggling with “How do I drag objects into an array” I finally gave up and said “Fine, every single time I spawn a projectile I will do finds and casting operations and just generally do a lot of overbloated code to simply enable one mesh and not the rest”… only to find that I can’t find any code to actually help me FIND the child objects of the mesh either… So I am now completely lost

I am thinking a quick shortcut would be to change this approach and rather just spawn a static mesh after I spawn the empty parent BP but this feels like a hack and I’m sure I will need this functionality again in future so I would rather find out the RIGHT way of doing this.

Can someone please share with me how to do the equivalent of that Unity code above inside a BP? I would be ever so grateful!

Thanks in advance

Could you briefly describe what the end goal is.

Do tell if I got it right - you want a projectile whose meshes you can swap on the fly and whose variables can be tweaked.

Yeah, you got that almost exactly right.

My game has exactly 4 types of projectiles. They don’t have their values tweaked as the game progresses but instead each projectile is more powerful than the last and unlocked as the game progresses. This means that each projectile’s values are hard coded directly inside the BP and not even publicly exposed.

Since all projectiles WORK the same in the background but the player only SEES a different mesh I figured it would make sense to store the movement speed and direction in which the projectile rotates while being hurled as array elements along with the meshes. The index just selects the projectiles’s properties from that hard coded list of values and job done…

When you left click I trigger SpawnProjectile(0) and when you right click I trigger SpawnProjectile(1) etc. That int value then sets up the properties I mentioned above, and when the projectile makes contact with an appropriate target it acts as a score multiplier: Score += (Score * projectile.Level)

And that is basically it. This is not something I would have thought to be difficult and yet, while I am able to do everything I intended to do, selecting which mesh to show is the one part that still has me stuck.

Edit: Just to add, on the target end I have a BP that keeps track of the targets on the firing range. This BP has an public array of type BP_target which I setup inside the editor. With this array of target objects readily available on the BP_TargetRange object it makes looping over the targets childsplay. It couldn’t be any easier to loop over the targets to test or change their active state.

What I am trying (and failing) to do now is exactly the same except I want to have a BP contain it’s children and have an array pointing to those children so I can loop over it. Again, I suppose an easy workaround is to make the meshes array public and instead of having one BP that contains it’s child objects I could add meshes to a public array in the editor and only spawn the one I want when the time comes… so this is not an insurmountable challenge. The question I cam here to ask is simply this:

If I WANT to keep the meshes as children inside the parent BP and I wanted to loop over the child meshes, how do I add those children to an array I can then loop over? It seems making public arrays to set in the editor is a piece of cake but making an array that points to child objects that are already in the BP, that seems to be impossible…

I find it hard to believe that such a simple thing could be impossible so I must just be doing it wrong. Let me give you another example… Say you wanted to loop over every bone in a skeleton for some reason (say the knee was hit and now you want to do something with all bones under that knee), surely you can’t mean to tel me that it is impossible for a BP to fetch all bones and store them inside an array… Surely the only solution can’t be to make a public array and then manually drag each and every single bone into the array by hand?

This is why I said that I am able to create workarounds for my current mesh spawning issue but I might have use of internal arrays like this in future also so I am trying to figure out what the right way is for a BP to fetch it’s child objects.

If all you want is a different mesh, you can do it like so:

That’s the Construction Script above. Mesh, SomeStat, and AnotherStat are all exposed.

When you spawn a projectile…

…you can see those exposed pins (right click and *Refresh *the node if they’re not showing). Populate them with the relevant data. Each projectile instance will spawn with those values.

This above was taken in the FPS template.

That Select element would sure make work of this current BP’s issues, thanks!

…but (my edited post went life while you were replying), is there was way to fetch those child objects programatically instead of manually dragging them into the select? Like the example I said above: What if I have 206 bones I want to add to such a Selectelement?

Non the less, your solution will work perfectly for my current (and non-theoretical use case) so thank you very much! :slight_smile:

I’d load up an Excel exported cvs file into a Data Table - you can then fetch stuff from a data table by Name. This could work well for a larger number of elements. [HR][/HR]
edit: Regarding the edited post.

Never worked with bones so the above makes little sense in this context. But there’s a bunch of nodes that spit out arrays:

Annotation-20191119-131701.png

Perhaps that’s what you’re looking for. You are also free to manipulate arrays any way you need.

“Get components by…”
I think that might be what I was looking for. I’ll investigate that some more!
Thank you very , very much for your help!