Godot Version
4.5.1.stable
Question
I am working on Item pickup mechanics for my project, and I have an Area2D node to sense when the player is inside of a CollisionShape2D node. When the player is within the range, they are able to pick up an item.
The problem is that when the scene starts, if you don’t move within the range of an item, you can immediately use the click function (input from right mouse button) to get as many items as you want, even though you aren’t in range to get any.
Any help would be good help, and I’m a bit new to syntax-based coding so please be patient with me and use noobie words if you can, thank you!
P.S. Tell me if there is any code that you would need to help fix the problem, I will provide as quickly as I can!
Hotbar script code:
extends Control
@onready var in_area = GlobalVariables.in_area
@onready var rng = RandomNumberGenerator.new()
@onready var RNoutput = null
#I removed the inventory slot variables because it takes up a lot of space here, EmptySlot below is a variable for the next open slot in sequential order.
#region toggle hotbar visibility, interact, inventory visibility
func click():
change_scene()
var EmptySlot = decide_number()
if EmptySlot != null:
var Item_Instance = chosen_item.instantiate()
EmptySlot.add_child(Item_Instance)
#region input events
func _input(event):
# INTERACT
if GlobalVariables.in_area:
if event.is_action_pressed("Interact"):
click()
Item script code:
extends Node2D
#region create variables
@onready var colshape = $Area2D2/colshape2
@onready var sprite = $Sprite2D
@onready var scenenumber = 1
#endregion
#region set variable for player range boolean
@onready var in_area = false
#endregion
#region show mouse button if player is or isnt in range, set the variable too
func _on_area_2d_body_entered(_body: Node2D) -> void:
$RightMouseClickButton.visible = true
in_area = true
GlobalVariables.in_area = true
func _on_area_2d_body_exited(_body: Node2D) -> void:
$RightMouseClickButton.visible = false
in_area = false
GlobalVariables.in_area = false
#endregion
#region looking if player is in range to get item into inventory CLICK FUNC
func click():
queue_free()
func _input(event):
if in_area:
if event.is_action_pressed("Interact"):
click()