Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | I3randon5 |
Hello, I’m trying to create a inventory system using C# but seem to be struggling on the drag and drop part. For some reason it doesn’t want to let me get GetGrabData etc. I have made it in GdScript which works perfectly fine but struggling to convert it. I’m also confused on converting the data part, I think it’s a dictionary, but struggling converting that also. If someone can help I would greatly appreciate it as I have been stuck on it for awhile (struggling to find any info online to help with this)
C# Script
//Pick up item when click and hold with mouse in inventory
public override Godot.Object GetDragData(Vector2 pos)
{
int itemIndex = GetIndex();
ItemResource item = inventory.RemoveItem(itemIndex);
if(item is ItemResource)
{
//internal List<ItemResource> data = new List<ItemResource>();
var data = new Dictionary <Godot.Object, int>();
//data.item = item;
data.Add(item, itemIndex);
//Show what item your dragging
TextureRect dragPreview = new TextureRect();
dragPreview.Texture = item.icon;
//Center the icon to mouse
Control c = new Control();
c.AddChild(dragPreview);
dragPreview.RectPosition = new Vector2(item.icon.GetSize()) * -0.5f;
var preview = c;
//set and show what item you are dragging when centered
this.SetDragPreview(preview);
//setting drag data to allow item to return to slot when dropped outside of a non placeable position
inventory.dragData = data;
return data;
}
return null;
}
//when mouse is hovering over a area that item can be placed (inventory slot)#
//Can it be placed?
public bool CanDropData(Dictionary<Godot.Object, int> data)
{
int itemIndex = GetIndex();
ItemResource item = inventory.RemoveItem(itemIndex);
//return data.ContainsKey(inventory.dragData.has);
return data is Dictionary<Godot.Object, int> && data.ContainsKey(item);
}
//Drop item when holding with mouse in inventory#
public override void DropData(Vector2 position, Dictionary<Godot.Object, int> data)
{
int myItemIndex = GetIndex();
ItemResource myItem = inventory.items[myItemIndex];
inventory.dragData = null;
//if the items name is the same and is stackable stack it
if(myItem is ItemResource && myItem.stackable == true)//and is equal to same item name?
{
myItem.amount += data[myItem];
inventory.EmitSignal("ItemsChanged", myItemIndex);
}
else //if item is not the same swap it
{
inventory.SwapItems(myItemIndex, data[myItem].amount);
inventory.SetItems(myItemIndex, data[myItem]);
}
}
GDScript
func DisplayItem(item):
if item: #if item is a item
#setting item amount to label and converting to a string
itemQuantityLabel.text = str(item.amount)
#setting icon
itemSlotIcon.texture = item.icon
else: #if item is not an item then it has no icon or text quantity
itemSlotIcon.texture = null
itemQuantityLabel.text = ""
#Pick up item when click and hold with mouse in inventory#
func get_drag_data(_position):
var itemIndex = get_index()
var item = inventory.RemoveItem(itemIndex)
if item:
var data = {}
data.item = item
data.itemIndex = itemIndex
#Show what item your dragging
var dragPreview = TextureRect.new()
dragPreview.texture = item.icon
#Center the icon to mouse
var c = Control.new()
dragPreview.rect_position = -0.5 * item.icon.get_size()
c.add_child(dragPreview)
#set and show what item you are dragging when centered
set_drag_preview(c)
#setting drag data to allow item to return to slot when dropped outside of a non placeable position
inventory.drag_data = data
return data
return null
#when mouse is hovering over a area that item can be placed (inventory slot)#
func can_drop_data(_position, data):
return data is Dictionary and data.has("item")
#Drop item when holding with mouse in inventory#
func drop_data(_position, data):
var myItemIndex = get_index()
var myItem = inventory.items[myItemIndex]
inventory.DragData = null
#if the items name is the same and is stackable stack it
if myItem and myItem.name == data.item.name and myItem.stackable == true:
myItem.amount += data.item.amount
inventory.emit_signal("ItemsChanged", [myItemIndex])
else: #if item is not the same swap it
inventory.SwapItems(myItemIndex, data.itemIndex)
inventory.SetItems(myItemIndex, data.item)