Inventory Transaction Factory - coldrockgames/gml-raptor GitHub Wiki

InventoryTransactionFactory

Factory for creating and managing inventory transactions. Supports chaining and configuration before execution.

Constructor:

InventoryTransactionFactory(_inventory)

Factory Methods:

  • create_add(_item, _stack_size): Prepares AddTransaction.
  • create_remove(_item, _is_moving_all): Prepares RemoveTransaction.
  • create_move(_item, _target, _is_removing_all): Prepares MoveTransaction.
  • create_buy(_item, _inventory, _available_money): Prepares BuyTransaction.
  • create_split(_item, _stack_size): Prepares SplitTransaction.
  • create_join_or_swap(_item1, _item2, _stack_size): Prepares Join or Swap transaction.
  • create_join(_item1, _item2, _stack_size): Prepares JoinTransaction.
  • create_swap(_item1, _item2): Prepares SwapTransaction.
  • create_clean_up(): Prepares CleanUpTransaction.

Execution Methods:

  • execute(): Executes transaction and returns _TransactionData.
  • try_execute(): Simulates transaction without modifying inventory.
  • repeat_exec(_count, _is_trying): Executes multiple times; stops on failure.
  • get_data(): Returns _TransactionData of last transaction.

Configuration / Chaining:

  • set_inventory(_inventory): Assigns inventory.
  • set_owner(_owner): Sets transaction owner.
  • set_prop(_name, _value): Sets custom property.
  • binder(): Returns binder for stepping back in chain.

Transaction Factory Usage Examples

The InventoryTransactionFactory allows chaining of transactions, configuration of owners, and simulation of transactions using try_execute().

Set Inventory and Execute Add

var fac  = new InventoryTransactionFactory(player_inventory);
var inv1 = fac.get_inventory();
var inv2 = another_inventory;
var item = new InventoryItem("Potion", 10);

var trans_data = fac
    .create_add(item)
    .set_inventory(inv2)
    .execute();

dlog(trans_data.success_state);
dlog(trans_data.new_length);
dlog(trans_data.new_count);
dlog(trans_data.new_weight);

Set Owner and Custom Property

var trans_data = fac
    .create_add(item)
    .set_owner(self)
    .set_prop("test_prop", "Hello World!")
    .execute();

dlog(trans_data.owner);
dlog(trans_data.test_prop);

Repeat Execution with Remove

var trans_data = fac
    .create_add(item)
    .repeat_exec(2)
    .parent()
    .create_remove(item)
    .execute();
dlog(trans_data.new_count);
dlog(trans_data.new_weight);

Move Items with Wildcard

var trans_data = fac
    .create_add(item)
    .repeat_exec(2)
    .parent()
    .create_move(new InventoryItem("*"), inv2, true)
    .execute();
dlog(trans_data.new_count);
dlog(trans_data.unmoveable_count);

Buy Items from Another Inventory

var trans_data = fac
    .create_add(item)
    .repeat_exec(2)
    .parent()
    .create_buy(new InventoryItem("*"), inv2, 750)
    .execute();
dlog(trans_data.new_count);
dlog(trans_data.return_money);

Split and Join Items

var trans_data = fac
    .create_add(item)
    .execute()
    .parent()
    .create_split(inv.get_item(0), 5)
    .execute();

trans_data = fac
    .create_join(inv.get_item(0), inv.get_item(1))
    .execute();

Swap Items

var trans_data = fac
    .create_add(item)
    .execute()
    .parent()
    .create_swap(inv.get_item(0), inv.get_item(9))
    .execute();
dlog(trans_data.new_index1);
dlog(trans_data.new_index2);

Clean Up Inventory

fac.set_inventory(inv3)
    .create_add(item)
    .repeat_exec(2);

var trans_data = fac
    .create_clean_up()
    .execute();
dlog(trans_data.new_length);

Try Execute (Simulation)

var trans_data = fac
    .create_add(item)
    .repeat_exec(2, true);
dlog(inv.items_length());