First of all, I would like to clarify that I am a beginner and I have been using Unreal Engine for 2 weeks.
I have a problem trying to integrate blueprints with C++ code.
Regarding the architecture of my project, I started from the principle that I should create a C++ class for each blueprint (where the blueprint will inherit from this class).
However, the issue is that if I create a blueprint that needs to inherit from another blueprint, then my workflow won’t be possible.
Here’s an example where I have 2 classes, Gun and Rifle, which inherit from Weapon. I do have a blueprint that inherits from each corresponding class in C++.
What I’d like is for AGun and ARifle to inherit from BP_Weapon so that when I create, for example, a BP_Gun, the code from BP_Weapon is executed. In terms of architecture, it would look like this.
I understood that it was not possible for a C++ class to inherit from a blueprint. As a result, I find myself stuck and I’m not sure how to architect my project to make blueprints and C++ coexist. Do you have any advice or ideas?
Once you are in the blueprint world you are pretty much stuck in it. In general I tend to do as much as I can in C++ and only do the last step in blueprints. So all the base classes, everything that could be abstract, are a good candidate for C++. Then I create BP classes out of that which defines data like the actual used meshes, tags and so on. I add class specific components like audio components or additional meshes that are not being used in the base classes. Also I often bind to dispatchers of my used components here. I rarely have any abstract BP classes or a more than 2 levels deep blueprint inheritance. In my opinion there’s no real need for a “every BP class should have a C++ parent” rule.
I agree with @pladux on his answer, that you should try to do as much as you can in C++, but depending on what your use case is, this might actually be a candidate for using Data Assets. I’m not specifically sure why you want to have a BP_Weapon that is a Blueprint while you want AGun and ARifle to inherit from it, but that could be because you’re setting up properties or configuring them in BP_Weapon in a way that you want to share with BP_Gun and BP_Rifle. If that’s the case, you can do some really cool things where you set up Data Assets to contain variables, structs, and even functions that you can set up in pre-configured ways.
You don’t have to have a one-size fits all configuration, either. You can break up properties into different DataAsset presets and plug them in. For Example, you could have UWeaponData that is used by All weapons, but you could have a separate set of properties that are URifleData that is a different data asset and only contains the ones you need for your ARifle and BP_Rifle classes.
Again, I don’t know specifically what you want to be able to do, but usually when I see someone wanting to be able to insert a Blueprint in the middle of C++ hierarchy, it’s about wanting to give users the ability to edit properties on that blueprint so they apply to the children. If you break those properties into Data Assets, you don’t have to worry about this, and can configure all your BP_ weapons with a variety of preset Data Assets that can be adjusted by a user, and any of those adjustments will apply to any BP that uses the same Data Asset preset. Worth exploring if that sounds like you!