Widget to actor communication

I’m trying to communicate between a widget and an actor. My approach is using BP Interfaces. I cannot get the print string working.



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…

GetActorOfClass( BPPistol )… :slight_smile:

I have tried this before and it does not work…
For some reason it says that “BP_Pistol” is not implementing the interface, even though it’s inherited.


23423222332
The print string does not work.

It won’t inherit the interface from the player, if that’s what you’re wondering?

You have to explicitly implement it in the gun.

“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”).
:wink:

Right, so BP_Pistol doesn’t implement the interface? ( it does need to ).

it does implement the same interface as the widget :slight_smile: (BPI_Weapon_Selection)

If that’s true, then this

image

would work with ‘get actor of class’

I’m just getting more and more confused here :clown_face:

1 If you’re picking the pistol up from the level, you can call the interface on the actor.

2 If you don’t have a pistol yet, you can’t call the interface on it, because it doesn’t exist.

If it’s 2, then you need to spawn a pistol ( or add it as a child actor component ) so you can call the interface on it.

Here’s what I’m trying to do: use the widget to attach different guns to my character

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.

That comment will no doubt draw about 500 people telling you different ways of doing this.

I do know that some people have all the guns and just hide and show them, which I think is a bit nuts.

What makes much more sense is spawning one when you need it, and destroying the other. That’s typically what’s happening when you switch weapons.

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.

Right there :0)

Nope, that’s the problem, you can’t use an interface to spawn the gun, because there isn’t one :slight_smile:

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:
image
and either filter through the children actors by interface, or tag, or casting.

2 Likes

Well spotted.

1 Like

So let me get this straight. I don’t even need interfaces at all. I just have to spawn actor from class, then attach it to my character.

2 Likes

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:

  1. 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 :slight_smile:

  2. 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… :smirk: 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. :smiling_face_with_tear:

1 Like