Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | ziomkobra |
Hello, I made a simple pickup system but I’m not 100% glad about it.
Here’s code:
func pickup():
if reach.is_colliding():
if reach.get_collider().is_in_group("AK"):
weapon_to_spawn = ak.instance()
elif reach.get_collider().is_in_group("Knife"):
weapon_to_spawn = knife.instance()
elif reach.get_collider().is_in_group("Grenade"):
weapon_to_spawn = grenade.instance()
elif reach.get_collider().is_in_group("AWP"):
weapon_to_spawn = awp.instance()
elif reach.get_collider().is_in_group("Axe"):
weapon_to_spawn = axe.instance()
else:
weapon_to_spawn = null
else:
weapon_to_spawn = null
if Input.is_action_just_pressed("interact"):
if hand.get_child(0) == null:
if weapon_to_spawn != null:
hand.add_child(weapon_to_spawn)
reach.get_collider().queue_free()
weapon_to_spawn.rotation = hand.rotation
else:
if hand.get_child(0).is_in_group("AK"):
weapon_to_drop = ak.instance()
elif hand.get_child(0).is_in_group("Knife"):
weapon_to_drop = knife.instance()
elif hand.get_child(0).is_in_group("Grenade"):
weapon_to_drop = grenade.instance()
elif hand.get_child(0).is_in_group("AWP"):
weapon_to_drop = awp.instance()
elif hand.get_child(0).is_in_group("Axe"):
weapon_to_drop = axe.instance()
get_parent().add_child(weapon_to_drop)
weapon_to_drop.global_transform = hand.global_transform
weapon_to_drop.dropped = true
hand.get_child(0).queue_free()
This function works in a process(delta), and yes I know it’s kinda shitty. So here’s my question - how to improve it? Maybe more accurately how to do it without all of this instance stuff and how to connect player’s node and script with gun’s node and script?
When I add player script as a global varabiale it won’t work. I tried adding a separete code to the reach node (it’s raycast) to make it act like a weapon handler or like a bridge between gun and player but it didn’t quite work.