In my game, I want the player to be able to “hold hands” with another character whenever I hold the H key, and stop holding hands when I release it. My plan is to use a pinjoint2D and assign my player and the other character as the nodes of the joint. Trouble is, I can’t figure out how to assign the nodes in code, and if I assign them in the inspector, the joint already exists when I run the game, but I only want them to exist when I press H. Here is my function:
You will only want hold hands and the setting of a and b to be called once, so thise should happen as result of a _input or more preferably _unhandled_input.
Second whenever you assign the nodes to the pin they will be jointed where they currently are. So to get a real holding hand effect, you need to have the hands in position before you assign the nodes to the joint. This can be done by simply posting the nodes where you want just before you assign the pin nodes.
Maybe third, looking at the node paths, it seems like the two nodes are children of the joint? This seems a little backwards, but may not matter. I would typically have the joint in one of the hands, most likely the players hand, if they will grap other things with the pin joint.
The other option is have the joint as siblings to the two nodes.
Player
Joint
Girl
# or
Player
Joint
Girl
# order doesn't really matter
Player
Girl
Joint
Joint
Girl
Player
#Etc.
What I ended up doing was making a pair of exported variables and set them to the two physicsbody2D’s (called “boy” and “girl”), and then used this function:
This kind of works, as it sets the nodes of the joints when I press the “hold hands” button. But there is a problem: I want the two bodies to be joined as long as I’m holding the button, and then unjoin them when I release the button. My first thought was to call queue_free on the joint, but I figured that wouldn’t work because it destroys the joint, and I can’t join the bodies again if the joint is destroyed, which I was correct about. How else might I go about unjoining the nodes when I release the button?
Setting the nodes to an empty string did the trick! Thank you so much
I’m a little bit confused about _unhandled_input and why I should use that instead of is_action_pressed and _is_action_just_released. I read the docs and I still don’t quite get it, could you elaborate on why it’s preferable?
It allows you to disentangle input logic with behavioral logic. This is nice if you want to reuse input logic for another type of character that has different behavior.
And at least for input in general there is an order of operations for _input and _unhandled_input. Where input can be used for gui elements which you can mark an event as consumed stopping the propagation down to other nodes.
If no gui handles the event then it is passed to nodes that define the unhanded_input like your player object.
If you use input is action pressed in a process function you will get behavior on the player even if you are using a menu that you may want the input to be focused on.
But for prototypes it’s fine to use the Input Singletons to poll input state.
Thank you so much for the thorough explanation. I’m coming from Unity so I’m still learning Godot while trying to unlearn my Unity habits, so this kind of explanation really helps.