Godot Version
v4.4.1.stable
Question
My code for grabbing a object works, however my code for letting the object go doesn’t; how do I fix this?
extends Area2D
var alreadyholding = false
func _process(delta: float) -> void:
pass
func _on_body_entered(body: Node2D) -> void:
if body.is_in_group("holdable"): ## kinematic body is "body"
if Input.is_action_pressed("Climb") and alreadyholding == false: ## if action "climb" is pressed and checks if a body is already being held
alreadyholding = true ## sees if player is holding a body
body.set_collision_layer_value(1, false) ## sets collision layer to false
body.set_collision_mask_value(1, false) ## sets collision mask to false
body.reparent($HoldingHoldable) ## sets the kinematicbody's parent to the marker (Holding Holdable)
body.global_position = $HoldingHoldable.global_position ## grabs the kinematicbody and sets the position to the marker (Holding Holdable) on top of the player head
body.freeze = true ## freezes block physics
elif Input.is_action_just_released("Climb") or $"..".wallclimbmode == true: ## if action "climb" is released or player is wallclimbing (thing that is not working)
print("dawg") ## print "dawg" in output
alreadyholding = false ## not holding block
body.freeze = false ## unfreezes block physics
body.set_collision_layer_value(1, true) ## sets collision layer to true
body.set_collision_mask_value(1, true) ## sets collision mask to true
body.global_position = body.global_position ## lets go of the kinematicbody
pass # Replace with function body.
You have an outer test for is_in_group()
, but once you press climb you reparent it…
So is reparenting the problem?
I change the code to remove reparenting, but now the object isn’t moving with the player. Also when I release the “climb” button, it still ignores it.
I found a solution myself, but thanks for the help! Here’s my code:
extends Area2D
var alreadyholding = false
func _process(delta: float) -> void:
if Input.is_action_just_pressed("Climb"):
if monitorable and monitoring: #rechecking if any grabbable objects are in range
monitorable = false
monitoring = false
if !monitorable and !monitoring:
monitorable = true
monitoring = true
if $HoldingHoldable.get_child_count(1): # Finds object currently held by the player in the marker's children
if Input.is_action_just_released("Climb"): # Throws object
if alreadyholding == true: #Check if it holding object
$HoldingHoldable.get_child(0).set_collision_layer_value(6, true) ## sets collision layer to false
$HoldingHoldable.get_child(0).set_collision_mask_value(6, true) ## sets collision mask to false
$"..".grabbing = false
if $"../AltruSprite".flip_h == false and not Input.is_action_pressed("ui_up") and not Input.is_action_pressed("ui_down"):
$HoldingHoldable.get_child(0).linear_velocity.x = 400 ## throws to the right if facing right
$HoldingHoldable.get_child(0).linear_velocity.y = -600
##throws object forward no vertical direction is held
elif $"../AltruSprite".flip_h == true and not Input.is_action_pressed("ui_up") and not Input.is_action_pressed("ui_down"):
$HoldingHoldable.get_child(0).linear_velocity.x = -400 ## throws to the left if facing left
$HoldingHoldable.get_child(0).linear_velocity.y = -600
##throws object forward no vertical direction is held
elif Input.is_action_pressed("ui_up") and $"../AltruSprite".flip_h == false and not Input.is_action_just_pressed("ui_down"):
$HoldingHoldable.get_child(0).linear_velocity.x = 100 ## throws to the right if facing right
$HoldingHoldable.get_child(0).linear_velocity.y = -900
##throws object up if "ui_up" is held
elif Input.is_action_pressed("ui_up") and $"../AltruSprite".flip_h == true and not Input.is_action_just_pressed("ui_down"):
$HoldingHoldable.get_child(0).linear_velocity.x = -100 ## throws to the left if facing left
$HoldingHoldable.get_child(0).linear_velocity.y = -900
##throws object up if "ui_up" is held
elif Input.is_action_pressed("ui_down") and $"../AltruSprite".flip_h == false and not Input.is_action_pressed("ui_up"):
$HoldingHoldable.get_child(0).linear_velocity.x = 100 ## throws to the right if facing right
$HoldingHoldable.get_child(0).linear_velocity.y = -20
##puts object down if "ui_down" is held
elif Input.is_action_pressed("ui_down") and $"../AltruSprite".flip_h == true and not Input.is_action_pressed("ui_up"):
$HoldingHoldable.get_child(0).linear_velocity.x = -100 ## throws to the left if facing left
$HoldingHoldable.get_child(0).linear_velocity.y = -20
##puts object down if "ui_down" is held
print("dawg") ## still here to check
alreadyholding = false ## not holding object
$HoldingHoldable.get_child(0).freeze = false ## unfreezes object's physics
$HoldingHoldable.get_child(0).reparent($"../..") ## unadopts object and brings it back to the Node 2D scene
pass
pass
func _on_body_entered(body: Node2D) -> void:
if body.is_in_group("holdable"): ## object is "body"
if Input.is_action_pressed("Climb") and alreadyholding == false: ## if action "climb" is pressed and checks if a object is already being held
alreadyholding = true ## sees if player is holding a object
$"..".grabbing = true
body.linear_velocity.x = 0 ## resets the velocity of object
body.linear_velocity.y = 0
body.set_collision_layer_value(1, false) ## sets collision layer to false
body.set_collision_mask_value(1, false) ## sets collision mask to false
body.set_collision_layer_value(6, false) ## sets collision layer to false
body.set_collision_mask_value(6, false) ## sets collision mask to false
body.reparent($HoldingHoldable) ## sets the object's parent to the marker (Holding Holdable)
body.global_position = $HoldingHoldable.global_position ## grabs the object and sets the position to the marker (Holding Holdable) on top of the player head
body.freeze = true ## freezes object physics
pass # Replace with function body.