i cant seem to figure out how to add pick/unlock a sword in my game,can someone help me?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By LJ INFINITY

so yeah i want make a simple weapon switching system and i cant seem to figure it out please help me
my entire code:(do you mind looking at the whole code while u r at it cause i made most of it myself and dont know if its good)

extends KinematicBody2D

var speed = 250
var GRAVITY = 600.0
var velocity = Vector2()
var jumpforce = -350
var hand = false
var sword = false
var sword_obtained = false
var bow_obtained = false

func _ready():
hand = true

func _process(_delta):
velocity.y += _delta * GRAVITY

if Input.is_action_pressed("left"):
	velocity.x = -speed
	if is_on_floor():
		if sword_obtained == false:
			$AnimatedSprite.play("run")
	$AnimatedSprite.flip_h = true
elif Input.is_action_pressed("right"):
	velocity.x =  speed
	if is_on_floor():
		if sword_obtained == false:
			$AnimatedSprite.play("run")
	$AnimatedSprite.flip_h = false
else:
	velocity.x = 0
	if is_on_floor():
		if sword_obtained == false:
			$AnimatedSprite.play("idle")
		elif sword_obtained == true:
			$AnimatedSprite.play("idle_sword_obtained")
		elif sword == true:
			$AnimatedSprite.play("idle_sword")
		
		
if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y = jumpforce
	if sword_obtained == false:
			$AnimatedSprite.play("jump")
if is_on_floor() == false:
	speed = 300
if is_on_floor():
	speed = 250
if Input.is_action_pressed("ui_down"):
	GRAVITY = 1200.0
elif Input.is_action_just_released("ui_down"):
	GRAVITY = 600.0

move_and_slide(velocity, Vector2(0, -1))


if Input.is_action_just_pressed("hand"):
	hand = true
	sword = false
if Input.is_action_just_pressed("sword") and sword_obtained == true:
	hand = false
	sword = true
	if Input.is_action_just_released("sword") and sword_obtained == true:
		$AnimatedSprite.play("sword_draw")
	

func pickup():
if Input.is_action_just_pressed(“take”):
sword_obtained = true

func _on_Area2D_body_entered(body):
if “pedestal” in body.name:
pickup()

and BTW i’m new to this engine and gamedev stuff

:bust_in_silhouette: Reply From: Thomas Karcher

How is picking up the sword supposed to work? Because right now, it would only work if you enter a special area and press the “take” action at exactly the same time. It would probably be better if the body_entered and body_exited events would only set a flag like “is_sword_pickable” (set to true when entering, set to false when exiting). Then you can check for the “take” action in the _input function and add the sword only if is_sword_pickable is true.

sorry but can u tell me how to use the exited function please

oh i figured it out
ok tnk u :slight_smile:

LJ INFINITY | 2021-04-23 04:31