I was following a relatively simple YouTube tutorial (for making a inventory system) for a inventory prototype I'm working on, since I am sort of a novice with GDscript. All was going swimmingly until I tried the code. For the most part, everything works fine, I try to combine anything with "1" (for one material only). It may be important to note I am using a JSON file for my inventory categorization. Here is some of my code;
Here is my main inventory scenes code:
extends Node2D
#vars
var playing = false
var men_open = false
var holding_items = null
onready var inventory_slots = $Grid
const SlotClass = preload("res://scripts/player/Slot.gd")
#scripts
func _ready():
for inv_slot in inventory_slots.get_children():
inv_slot.connect("gui_input", self, "slot_gui_input", [inv_slot])
int_inv()
func _input(_event):
if Input.is_action_just_pressed("e_men"):
if men_open == true:
if playing == false:
playing = true
get_node("bg/fade").play("fadeout")
get_node("Grid/fade").play("fadeout")
#get_tree().paused = false
Global.is_paused = false
men_open = false
elif men_open == false:
if playing == false:
int_inv()
playing = true
get_node("bg").show()
get_node("Grid").show()
get_node("bg/fade").play("fadein")
get_node("Grid/fade").play("fadein")
#get_tree().paused = true
Global.is_paused = true
men_open = true
if holding_items:
holding_items.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 holding_items != null:
if !slot.item: #put held item in slot
slot.put_in_slot(holding_items)
holding_items = null
else: #swap the held item with the item in slot
if holding_items.item_name != slot.item.item_name:
var temp_item = slot.item
slot.take_away_slot()
temp_item.global_position = event.global_position
slot.put_in_slot(holding_items)
holding_items = temp_item
else:
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 >= holding_items.item_quantity:
slot.item.add_item_quantity(holding_items.item_quantity)
holding_items.queue_free()
holding_items = null
else:
slot.item.add_item_quantity(able_to_add)
holding_items.sub_item_quantity(able_to_add)
elif slot.item:
holding_items = slot.item
slot.take_away_slot()
holding_items.global_position = get_global_mouse_position()
func int_inv():
var slots = inventory_slots.get_children()
for i in range(slots.size()):
if PlayerInventory.inventory.has(i):
slots[i].intialize_item(PlayerInventory.inventory[i][0], PlayerInventory.inventory[i])
func _on_fade_animation_finished(anim_name):
playing = false
if anim_name == "fadeout":
get_node("Grid").hide()
get_node("bg").hide()
Here is my "Slot" Code
extends Panel
#vars
var default_tex = preload("res://sprites/inventory/tile_inv.png")
var empty_tex = preload("res://sprites/inventory/trans_tile.png")
var default_style: StyleBoxTexture = null
var empty_style: StyleBoxTexture = null
var item_class = preload("res://scenes/player/inv/Item.tscn")
var item = null
#scripts
func _ready():
default_style = StyleBoxTexture.new()
empty_style = StyleBoxTexture.new()
default_style.texture = default_tex
empty_style.texture = empty_tex
if randi() % 2 == 0:
item = item_class.instance()
add_child(item)
refresh_style()
func refresh_style():
if item == null:
set('custom_styles/panel', empty_style)
else:
set('custom_styles/panel', default_style)
func take_away_slot():
remove_child(item)
var inventory_node = find_parent("Inventory")
inventory_node.add_child(item)
item = null
refresh_style()
func put_in_slot(new_item):
item = new_item
item.position = Vector2(0, 0)
var inventory_node = find_parent("Inventory")
inventory_node.remove_child(item)
add_child(item)
refresh_style()
func intialize_item(item_name, item_quantity):
if item == null:
item = item_class.instance()
add_child(item)
item.set_item(item_name, item_quantity)
else:
item.set_item(item_name, item_quantity)
refresh_style()
And here is what my JSON file contains:
{
"Titanium": {
"ItemCategory": "Resource",
"StackSize": 4,
"Description": "Test A"
-----},
"Gold": {
"ItemCategory": "Resource",
"StackSize": 4,
"Description": "Test B"
-----},
"Drill_D1": {
"ItemCategory": "Tool",
"StackSize": 1,
"Description": "Test C"
-----}
}