[{"data":1,"prerenderedAt":1056},["ShallowReactive",2],{"doc-global_essentials\u002Fitems\u002Fexchange":3},{"id":4,"title":5,"body":6,"description":1045,"extension":1046,"meta":1047,"navigation":1051,"path":1052,"seo":1053,"stem":1054,"__hash__":1055},"docs\u002Fdocs\u002Fglobal_essentials\u002Fitems\u002Fexchange.md","Exchange",{"type":7,"value":8,"toc":1033},"minimark",[9,14,27,30,35,83,85,89,92,125,132,135,155,158,160,164,171,204,209,271,276,308,311,313,317,324,368,373,486,491,592,594,598,657,663,665,669,744,749,759,789,791,795,854,857,859,863,915,917,921,1006,1008,1012,1029],[10,11,13],"h1",{"id":12},"exchange-system","Exchange System",[15,16,17,18,22,23,26],"p",{},"The exchange system handles two modes of item transfer between inventories: ",[19,20,21],"strong",{},"loot"," (free taking) and ",[19,24,25],{},"trade"," (buying\u002Fselling with currency).",[28,29],"hr",{},[31,32,34],"h2",{"id":33},"two-modes","Two Modes",[36,37,38,54],"table",{},[39,40,41],"thead",{},[42,43,44,48,51],"tr",{},[45,46,47],"th",{},"Mode",[45,49,50],{},"Description",[45,52,53],{},"Use Case",[55,56,57,71],"tbody",{},[42,58,59,65,68],{},[60,61,62],"td",{},[63,64,21],"code",{},[60,66,67],{},"Free item transfer, no currency involved",[60,69,70],{},"Treasure chests, corpse looting, storage",[42,72,73,77,80],{},[60,74,75],{},[63,76,25],{},[60,78,79],{},"Items have prices, currency is exchanged",[60,81,82],{},"Shops, merchants, trading NPCs",[28,84],{},[31,86,88],{"id":87},"opening-an-exchange","Opening an Exchange",[15,90,91],{},"Use actions to open exchanges:",[36,93,94,103],{},[39,95,96],{},[42,97,98,101],{},[45,99,100],{},"Action",[45,102,50],{},[55,104,105,115],{},[42,106,107,112],{},[60,108,109],{},[63,110,111],{},"{loot: \"inventory_id\"}",[60,113,114],{},"Open loot UI with inventory",[42,116,117,122],{},[60,118,119],{},[63,120,121],{},"{trade: \"inventory_id\"}",[60,123,124],{},"Open trade UI with inventory",[15,126,127,128,131],{},"Both actions are ",[19,129,130],{},"event delayed"," - they wait for the current scene to finish before opening.",[15,133,134],{},"The exchange UI shows two inventories side by side:",[136,137,138,149],"ul",{},[139,140,141,144,145,148],"li",{},[19,142,143],{},"Left:"," Party inventory (",[63,146,147],{},"_party_inventory",")",[139,150,151,154],{},[19,152,153],{},"Right:"," Exchange inventory (the one you specified)",[15,156,157],{},"Clicking items moves them between inventories. In trade mode, currency is automatically exchanged.",[28,159],{},[31,161,163],{"id":162},"currencies","Currencies",[15,165,166,167,170],{},"Currencies are regular items with ",[63,168,169],{},"is_currency: true"," set in their template.",[36,172,173,182],{},[39,174,175],{},[42,176,177,180],{},[45,178,179],{},"Field",[45,181,50],{},[55,183,184,194],{},[42,185,186,191],{},[60,187,188],{},[63,189,190],{},"is_currency",[60,192,193],{},"Mark item as currency (appears in currency bar)",[42,195,196,201],{},[60,197,198],{},[63,199,200],{},"price",[60,202,203],{},"Base price in various currencies",[15,205,206],{},[19,207,208],{},"Example currency item:",[36,210,211,220],{},[39,212,213],{},[42,214,215,217],{},[45,216,179],{},[45,218,219],{},"Value",[55,221,222,232,241,251,261],{},[42,223,224,229],{},[60,225,226],{},[63,227,228],{},"id",[60,230,231],{},"gold",[42,233,234,238],{},[60,235,236],{},[63,237,190],{},[60,239,240],{},"true",[42,242,243,248],{},[60,244,245],{},[63,246,247],{},"traits.name",[60,249,250],{},"\"Gold\"",[42,252,253,258],{},[60,254,255],{},[63,256,257],{},"traits.image",[60,259,260],{},"(coin icon)",[42,262,263,268],{},[60,264,265],{},[63,266,267],{},"max_stack",[60,269,270],{},"-1 (unlimited stacking)",[15,272,273],{},[19,274,275],{},"Setting item prices:",[36,277,278,286],{},[39,279,280],{},[42,281,282,284],{},[45,283,179],{},[45,285,219],{},[55,287,288,298],{},[42,289,290,295],{},[60,291,292],{},[63,293,294],{},"price.gold",[60,296,297],{},"100",[42,299,300,305],{},[60,301,302],{},[63,303,304],{},"price.gems",[60,306,307],{},"5",[15,309,310],{},"Items can have prices in multiple currencies. The buyer must have all required currencies.",[28,312],{},[31,314,316],{"id":315},"modifying-trade-prices","Modifying Trade Prices",[15,318,319,320,323],{},"The ",[63,321,322],{},"trade_init"," event fires for each item when a trade opens. Use it to set custom buy\u002Fsell prices.",[36,325,326,336],{},[39,327,328],{},[42,329,330,333],{},[45,331,332],{},"Property",[45,334,335],{},"What it controls",[55,337,338,348,358],{},[42,339,340,345],{},[60,341,342],{},[63,343,344],{},"item.price",[60,346,347],{},"Base price (from template, read-only)",[42,349,350,355],{},[60,351,352],{},[63,353,354],{},"item.tradePrice.player",[60,356,357],{},"What trader pays when buying from player",[42,359,360,365],{},[60,361,362],{},[63,363,364],{},"item.tradePrice.trader",[60,366,367],{},"What trader charges when selling to player",[15,369,370],{},[19,371,372],{},"Example - Merchants buy at 50% price:",[374,375,380],"pre",{"className":376,"code":377,"language":378,"meta":379,"style":379},"language-javascript shiki shiki-themes github-light github-dark","game.on(\"trade_init\", (traderInventory, item) => {\n  for (const currency in item.price) {\n    item.tradePrice.player[currency] = Math.round(item.price[currency] * 0.5);\n  }\n});\n","javascript","",[63,381,382,425,447,474,480],{"__ignoreMap":379},[383,384,387,391,395,398,402,405,409,412,415,418,422],"span",{"class":385,"line":386},"line",1,[383,388,390],{"class":389},"sVt8B","game.",[383,392,394],{"class":393},"sScJk","on",[383,396,397],{"class":389},"(",[383,399,401],{"class":400},"sZZnC","\"trade_init\"",[383,403,404],{"class":389},", (",[383,406,408],{"class":407},"s4XuR","traderInventory",[383,410,411],{"class":389},", ",[383,413,414],{"class":407},"item",[383,416,417],{"class":389},") ",[383,419,421],{"class":420},"szBVR","=>",[383,423,424],{"class":389}," {\n",[383,426,428,431,434,437,441,444],{"class":385,"line":427},2,[383,429,430],{"class":420},"  for",[383,432,433],{"class":389}," (",[383,435,436],{"class":420},"const",[383,438,440],{"class":439},"sj4cs"," currency",[383,442,443],{"class":420}," in",[383,445,446],{"class":389}," item.price) {\n",[383,448,450,453,456,459,462,465,468,471],{"class":385,"line":449},3,[383,451,452],{"class":389},"    item.tradePrice.player[currency] ",[383,454,455],{"class":420},"=",[383,457,458],{"class":389}," Math.",[383,460,461],{"class":393},"round",[383,463,464],{"class":389},"(item.price[currency] ",[383,466,467],{"class":420},"*",[383,469,470],{"class":439}," 0.5",[383,472,473],{"class":389},");\n",[383,475,477],{"class":385,"line":476},4,[383,478,479],{"class":389},"  }\n",[383,481,483],{"class":385,"line":482},5,[383,484,485],{"class":389},"});\n",[15,487,488],{},[19,489,490],{},"Example - Mage trader only accepts arcane items:",[374,492,494],{"className":376,"code":493,"language":378,"meta":379,"style":379},"game.on(\"trade_init\", (traderInventory, item) => {\n  if (traderInventory.tags.includes(\"mage_trader\") && !item.tags.includes(\"arcane\")) {\n    item.tradePrice.player = {};  \u002F\u002F Can't sell to this merchant\n    item.tradePrice.trader = {};  \u002F\u002F Can't buy from this merchant\n  }\n});\n",[63,495,496,520,557,571,583,587],{"__ignoreMap":379},[383,497,498,500,502,504,506,508,510,512,514,516,518],{"class":385,"line":386},[383,499,390],{"class":389},[383,501,394],{"class":393},[383,503,397],{"class":389},[383,505,401],{"class":400},[383,507,404],{"class":389},[383,509,408],{"class":407},[383,511,411],{"class":389},[383,513,414],{"class":407},[383,515,417],{"class":389},[383,517,421],{"class":420},[383,519,424],{"class":389},[383,521,522,525,528,531,533,536,538,541,544,547,549,551,554],{"class":385,"line":427},[383,523,524],{"class":420},"  if",[383,526,527],{"class":389}," (traderInventory.tags.",[383,529,530],{"class":393},"includes",[383,532,397],{"class":389},[383,534,535],{"class":400},"\"mage_trader\"",[383,537,417],{"class":389},[383,539,540],{"class":420},"&&",[383,542,543],{"class":420}," !",[383,545,546],{"class":389},"item.tags.",[383,548,530],{"class":393},[383,550,397],{"class":389},[383,552,553],{"class":400},"\"arcane\"",[383,555,556],{"class":389},")) {\n",[383,558,559,562,564,567],{"class":385,"line":449},[383,560,561],{"class":389},"    item.tradePrice.player ",[383,563,455],{"class":420},[383,565,566],{"class":389}," {};  ",[383,568,570],{"class":569},"sJ8bj","\u002F\u002F Can't sell to this merchant\n",[383,572,573,576,578,580],{"class":385,"line":476},[383,574,575],{"class":389},"    item.tradePrice.trader ",[383,577,455],{"class":420},[383,579,566],{"class":389},[383,581,582],{"class":569},"\u002F\u002F Can't buy from this merchant\n",[383,584,585],{"class":385,"line":482},[383,586,479],{"class":389},[383,588,590],{"class":385,"line":589},6,[383,591,485],{"class":389},[28,593],{},[31,595,597],{"id":596},"currency-methods","Currency Methods",[36,599,600,609],{},[39,601,602],{},[42,603,604,607],{},[45,605,606],{},"Method",[45,608,50],{},[55,610,611,621,634,647],{},[42,612,613,618],{},[60,614,615],{},[63,616,617],{},"inventory.getCurrencyAmount(currencyId)",[60,619,620],{},"Get total of a specific currency",[42,622,623,628],{},[60,624,625],{},[63,626,627],{},"inventory.getCurrencies()",[60,629,630,631],{},"Get all currencies as ",[63,632,633],{},"{ id: amount }",[42,635,636,641],{},[60,637,638],{},[63,639,640],{},"inventory.canAffordPrice(price)",[60,642,643,644],{},"Check if can afford ",[63,645,646],{},"{ gold: 100, gems: 5 }",[42,648,649,654],{},[60,650,651],{},[63,652,653],{},"inventory.deductCurrency(price)",[60,655,656],{},"Pay with currency (returns false if can't afford)",[15,658,659,662],{},[19,660,661],{},"Currency only counts unequipped items"," - equipped currency items are not spendable.",[28,664],{},[31,666,668],{"id":667},"exchange-events","Exchange Events",[36,670,671,684],{},[39,672,673],{},[42,674,675,678,681],{},[45,676,677],{},"Event",[45,679,680],{},"When it fires",[45,682,683],{},"Parameters",[55,685,686,701,715,730],{},[42,687,688,693,696],{},[60,689,690],{},[63,691,692],{},"inventory_open",[60,694,695],{},"Exchange UI opens",[60,697,698],{},[63,699,700],{},"(inventory)",[42,702,703,708,711],{},[60,704,705],{},[63,706,707],{},"inventory_close",[60,709,710],{},"Exchange UI closes",[60,712,713],{},[63,714,700],{},[42,716,717,722,725],{},[60,718,719],{},[63,720,721],{},"inventory_transfer",[60,723,724],{},"Item moves between inventories",[60,726,727],{},[63,728,729],{},"(source, target, item, quantity, isTrade)",[42,731,732,736,739],{},[60,733,734],{},[63,735,322],{},[60,737,738],{},"Trade opens, for each item",[60,740,741],{},[63,742,743],{},"(traderInventory, item)",[15,745,746],{},[19,747,748],{},"Canceling transfers:",[15,750,751,752,755,756,758],{},"Return ",[63,753,754],{},"false"," from ",[63,757,721],{}," to prevent the transfer:",[36,760,761,773],{},[39,762,763],{},[42,764,765,767,770],{},[45,766,677],{},[45,768,769],{},"Check",[45,771,772],{},"Result",[55,774,775],{},[42,776,777,781,786],{},[60,778,779],{},[63,780,721],{},[60,782,783],{},[63,784,785],{},"item.traits.is_quest_item",[60,787,788],{},"return false (can't sell quest items)",[28,790],{},[31,792,794],{"id":793},"trade-flow-example","Trade Flow Example",[36,796,797,808],{},[39,798,799],{},[42,800,801,804,806],{},[45,802,803],{},"Step",[45,805,100],{},[45,807,772],{},[55,809,810,821,832,843],{},[42,811,812,815,818],{},[60,813,814],{},"1",[60,816,817],{},"Player clicks item in merchant inventory",[60,819,820],{},"Check if party can afford price",[42,822,823,826,829],{},[60,824,825],{},"2",[60,827,828],{},"Party can afford",[60,830,831],{},"Deduct currency from party inventory",[42,833,834,837,840],{},[60,835,836],{},"3",[60,838,839],{},"Currency deducted",[60,841,842],{},"Add currency to merchant inventory",[42,844,845,848,851],{},[60,846,847],{},"4",[60,849,850],{},"Currency transferred",[60,852,853],{},"Move item from merchant to party",[15,855,856],{},"If any step fails (can't afford, inventory full), the whole transaction is cancelled.",[28,858],{},[31,860,862],{"id":861},"creating-a-shop","Creating a Shop",[36,864,865,874],{},[39,866,867],{},[42,868,869,871],{},[45,870,803],{},[45,872,873],{},"What to do",[55,875,876,883,890,897,904],{},[42,877,878,880],{},[60,879,814],{},[60,881,882],{},"Create inventory template with shop items",[42,884,885,887],{},[60,886,825],{},[60,888,889],{},"Set prices on each item template",[42,891,892,894],{},[60,893,836],{},[60,895,896],{},"Create a currency item (gold, coins, etc.)",[42,898,899,901],{},[60,900,847],{},[60,902,903],{},"Give player starting currency",[42,905,906,908],{},[60,907,307],{},[60,909,910,911,914],{},"Use ",[63,912,913],{},"{trade: \"shop_id\"}"," action to open",[28,916],{},[31,918,920],{"id":919},"quick-reference","Quick Reference",[36,922,923,933],{},[39,924,925],{},[42,926,927,930],{},[45,928,929],{},"I want to...",[45,931,932],{},"Do this",[55,934,935,944,954,964,974,985,995],{},[42,936,937,940],{},[60,938,939],{},"Open a shop",[60,941,942],{},[63,943,913],{},[42,945,946,949],{},[60,947,948],{},"Open a loot container",[60,950,951],{},[63,952,953],{},"{loot: \"chest_id\"}",[42,955,956,959],{},[60,957,958],{},"Check player gold",[60,960,961],{},[63,962,963],{},"inventory.getCurrencyAmount('gold')",[42,965,966,969],{},[60,967,968],{},"Check if can buy",[60,970,971],{},[63,972,973],{},"inventory.canAffordPrice({ gold: 100 })",[42,975,976,979],{},[60,977,978],{},"Modify trade prices",[60,980,981,982,984],{},"Listen to ",[63,983,322],{}," event",[42,986,987,990],{},[60,988,989],{},"Prevent item transfer",[60,991,992,993],{},"Return false from ",[63,994,721],{},[42,996,997,1000],{},[60,998,999],{},"Create a currency",[60,1001,1002,1003,1005],{},"Set ",[63,1004,169],{}," on item template",[28,1007],{},[31,1009,1011],{"id":1010},"next-steps","Next Steps",[136,1013,1014,1022],{},[139,1015,1016,1021],{},[1017,1018,1020],"a",{"href":1019},"\u002Fdocs\u002Fglobal_essentials\u002Fitems\u002Fitems_overview","Overview"," - Item basics and templates",[139,1023,1024,1028],{},[1017,1025,1027],{"href":1026},"\u002Fdocs\u002Fglobal_essentials\u002Fitems\u002Fapply","Apply"," - Crafting and custom apply logic",[1030,1031,1032],"style",{},"html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":379,"searchDepth":427,"depth":427,"links":1034},[1035,1036,1037,1038,1039,1040,1041,1042,1043,1044],{"id":33,"depth":427,"text":34},{"id":87,"depth":427,"text":88},{"id":162,"depth":427,"text":163},{"id":315,"depth":427,"text":316},{"id":596,"depth":427,"text":597},{"id":667,"depth":427,"text":668},{"id":793,"depth":427,"text":794},{"id":861,"depth":427,"text":862},{"id":919,"depth":427,"text":920},{"id":1010,"depth":427,"text":1011},"The exchange system handles two modes of item transfer between inventories: loot (free taking) and trade (buying\u002Fselling with currency).","md",{"plugin":1048,"category":1049,"page":1050},"global_essentials","items","exchange",true,"\u002Fdocs\u002Fglobal_essentials\u002Fitems\u002Fexchange",{"title":5,"description":1045},"docs\u002Fglobal_essentials\u002Fitems\u002Fexchange","XN97Zad2gNGqjV_XtZ6oaUewWZ7EfZfPYtibi-TEp-U",1779582261310]