So far i have this problem when trying to equip the Weapons,
Weapon.tscn Tree
this is Weapon.gd
extends Node2D
class_name Weapon
onready var playerPath = preload("res://Player.tscn")
onready var animation_player: AnimationPlayer = get_node("AnimationPlayer")
onready var hitbox: Area2D = get_node("Node2D/Sprite/Hitbox")
const M4A1_bulletPath = preload("res://Bullet.tscn")
# true/false on the ground
export(bool) var on_floor: bool = true
var parent_velocity = Vector2()
var velocity = Vector2()
var player_in_range : bool
var player_picked_up : bool
var hasWeapon : bool = false
var ammoAmount: int
var ammoMagazineAmount: int
var bulletSpread: Array = [0,0]
var bullet_speed = -500
var fire_rate: float
var damage: float
var freeze: float
# Attachment Mods
var hasAttachedMod1: bool
var hasAttachedMod2: bool
var hasAttachedMod3: bool
var hasAttachedMod4: bool
var hasAttachedMod5: bool
var isShooting : bool
var isLazer : bool # detect on/ off
#onready var animationPlayer = $AnimationPlayer
#onready var animationTree = $AnimationTree
#onready var animationState = animationTree.get("parameters/playback")
var item_name
var player = null
var being_picked_up = false
func _ready():
item_name = "M4"
func _physics_process(delta):
if being_picked_up == false:
pass
else:
PlayerInventory.add_item(item_name, 1)
queue_free()
hasWeapon = true
#_guns_input()
#_assault_Rifle_Animation()
func pick_up_item(body):
player = body
being_picked_up = true```
which is the Universal Weapon script that M4.tscn inherits Weapon,tscn
M4.tscn inherits the nodes from Weapon.tsn and Weapon.gd as the kinematic body2d
[upl-image-preview url=https://i.imgur.com/ggpcWnU.png]
im trying to make an auto load where i can store the data of the weapons into dictionaries then pass them in when i want to equip them, im not sure how to do this but here is more code
WeaponData.gd which is autoloaded
```extends Node
var ammoAmount: int
var ammoMagazineAmount: int
var bulletSpread: Array = [0,0]
var bullet_speed = -500
var fire_rate: float
var damage: float
var freeze: float
# Attachment Mods
var hasAttachedMod1: bool
var hasAttachedMod2: bool
var hasAttachedMod3: bool
var hasAttachedMod4: bool
var hasAttachedMod5: bool
var isShooting : bool
var isLazer : bool # detect on/ off
#onready var animationPlayer = $AnimationPlayer
#onready var animationTree = $AnimationTree
#onready var animationState = animationTree.get("parameters/playback")
var item_name
var AssaultRifles
var SecondarySideArms
var weaponList = {
AssaultRifles: ['M4'],
SecondarySideArms: ['Tac1']
}
func _ready():
pass # Replace with function body.```
and this
ItemData.gd, where i want to reference ItemData.weapons to get the specs of whatever you're holding. You could keep the item_held variable in the Player script maybe
And match the Player.item_held variable to the ItemData.weapon
here is the UI code
UserInterface.gd
```extends CanvasLayer
var holding_item = null
func _input(event):
if event.is_action_pressed("Inventory"):
$Inventory.visible = !$Inventory.visible
$Inventory.initialize_inventory()
if event.is_action_pressed("scroll_up"):
PlayerInventory.active_item_scroll_down()
elif event.is_action_pressed("scroll_down"):
PlayerInventory.active_item_scroll_up()
func _ready():
pass # Replace with function body.
here is PlayerInventory.gd which has no scene
extends Node
signal active_item_updated
const SlotClass = preload("res://Slot.gd")
const ItemClass = preload("res://Item.gd")
const NUM_INVENTORY_SLOTS = 20
const NUM_HOTBAR_SLOTS = 5
var inventory = {
# 0: ["M4", 1], #--> slot_index: [item_name, item_quantity]
# 1: ["M4", 1] #--> slot_index: [item_name, item_quantity]
}
var hotbar = {
# 0: ["M4", 1],
# 1: ["M4", 1]
}
var active_item_slot = 0
func add_item(item_name, item_quantity):
for item in inventory:
if inventory[item][0] == item_name:
var stack_size = int(JsonData.item_data[item_name]["StackSize"])
var able_to_add = stack_size - inventory[item][1]
if able_to_add >= item_quantity:
inventory[item][1] += item_quantity
update_slot_visual(item, inventory[item][0], inventory[item][1])
return
else:
inventory[item][1] += able_to_add
update_slot_visual(item, inventory[item][0], inventory[item][1])
item_quantity = item_quantity - able_to_add
# item doesn't exist in inventory yet, so add it to an empty slot
for i in range(NUM_INVENTORY_SLOTS):
if inventory.has(i) == false:
inventory[i] = [item_name, item_quantity]
update_slot_visual(i, inventory[i][0], inventory[i][1])
return
func update_slot_visual(slot_index, item_name, new_quantity):
var slot = get_tree().root.get_node("/root/Game/World/YSort/Character/UserInterface/Inventory/GridContainer/Slot" + str(slot_index + 1))
#var slot = get_node("/root/Game/World/YSort/Character/Player/UserInterface/Inventory/GridContainer/Slot" + str(slot_index + 1))
if slot.item != null:
slot.item.set_item(item_name, new_quantity)
else:
slot.initialize_item(item_name, new_quantity)
func remove_item(slot: SlotClass, is_hotbar: bool = false):
if is_hotbar:
hotbar.erase(slot.slot_index)
else:
inventory.erase(slot.slot_index)
func add_item_to_empty_slot(item: ItemClass, slot: SlotClass, is_hotbar: bool = false):
if is_hotbar:
hotbar[slot.slot_index] = [item.item_name, item.item_quantity]
else:
inventory[slot.slot_index] = [item.item_name, item.item_quantity]
func add_item_quantity(slot: SlotClass, quantity_to_add: int, is_hotbar: bool = false):
if is_hotbar:
hotbar[slot.slot_index][1] += quantity_to_add
else:
inventory[slot.slot_index][1] += quantity_to_add
func active_item_scroll_up():
active_item_slot = (active_item_slot + 1) % NUM_HOTBAR_SLOTS
emit_signal("active_item_updated")
if hotbar[active_item_slot][2] == "weapon":
hasWeapon = true # ERROR FINDING THIS
#func active_item_scroll_up():
# active_item_slot = (active_item_slot + 1) % NUM_HOTBAR_SLOTS
# emit_signal("active_item_updated")
func active_item_scroll_down():
if active_item_slot == 0:
active_item_slot = NUM_HOTBAR_SLOTS - 1
else:
active_item_slot -= 1
emit_signal("active_item_updated")```
Here is Inventory.gd
```extends Node2D
const SlotClass = preload("res://Slot.gd")
onready var inventory_slots = $GridContainer
func _ready():
var slots = inventory_slots.get_children()
for i in range(slots.size()):
slots[i].connect("gui_input", self, "slot_gui_input", [slots[i]])
slots[i].slot_index = i
slots[i].slot_type = SlotClass.SlotType.INVENTORY
initialize_inventory()
func initialize_inventory():
var slots = inventory_slots.get_children()
for i in range(slots.size()):
if PlayerInventory.inventory.has(i):
slots[i].initialize_item(PlayerInventory.inventory[i][0], PlayerInventory.inventory[i][1])
func slot_gui_input(event: InputEvent, slot: SlotClass):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT && event.pressed:
if find_parent("UserInterface").holding_item != null:
if !slot.item:
left_click_empty_slot(slot)
else:
if find_parent("UserInterface").holding_item.item_name != slot.item.item_name:
left_click_different_item(event, slot)
else:
left_click_same_item(slot)
elif slot.item:
left_click_not_holding(slot)
func _input(event):
if find_parent("UserInterface").holding_item:
find_parent("UserInterface").holding_item.global_position = get_global_mouse_position()
func left_click_empty_slot(slot: SlotClass):
PlayerInventory.add_item_to_empty_slot(find_parent("UserInterface").holding_item, slot)
slot.putIntoSlot(find_parent("UserInterface").holding_item)
find_parent("UserInterface").holding_item = null
func left_click_different_item(event: InputEvent, slot: SlotClass):
PlayerInventory.remove_item(slot)
PlayerInventory.add_item_to_empty_slot(find_parent("UserInterface").holding_item, slot)
var temp_item = slot.item
slot.pickFromSlot()
temp_item.global_position = event.global_position
slot.putIntoSlot(find_parent("UserInterface").holding_item)
find_parent("UserInterface").holding_item = temp_item
func left_click_same_item(slot: SlotClass):
var stack_size = int(JsonData.item_data[slot.item.item_name]["StackSize"])
var able_to_add = stack_size - slot.item.item_quantity
if able_to_add >= find_parent("UserInterface").holding_item.item_quantity:
PlayerInventory.add_item_quantity(slot, find_parent("UserInterface").holding_item.item_quantity)
slot.item.add_item_quantity(find_parent("UserInterface").holding_item.item_quantity)
find_parent("UserInterface").holding_item.queue_free()
find_parent("UserInterface").holding_item = null
else:
PlayerInventory.add_item_quantity(slot, able_to_add)
slot.item.add_item_quantity(able_to_add)
find_parent("UserInterface").holding_item.decrease_item_quantity(able_to_add)
func left_click_not_holding(slot: SlotClass):
PlayerInventory.remove_item(slot)
find_parent("UserInterface").holding_item = slot.item
slot.pickFromSlot()
find_parent("UserInterface").holding_item.global_position = get_global_mouse_position()
here is slot.gd
extends Panel
var default_tex = preload("res://Assets/GUI/inventory-slot.png")
var empty_tex = preload("res://Assets/GUI/inventory-slot-inactive.png")
var selected_tex = preload("res://Assets/GUI/hotbar_selected_slot.png")
var default_style: StyleBoxTexture = null
var empty_style: StyleBoxTexture = null
var selected_style: StyleBoxTexture = null
var ItemClass = preload("res://Item.tscn")
var item = null
var slot_index
var slot_type
enum SlotType {
HOTBAR = 0,
INVENTORY,
}
func _ready():
default_style = StyleBoxTexture.new()
empty_style = StyleBoxTexture.new()
selected_style = StyleBoxTexture.new()
default_style.texture = default_tex
empty_style.texture = empty_tex
selected_style.texture = selected_tex
# if randi() % 2 == 0:
# item = ItemClass.instance()
# add_child(item)
refresh_style()
func refresh_style():
if SlotType.HOTBAR == slot_type and PlayerInventory.active_item_slot == slot_index:
set('custom_styles/panel', selected_style)
elif item == null:
set('custom_styles/panel', empty_style)
else:
set('custom_styles/panel', default_style)
func pickFromSlot():
remove_child(item)
var inventoryNode = find_parent("UserInterface")
inventoryNode.add_child(item)
item = null
refresh_style()
func putIntoSlot(new_item):
item = new_item
item.position = Vector2(0, 0)
var inventoryNode = find_parent("UserInterface")
inventoryNode.remove_child(item)
add_child(item)
refresh_style()
func initialize_item(item_name, item_quantity):
if item == null:
item = ItemClass.instance()
add_child(item)
item.set_item(item_name, item_quantity)
else:
item.set_item(item_name, item_quantity)
refresh_style()
and here is the 5slot hotbar
Hotbar.gd
extends Node2D
const SlotClass = preload("res://Slot.gd")
onready var hotbar = $HotbarSlots
onready var active_item_label = $ActiveItemLabel
onready var slots = hotbar.get_children()
func _ready():
# for i in range(slots.size()):
# slots[i].connect("gui_input", self, "slot_gui_input", [slots[i]])
# slots[i].slot_index = i
PlayerInventory.connect("active_item_updated", self, "update_active_item_label")
for i in range(slots.size()):
slots[i].connect("gui_input", self, "slot_gui_input", [slots[i]])
PlayerInventory.connect("active_item_updated", slots[i], "refresh_style")
slots[i].slot_index = i
slots[i].slot_type = SlotClass.SlotType.HOTBAR
initialize_hotbar()
update_active_item_label()
func update_active_item_label():
if slots[PlayerInventory.active_item_slot].item != null:
active_item_label.text = slots[PlayerInventory.active_item_slot].item.item_name
else:
active_item_label.text = ""
func initialize_hotbar():
for i in range(slots.size()):
if PlayerInventory.hotbar.has(i):
slots[i].initialize_item(PlayerInventory.hotbar[i][0], PlayerInventory.hotbar[i][1])
func _input(event):
if find_parent("UserInterface").holding_item:
find_parent("UserInterface").holding_item.global_position = get_global_mouse_position()
func slot_gui_input(event: InputEvent, slot: SlotClass):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT && event.pressed:
if find_parent("UserInterface").holding_item != null:
if !slot.item:
left_click_empty_slot(slot)
else:
if find_parent("UserInterface").holding_item.item_name != slot.item.item_name:
left_click_different_item(event, slot)
else:
left_click_same_item(slot)
elif slot.item:
left_click_not_holding(slot)
update_active_item_label()
func left_click_empty_slot(slot: SlotClass):
PlayerInventory.add_item_to_empty_slot(find_parent("UserInterface").holding_item, slot, true)
slot.putIntoSlot(find_parent("UserInterface").holding_item)
find_parent("UserInterface").holding_item = null
func left_click_different_item(event: InputEvent, slot: SlotClass):
PlayerInventory.remove_item(slot, true)
PlayerInventory.add_item_to_empty_slot(find_parent("UserInterface").holding_item, slot, true)
var temp_item = slot.item
slot.pickFromSlot()
temp_item.global_position = event.global_position
slot.putIntoSlot(find_parent("UserInterface").holding_item)
find_parent("UserInterface").holding_item = temp_item
func left_click_same_item(slot: SlotClass):
var stack_size = int(JsonData.item_data[slot.item.item_name]["StackSize"])
var able_to_add = stack_size - slot.item.item_quantity
if able_to_add >= find_parent("UserInterface").holding_item.item_quantity:
PlayerInventory.add_item_quantity(slot, find_parent("UserInterface").holding_item.item_quantity, true)
slot.item.add_item_quantity(find_parent("UserInterface").holding_item.item_quantity)
find_parent("UserInterface").holding_item.queue_free()
find_parent("UserInterface").holding_item = null
else:
PlayerInventory.add_item_quantity(slot, able_to_add, true)
slot.item.add_item_quantity(able_to_add)
find_parent("UserInterface").holding_item.decrease_item_quantity(able_to_add)
func left_click_not_holding(slot: SlotClass):
PlayerInventory.remove_item(slot, true)
find_parent("UserInterface").holding_item = slot.item
slot.pickFromSlot()
find_parent("UserInterface").holding_item.global_position = get_global_mouse_position()