#Take items and drop them

82 messages · Page 1 of 1 (latest)

vague plinth
#

You gotta deconstruct the problem and solve it's parts, so here are the steps:

  • get object that you are pointing at
  • parent the object to a holding object that is a child of the player that is a bit in front of the player
  • unparent the object to release
summer vortex
#

i've found a tutorial and kind of made it working...

#

but i've got one problem:

#

i cant make it drop the picked item

#
extends RayCast3D

@onready var hold_pos = $Hold_Position
@onready var is_held = false

@onready var old_pos
@onready var old_rot

func _physics_process(delta):
    if self.is_colliding():
        var collider = self.get_collider()
        var obj_col = collider.get_parent()
        #print(obj_col)
        
        if Input.is_action_just_pressed("player_interaction") && is_held == false:
            old_pos = obj_col.get_global_transform()
            old_rot = obj_col.rotation_degrees
            
            obj_col.get_parent().remove_child(obj_col)
            hold_pos.add_child(obj_col)
            
            obj_col.transform.origin = Vector3(0.8,1.2,-1)
            obj_col.rotation_degrees = Vector3(0,0,0)
            is_held = true
        if Input.is_action_just_pressed("player_interaction") && is_held == true:
vague plinth
summer vortex
#

yeah, i dont really have an idea how to make it drop the item

#

the tutorial was just about interacting with objects, and thats why i can only pick up items

#

and i dont really know how to make it drop..

vague plinth
#

and parent it to the world

#

like it was before

summer vortex
#

yeah thats also a problem, how do i $ the root?

#

(just like @onready var hold_pos = $Hold_Position but for the root)

vague plinth
#

or use this get_tree().root.get_child(0),
though I prefer the first solution

summer vortex
#

okay,

#
        if Input.is_action_just_pressed("player_interaction") && is_held == true:
            hold_pos.remove_child(obj_col)
            get_tree().root.get_child(0).Scene.add_child(obj_col)
            
            is_held = false
#

just tried this code block, and when i try to pick something up (in this case a cube) it give me the error

#

btw, this is my file-tree:

vague plinth
#

which is 100% fine

summer vortex
#

whoops

vague plinth
#

so there probably is another error

summer vortex
#

that aint the error

#

when i try to pickup the cube the game crashess and i get:

vague plinth
#

get_tree().root.get_child(0).add_child(obj_col) is probably fine

summer vortex
#

okay, now when i try too pick up the cube, it teleports away

#

wait lemme make a video

#

cant interact with it anymore after it teleported

#

maybe its because theres no delay? i mean when i press E on the cube, its instantly picked up, and maybe then its instantly dropped?

#

i dunno

vague plinth
#

You need to add an else

#

so it doesnt pick up and drop in the same press

#

because you set held to true so the second if goes through

summer vortex
#

alright, now i have no error, but cant drop it too

#
extends RayCast3D

@onready var hold_pos = $Hold_Position
@onready var is_held = false

@onready var old_pos
@onready var old_rot

func _physics_process(delta):
    if self.is_colliding():
        var collider = self.get_collider()
        var obj_col = collider.get_parent()
        #print(obj_col)
        
        if Input.is_action_just_pressed("player_interaction") && is_held == false:
            old_pos = obj_col.get_global_transform()
            old_rot = obj_col.rotation_degrees
            
            obj_col.get_parent().remove_child(obj_col)
            hold_pos.add_child(obj_col)
            
            obj_col.transform.origin = Vector3(0.8,1.2,-1)
            obj_col.rotation_degrees = Vector3(0,0,0)
            is_held = true
        else:# Input.is_action_just_pressed("player_interaction") && is_held == true:
            hold_pos.remove_child(obj_col)
            get_tree().root.get_child(0).add_child(obj_col)
            
            is_held = false
#

@vague plinth nvm, when i look at the cube i get errors

vague plinth
#
if Input.is_action_just_pressed("player_interaction"):
  if is_held == false:
            old_pos = obj_col.get_global_transform()
            old_rot = obj_col.rotation_degrees
            
            obj_col.get_parent().remove_child(obj_col)
            hold_pos.add_child(obj_col)
            
            obj_col.transform.origin = Vector3(0.8,1.2,-1)
            obj_col.rotation_degrees = Vector3(0,0,0)
            is_held = true
  else:
            hold_pos.remove_child(obj_col)
           get_tree().root.get_child(0).add_child(obj_col)
            is_held = false

Your if structure is wrong, try something more like that

#

rn you are telling it to drop it everyframe you are not pressing the interaction button and arn't holding anything

