I created the “Pistol” variable inside the widget and is a type of “BP_Pistol”(the actor I’m trying to communicate with). I know this is wrong, and I need a reference to my actor. The problem is that “BP_Pistol” is not in the level.
Here’s how things work: An in-game actor opens up this widget when the player interacts with it, then I use the button to add “BP_Pistol” to my character’s socket.
I know I might run into the same problem when trying to make the weapon shoot…
“BP_Pistol” is a child of an actor that has the interface implemented.
I went ahead and created a new actor, added a skeletal mesh and implemented the “BPI_Weapon_Selection” interface. Through this interface, I want to add the actor to my character.
On button pressed → fire an event inside my Pistol actor that attaches it to my character(using “Attach component to component”).
So you have a picture of the gun in the widget, and the player clicks ‘add’.
Then, you either have to spawn an actor, or add a child actor component, and then attach it to the player.
After you’ve done one of the above, you can use an interface to attach the gun to the player. But the gun has to exist before you can attach it. And the gun blueprint has to implement the interface ( not the player ).
So what should I do? Spawn all the guns at the beginning of the game? Or just place them somewhere hidden in the level? I want to be able to switch between guns.
I want to spawn the weapon and add it to my character when I press the widget button, but I cannot use the interface to spawn the weapon through the widget since it’s not spawned.
This is just a vicious cycle.
Nope, that’s the problem, you can’t use an interface to spawn the gun, because there isn’t one
You could use an interface to talk to the player, that’s possible, and tell them to spawn the gun. But you don’t really need an interface to talk to the player, unless you have many sorts of players.
I don’t get what the problem is, actually. You have the list of weapons on screen, the player chooses, and you spawn from an array of class types. After the gun has spawned, you can attach it to the player.
Child actor does not inherit its parent actors interface - perhaps some confusion with that term there.
If you have a parent class that implements an interface, derived child classes will implement the interface.
But putting an actor from a different class as a “child actor” within a blueprint is not the same thing.
To talk to the weapon if it is child actor to the player character, you might say, Get Player Character, then:
and either filter through the children actors by interface, or tag, or casting.
You can do that. But also here is why an interface would be a good thing.
Because you can give the same instruction to all the guns, and they know how to implement it in their own way.
I’m assuming you can attach the gun to the player from the gun BP?
Then using interfaces is a good idea, AND using class inheritance is also useful.
BPI, because you can give one instruction, and they way each gun implements it is different.
Inheritance, because you can put all the code that’s common to all guns in one BP, then inherit that into all your various weapons, and go forward.
It’s a good point that @BIGTIMEMASTER made, which I think might be part of the confusion.
You have more than one place in engine terminology where the concept of ‘parent’ appears.
The two we think you are getting muddled here are:
After you’ve coded a BP, you can right click on it in the content browser and choose ‘make child blueprint class’ ( or something similar ). This is class inheritance. All the code of the parent will be in the child, including the interface
When you are writing a BP and you put a ‘child actor component’ in it, then naturally, the outer BP is called the parent, and the other, a child. But there is node code copying between them, whatsoever.
Inheriting the interface from the “BP_Weapon_Master” to “BP_Pistol” was what I did. The pistol is a child of it’s master… so it naturally inherited the interface. I did it right and I was not confused about that. Still, it’s good to know about it, in case I’ll ever use “child actor components”.
I just did not know I can’t use an interface to spawn actors.