Ah ha. Ok, that post was very helpful in understanding.
The “OnComponentHit” did a bit of the work for you with the can. It says “When this is hit, do this with the thing that hit it”. You could do the same thing with the generator, I suppose. Add a “OnComponentHit” and then you’ll have the player passed in which you can cast and modify values.
But if you want this to work with the “F” key, I would start with the character blueprint, not the generator. I think it’s a bit more organized to have all the inputs at the character blueprint level so it’s easier to find them later.
Ok, so… first thing’s first. Is the character going to be pressing “F” to activate a large number of objects or just this one? If a large number of objects (or even more than 1, really) will need to be activated with “F”, I would use an interface. It’s not a bad idea to make one anyways just in case you want to use it in the future.
Interfaces… like I said in an earlier post, it’s basically a way to standardize a set of events that can be used. Think of it as a face plate on an electronic device. You create a face plate that has 2 buttons “On” and “Off”. Every electronic device can be turned on and off, so you can put this face plate on all of them. Now when you actually want to turn something on and off, you don’t need to know what the device is… you just need to know that it’s using that face plate.
So let’s make the interface. Right click in the content browser and create a new interface blueprint. You can call it whatever you want, but it can be as general as “InteractionInterface” and you can use it for a large number of objects. Add one function, can call it like “Activate”. I would also add a single input with the type of “Character”. So now this interface can be used with any object you want the player to be able to interact with, just like the face plate on the electronic devices.
There’s two parts that need to be done. We need to tell the character how to use the interface (what does the character need to do in order to access the “button”?) and we need to put the interface on the object.
So lets go into the character blueprint. Here, you can add your “F” event.
This part is a little tricky because we need to find the generator. I would personally do this in a Single Line Trace from the camera to activate the object the player is facing, but your current implementation simply activates the object the player is near (within the collision box). So in order to replicate how yours works, we need a node that I believe is called something like “Get Colliding Actors”. I’m at work and don’t have access to the editor at the moment, so that name might not be 100% accurate, but if you search for “collide” or “collision”, you should find it pretty easily.
Anyways, this function returns an array, a list of all actors the player is colliding with. If the player is within the collision box of the generator, the generator will be in this array. The reason I’d use a single line trace instead of this method is it ensures you will only get one object instead of an array of all objects near the player, but … it is what it is.
Ok, so you have your array of objects. I would attach a “For Each” loop to the array, then for each object, call the “Activate” function defined in the interface. You should also create a reference to “self” and pass it into the character input pin on the function call. So this basically says “Try to press the button on the interface and tell the object I’m the one that pressed it”. It will try even if the interface doesn’t exist for that object, but that’s just fine. It will only work when you intended it to.
So far, we have the character, presses “F” and runs the “Activate” function for all actors he is colliding with. We don’t have any actors actually using this interface yet though, so that’s coming up next. One note here, if you had more than 1 generator next to each other to the point where their collision boxes overlapped and the player could be within both at the same time, it will call the “Activate” on both of them. To prevent this, you could break that “For Each” loop after the first “Activate” is called, or change it to a single line trace.
The next step is to put the face plate on the device. So go to the generator blueprint, open the blueprint properties (button on the top bar somewhere), scroll down to where you see “Interfaces” and add the “InteractionInterface”. Compile and save. Now if you right click in the event graph, you should be able to add the “Activate” event. This is what is called when the character presses “F”. You should also see the character output pin on that function. That will contain the character that activated the generator. You can now treat this exactly like you did with the fuel can and the OnComponentHit event. You can access the character’s current fuel and reduce it to 0.
Phew, that took longer than I had planned. Anyways, let me know if you have any questions.
Edit: If you don’t want to use an interface, you would do the “Get Colliding Actors”, run the “For Each” loop, cast the actor to your Generator and execute a custom event you create on the Generator blueprint. It’s much less dynamic, but if you’re sure this is the only place you’ll be using F to activate stuff it might just be easier.