Inventory Transactions - coldrockgames/gml-raptor GitHub Wiki
Transaction Data
The Transaction data is a data structs that is going to be returned as a result, when the transaction has been executed. It contains the following properties:
- inventory: The inventory instance on which the transaction was executed.
- owner: Reference to the owner of the transaction, usually the owner of the inventory performing the action.
- items_before: Array of item instances before the transaction occurred.
- sub_transactions: Array of any sub-transactions triggered during execution
- begin_time_stamp: Timestamp when the transaction started execution.
- end_time_stamp: Timestamp when the transaction completed execution.
- success_state: Enum/string indicating the outcome of the transaction (success, partial success, failure due to constraints).
- description: Optional human-readable description of the transaction, useful for logging or debugging.
- new_length: Number of items in the inventory after the transaction.
- new_weight: Total weight of items in the inventory after the transaction.
- new_count: Total count of items after the transaction, accounting for stack sizes.
Transactions
Add Transaction
/// @func add_item(_item, _stack_size = stack_size)
/// @desc Adds an item into the inventory, splitting it into stacks if needed.
/// Respects the inventory’s maximum size, weight, and stack constraints.
/// Returns data about items added and whether the operation succeeded.
add_item = function(_item, _stack_size = stack_size) {
Remove Transaction
/// @func remove_item(_item, _is_removing_all = false)
/// @desc Removes an item or stack of items from the inventory.
/// If _is_removing_all = true, deletes all matching items.
/// Returns the number of items that could not be removed.
remove_item = function(_item, _is_removing_all = false) {
Move Transaction
/// @func move_item(_item, _inventory, _is_moving_all = false)
/// @desc Moves an item (or multiple items) to another inventory.
/// Validates both inventories’ constraints.
/// Returns the number of items that could not be moved.
move_item = function(_item, _inventory, _is_moving_all = false) {
Buy Transaction
/// @func buy_item(_item, _inventory, _available_money)
/// @desc Buys an item from another inventory (seller) using available currency.
/// Checks if the buyer has enough money, moves the item, and returns the change.
buy_item = function(_item, _inventory, _available_money) {
Split Transaction
/// @func split_item(_item, _stack_size)
/// @desc Splits a stack of items into smaller stacks of given size.
/// Returns the newly created stacks.
split_item = function(_item, _stack_size) {
Join Transaction
/// @func join_items(_item1, _item2, _stack_size = stack_size)
/// @desc Joins two item stacks together, respecting maximum stack size.
/// If the stack limit is exceeded, splits the overflow into a new stack.
join_items = function(_item1, _item2, _stack_size = stack_size) {
Swap Transaction
/// @func swap_items(_item1, _item2)
/// @desc Swaps the positions of two items in the same inventory.
/// Returns success state after swapping.
swap_items = function(_item1, _item2) {
Join or Swap Transaction
/// @func join_or_swap_items(_item1, _item2, _stack_size = stack_size)
/// @desc If both items have the same name, joins them; otherwise swaps them.
/// Useful for drag-and-drop inventory UIs.
join_or_swap_items = function(_item1, _item2, _stack_size = stack_size) {
Clean Up Transaction
/// @func clean_up()
/// @desc Reorganizes the inventory by merging partial stacks and splitting oversized stacks.
/// Iterates over all items and normalizes the inventory.
clean_up = function() {
Slot Inventory Transactions
Add Or Swap Item To Slot Transaction
/// @func add_or_swap_item_to_slot(_item, _slot_name)
/// @desc Adds an item to a specific named slot in a SlotInventory.
/// If the slot is empty, the item is placed directly.
/// If the slot already contains an item, the two items are swapped.
/// Returns a _TransactionData object with the result of the operation.
add_or_swap_item_to_slot = function(_item, _slot_name) {