How to target the object BP your character is "holding"

Hello! I’m working on a third person project where I want my character to be able to pick up an object, let’s say puzzlepiece for simplicity, and attach it to sockets on a wall. The puzzlepieces are all duplicates of eachother, and will spawn when killing an enemy in game. Then, for the placing event, I want to add them to the sockets in a specific order (first to socket 1, then 2, then 3 etc). But I only want you to be able to place the puzzlepiece your character has attached to their hand socket (the piece your “holding”). Currently, I have a line traces set up so that I can attach any of the duplicate pieces to my character, and for the placing event, I have a boolean check after the line trace so that you can only attach a puzzlepiece if your character “is holding” one. But the problem is that when I trigger the placing event, it only attaches one of the pieces (the “original” piece, i.e. not a duplicate). I’m really new to all forms of programming and have a hard time figuring this biz out. Any advice is helpful!

Oh right, my blueprints for y’all:

The grab event, in the puzzlepieces’ BP

83525-grabcodeevent.png

The line trace to trigger grab event in characters BP

The place (attach to wall) event in the walls BP

83528-placode.png

The line trace to trigger place event in characters BP

Hi tamakerii i think/hope i can help, i think the reason why only the first ‘Code Piece’ is being used/attached is because the array variable of ‘Code Piece’(s) is going through the get node and you’re always getting the first one in the list, which counts up from 0, 1, 2, 3 etc.

One way you i think could add the puzzle pieces to socket 1, 2 then 3 is to right click on the green pin on the get node and promote it to a variable called ‘Which Socket’, set the value to 0 so it will get the first code piece in the array which is 0, after you’ve set ‘is Filled?’ add a get ‘Which Socket’ node, drag out of it and add a float + float node and change is so the maths is get ‘Which Socket’ + 1, then drag out from the output of that lasts node and make a set ‘Which Socket’

What this should do is the first time you call ‘PlaceCode’ you attach ‘Code Piece’ 1, then you set the next code piece to be attached is 2, and the next time you call you attach code piece 2.

Hi call95, thank you for the answer! Just one question; will this make the player able to pick up and place any of the code pieces in the sockets? Since they’re going to drop randomly from enemies, I don’t know which one the player will attach first, and I want the player to be able to attach any of the code pieces and have them be placed in the order of the sockets. Just to clarify, I have 6 code pieces and 6 sockets, so if the player were to pick up code piece 4 first, I still want it to attach to socket 1 (if that isn’t filled, ofcourse).

And also, I’m still uncertain as to how this will make it so that the code piece that the character is “holding” is the one that gets placed. Am I just missing something? I’m a complete noob so I’m sorry if I sound totally confusing :')

I think you can do something like this;

Make an integer variable in you piece blueprint called ‘piece number’ and make it editable and expose on spawn so that you can change the number of the piece when it’s in the world or when enemies drop/ spawn them

Make another integer variable in your character blueprint and set this in the code piece blueprint after you’ve attached the code piece to your character, so whatever number piece number you picked up is the piece you have

In the interact wall blueprint after you’ve called ‘place code’ make a ‘switch on int’ node with exec pins connecting to ‘attach actor to component’ nodes with different slots

Make a ‘which slot next?’ int variable which will change the selection of the slots

Change the value of ‘which slot next?’ by setting it to 1 after you’ve used slot 0 which is now filled

And the ‘code piece’ that you ‘GET’ depends on what piece you’re holding if you get the ‘piece i’m holding’ from your character blueprint

So this should make sure the slots are filled like 1, 2, 3 and that you attach whatever piece you’re holding to the wall

Hope this helps :slight_smile:

Thank you! Now it works out perfectly with the pieces ending up in the right sockets, but it’s still not placing the piece that I’m holding… Do I have to set the piece number I’m holding in some way? I have placed six code pieces in my level, just to try this out, and numbered them 1-6. And when I grab one of them, at random, it still places one that I haven’t grabbed… Do you know why this could be happening? I feel like we’re on the right track here so it might just be me messing something up along they way…

When I say set the piece number I’m holding I mean in the character BP. Currently it is just a variable in my character BP but I haven’t get/set it in my event graph, and otherwise I have my blueprints set up just like yours…

Hi call, as I figured, I had messed it up along the way. It was my code pieces numbers that didn’t match up with the array in the wall. Now I fixed that and everything works out just as it should! One quick last question, do you know how I will be able to number them once they spawn from the enemies?

Hi tamakeri, glad to know it’s working, i think you can change the number of the piece after spawning it by dragging out from the return value of the ‘spawn actor’ node and using a ‘set piece number’ variable and just type in whatever number you want it to be after it spawns.