An easier way to attach guns to characters

From what I learned in the “Unreal Engine Blueprint Game Developer Online Course”, there’s an easier way to attach guns to characters without using construction scripts.

What you do is add or move the gun mesh and make it a child of the character mesh. This allows you to set a Parent Socket here instead of in a construction script (Note: You would still have to find the right socket manually). Once you set the parent socket though, it will either snap perfectly in place or you may just have to adjust the X,Y,Z locations in small increments to make it fit exactly.

In this video, the Gun was just added normally as a child of the capsule component so you may have some problems setting a parent socket correctly and that’s likely why a Construction Script was done.

That seems so much easier. Yes, my transform on the gun was way off and I had to reset it but then it was perfect. Why wouldn’t we have done this??

Perhaps this was just used as a touch point to allow it to illustrate attaching via script and the difference of using a construction script?

This isn’t touched on but consider this…

What if we wanted to spawn the player but they had a custom loadout, with guns using different relative location/rotation offsets depending on the model of the gun?

You can still handle this with the method used in the Blueprint course, but the best place for the code would be in the construction script.

1 Like

The construction script should really only be used for, well construction time, it’s fired in the editor/gameplay every single time the actor is constructed, not spawned or created, which is fine for characters that’d need a pre-determined (by the developer) loadout in a single player scenario, but (although out of scope in this course) in multiplayer, the construction script could/will lead to unintended side effects with networking/replication, essentially, the best use of the construction script is to setup/modify existing components and/or variables, not add new ones.

With that in mind, I think the way the course demonstrates from the BeginPlay event at runtime after the character has been spawned, etc, teaches a more reusable and adaptable approach to handling gameplay logic such as picking up/spawning and attaching weapons mid-gameplay.

Despite this course being targeted at being better suited to total beginners, I think a far better way to demonstrate this overall would have been going more into class inheritance and using an Actor blueprint style approach for weapons to teach the basics and power of pre-definining any template logic in a parent blueprint and overriding it in the children (BaseWeapon, M4, etc, etc), which are a perfect use-case for per-weapon hand offsets/sockets and the many other factors that change from weapon to weapon.

I realize this was a lengthy response but as someone who has been developing in Unreal for several years now (thanks to the “Unreal bread and butter” I learnt in the original course by Ben and Sam), I felt like sharing some insight and experience regarding things I wish I’d also learnt way sooner.

2 Likes

Privacy & Terms