Jump to content

Recommended Posts

Posted (edited)

Adding items to the player inventory is kinda hard, so I decided to tell everyone how to do it since I know a lot of people don't know how.
These are the basics.
The functions are basically taken right from the game scripts, but I have modified them to be more simplified.

First you need to add these structures.
Note that script struct and array variables are aligned to 8 bytes.
 

struct sGuid
{
	alignas(8) int data1;
	alignas(8) int data2;
	alignas(8) int data3;
	alignas(8) int data4;
};

struct sSlotInfo
{
	alignas(8) sGuid guid;
	alignas(8) int f_1;
	alignas(8) int f_2;
	alignas(8) int f_3;
	alignas(8) int slotId;
};

struct sItemInfo
{
	alignas(8) int f_0;
	alignas(8) int f_1;
	alignas(8) int f_2;
	alignas(8) int f_3;
	alignas(8) int f_4;
	alignas(8) int f_5;
	alignas(8) int f_6;
};



Then you will need to add these functions:

 

// Creates an empty GUID instance
sGuid CreateNewGUID()
{
	sGuid guid{};
	return guid;
}

// Gets an item's GUID from the inventory
sGuid GetPlayerInventoryItemGUID(u32 item, sGuid guid, u32 slotId)
{
	sGuid outGuid{};
	INVENTORY::INVENTORY_GET_GUID_FROM_ITEMID(1, (Any*)&guid, item, slotId, (Any*)&outGuid);
	return outGuid;
}

// Gets the players inventory GUID
sGuid GetPlayerInventoryGUID()
{
	return GetPlayerInventoryItemGUID(MISC::GET_HASH_KEY("CHARACTER"), CreateNewGUID(), MISC::GET_HASH_KEY("SLOTID_NONE"));
}

// Gets an item's group hash (eInvItemGroup)
u32 GetItemGroup(u32 item)
{
	sItemInfo info{};

	if (!ITEMDATABASE::_ITEMDATABASE_IS_KEY_VALID(item, 0))
	{
		return 0;
	}
	if (!ITEMDATABASE::ITEMDATABASE_FILLOUT_ITEM_INFO(item, (Any*)&info))
	{
		return 0;
	}
	return info.f_2;
}

// Gets an item's slot info data
sSlotInfo GetItemSlotInfo(u32 item)
{
	sSlotInfo slotInfo{};

	slotInfo.guid = GetPlayerInventoryGUID();
	slotInfo.slotId = MISC::GET_HASH_KEY("SLOTID_SATCHEL");

	u32 group = GetItemGroup(item);
	switch (group)
	{
	case 0xC2286F01: // CLOTHING
		if (!INVENTORY::_INVENTORY_FITS_SLOT_ID(item, MISC::GET_HASH_KEY("SLOTID_WARDROBE")))
		{
			slotInfo.guid = GetPlayerInventoryItemGUID(MISC::GET_HASH_KEY("WARDROBE"), slotInfo.guid, MISC::GET_HASH_KEY("SLOTID_WARDROBE"));
			slotInfo.slotId = INVENTORY::_GET_DEFAULT_ITEM_SLOT_INFO(item, MISC::GET_HASH_KEY("WARDROBE"));
		}
		else
		{
			slotInfo.slotId = MISC::GET_HASH_KEY("SLOTID_WARDROBE");
		}
		break;
	case 0x95A6F147: // HORSE
		slotInfo.slotId = MISC::GET_HASH_KEY("SLOTID_ACTIVE_HORSE");
		break;
	case 0x80FB92CD: // UPGRADE
		if (INVENTORY::_INVENTORY_FITS_SLOT_ID(item, MISC::GET_HASH_KEY("SLOTID_UPGRADE")))
		{
			slotInfo.slotId = MISC::GET_HASH_KEY("SLOTID_UPGRADE");
		}
		break;
	default:
		if (INVENTORY::_INVENTORY_FITS_SLOT_ID(item, MISC::GET_HASH_KEY("SLOTID_SATCHEL")))
		{
			slotInfo.slotId = MISC::GET_HASH_KEY("SLOTID_SATCHEL");
		}
		else if (INVENTORY::_INVENTORY_FITS_SLOT_ID(item, MISC::GET_HASH_KEY("SLOTID_WARDROBE")))
		{
			slotInfo.slotId = MISC::GET_HASH_KEY("SLOTID_WARDROBE");
		}
		else
		{
			slotInfo.slotId = INVENTORY::_GET_DEFAULT_ITEM_SLOT_INFO(item, MISC::GET_HASH_KEY("CHARACTER"));
		}
		break;
	}
	return slotInfo;
}

// Adds an item to the player inventory via GUID
bool AddItemWithGUID(u32 item, sGuid &guid, sSlotInfo &slotInfo, u32 quantity, u32 addReason)
{
	if (!INVENTORY::_INVENTORY_IS_GUID_VALID((Any*)&slotInfo.guid))
	{
		return false;
	}
	if (!INVENTORY::_INVENTORY_ADD_ITEM_WITH_GUID(1, (Any*)&guid, (Any*)&slotInfo.guid, item, slotInfo.slotId, quantity, addReason))
	{
		return false;
	}
	return true;
}

// Adds an item to the player inventory via hash
// This is the main function you will be calling to add items to your inventory
bool AddItemToInventory(u32 item, u32 quantity)
{
	sSlotInfo slotInfo = GetItemSlotInfo(item);
	sGuid guid = GetPlayerInventoryItemGUID(item, slotInfo.guid, slotInfo.slotId);
	return AddItemWithGUID(item, guid, slotInfo, quantity, MISC::GET_HASH_KEY("ADD_REASON_DEFAULT"));
}



Now to actually add an item to the player inventory, all you need to call is AddItemToInventory()

Like so:

 

AddItemToInventory(MISC::GET_HASH_KEY("CONSUMABLE_CORNEDBEEF_CAN"), 1);
AddItemToInventory(MISC::GET_HASH_KEY("PROVISION_TALISMAN_BUFFALO_HORN"), 1);
// etc



To remove an item from the player inventory is way easier and can be done with a single native call:

 

INVENTORY::_INVENTORY_REMOVE_INVENTORY_ITEM_WITH_ITEMID(1, MISC::GET_HASH_KEY("CONSUMABLE_CORNEDBEEF_CAN"), 1, MISC::GET_HASH_KEY("REMOVE_REASON_DEFAULT"));



And that's how you add/remove items from the player inventory.
Any questions, ask me and I'll try to answer them.

Edited by TuffyTown
Fix code error
  • Like 2
  • Thanks 1
  • 2 months later...
Posted

Thank you a lot for that! Where exactly did you find these? I'm a relative beginner and I'm not sure what you mean by "right from the game scripts". I feel like if I could look into such game scripts I would learn a lot

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...