#

so it tries to drop something it doesn't have and goes boom i'm pretty sure

summer vortex
#

okay, this time there are actually no errors (it dont go boom) and i cant drop it

#

@vague plinth do you want the project files?

vague plinth
#

Send your code

summer vortex
#

okay :)

#
extends RayCast3D

@onready var hold_pos = $Hold_Position
@onready var is_held = false

@onready var old_pos
@onready var old_rot

func _physics_process(delta):
    if self.is_colliding():
        var collider = self.get_collider()
        var obj_col = collider.get_parent()
        #print(obj_col)
        
        if Input.is_action_just_pressed("player_interaction"):
            if is_held == false:
                old_pos = obj_col.get_global_transform()
                old_rot = obj_col.rotation_degrees
            
                obj_col.get_parent().remove_child(obj_col)
                hold_pos.add_child(obj_col)
            
                obj_col.transform.origin = Vector3(0.8,1.2,-1)
                obj_col.rotation_degrees = Vector3(0,0,0)
                is_held = true
            else:
                hold_pos.remove_child(obj_col)
                get_tree().root.get_child(0).add_child(obj_col)
                is_held = false
vague plinth
summer vortex
#

nah

#

im kinda a beginner

#

(not a beginner programmer, but to godot)

vague plinth
#

When the game is running, there is a remote button that allows you to inspect how the hierarchy is looking

#

So you can see what happens to your held object

summer vortex
#

before pickup:

#

after pickup:

#

after my drop try nothing happends changes

vague plinth
#

Can you put a print inside the else to see if it doesn't reach it for some reason

summer vortex
#
        if Input.is_action_just_pressed("player_interaction"):
            if is_held == false:
                print("if works!")
                old_pos = obj_col.get_global_transform()
                old_rot = obj_col.rotation_degrees
            
                obj_col.get_parent().remove_child(obj_col)
                hold_pos.add_child(obj_col)
            
                obj_col.transform.origin = Vector3(0.8,1.2,-1)
                obj_col.rotation_degrees = Vector3(0,0,0)
                is_held = true
            else:
                print("else works!")
                hold_pos.remove_child(obj_col)
                get_tree().root.get_child(0).add_child(obj_col)
                is_held = false
#

it only prints if works!

vague plinth
#

Weird

summer vortex
#

yeah

#

somehow the else one worked recently

#

in a older code block

vague plinth
#
if Input.is_action_just_pressed("player_interaction"):
            print("test")
            if is_held == false:
                print("if works!")
                old_pos = obj_col.get_global_transform()
                old_rot = obj_col.rotation_degrees
            
                obj_col.get_parent().remove_child(obj_col)
                hold_pos.add_child(obj_col)
            
                obj_col.transform.origin = Vector3(0.8,1.2,-1)
                obj_col.rotation_degrees = Vector3(0,0,0)
                is_held = true
            else:
                print("else works!")
                hold_pos.remove_child(obj_col)
                get_tree().root.get_child(0).add_child(obj_col)
                is_held = false

Try seeing if it gets inside the first if

#

maybe you have something else that I can't see preventing the code from reaching here

summer vortex
#
 
test
if works!
--- Debugging process stopped ---
#

(i tried dropping it and then closed the program)

vague plinth
#

Can you show the full file ?

summer vortex
#
extends RayCast3D

@onready var hold_pos = $Hold_Position
@onready var is_held = false

@onready var old_pos
@onready var old_rot

func _physics_process(delta):
    if self.is_colliding():
        var collider = self.get_collider()
        var obj_col = collider.get_parent()
        #print(obj_col)
        
        if Input.is_action_just_pressed("player_interaction"):
            print("test")
            if is_held == false:
                print("if works!")
                old_pos = obj_col.get_global_transform()
                old_rot = obj_col.rotation_degrees
            
                obj_col.get_parent().remove_child(obj_col)
                hold_pos.add_child(obj_col)
            
                obj_col.transform.origin = Vector3(0.8,1.2,-1)
                obj_col.rotation_degrees = Vector3(0,0,0)
                is_held = true
            else:
                print("else works!")
                hold_pos.remove_child(obj_col)
                get_tree().root.get_child(0).add_child(obj_col)
                is_held = false
summer vortex
#

@vague plinth my big brain solved the issue (ironic)

#

it could only drop, when the raycast was colliding with the item!

#

yeah

#

thanks!

vague plinth
#

Nice, happy to help

summer vortex
#

@vague plinth nvm

#

its pretty broken