0

Edit:

Solved it after some more trial and error. I needed to "capture" the variables first:

...
<<capture _itemkey, _itemkeys, _itemkeysattributes>>
<tr>
<td><<print [_itemkeys[_j]] >> <<print [_itemkeysattributes[_k]]>></td>
<td><<print $items[_itemkey[_i]][_itemkeys[_j]][[_itemkeysattributes[_k]]].cost>></td>
<td><<print $items[_itemkey[_i]][_itemkeys[_j]][[_itemkeysattributes[_k]]].owned>></td>
<<print ' @@.UIButton;<<button "Use a potion">>
<<run $items[_itemkey[(' + _i + ')]][_itemkeys[(' + _j + ')]][_itemkeysattributes[(' + _k + ')]].owned -= 1>>
<<goto "' + passage() + '">><</button>>@@'>>
</tr>
<</capture>>
...

I implemented a table with for loops. While the table generates fine, I can't get a button that adjusts an array value to work, it just gives the following error: "Error: <>: bad evaluation: Cannot read properties of undefined (reading 'undefined')."

printing $items[_itemkey[_i]][_itemkeys[_j]][[_itemkeysattributes[_k]]].owned works, just not adjusting it via the button.

Item array looks like this:

<<set $items = {
   "Potions":{
      "IQ":{
         "+":{
            purchased: false,
            class: "item",
            cost: 20,
            owned: 0,
         },
        "+ +":{
            purchased: false,
            class: "item",
            cost: 50,
            owned: 0,
         },
         "-":{
            purchased: false,
            class: "item",
            cost: 20,
            owned: 0,
         },
        "- -":{
            purchased: false,
            class: "item",
            cost: 50,
            owned: 0,
         },
      },
      "Luck":{
         "+":{
            purchased: false,
            class: "item",
            cost: 20,
            owned: 0,
         },
        "+ +":{
            purchased: false,
            class: "item",
            cost: 50,
            owned: 0,
         },
         "-":{
            purchased: false,
            class: "item",
            cost: 20,
            owned: 0,
         },
        "- -":{
            purchased: false,
            class: "item",
            cost: 50,
            owned: 0,
         },
      },
   },
}>>

Table is then generated like this:

<table style="width:100%">
<tr>
<th style="text-align:center;">Item</th>
<th style="text-align:center;">Current cost</th>
<th style="text-align:center;">Owned Quantity</th>
<th style="text-align:center;">Action</th>
</tr>
<<set _itemkey = Object.keys($items)>>
<<for _i = 0; _i < _itemkey.length; _i++>>
<<set _itemkeys = Object.keys($items[_itemkey[_i]])>>
<<for _j = 0; _j < _itemkeys.length; _j++>>
<<set _itemkeysattributes = Object.keys($items[_itemkey[_i]][_itemkeys[_j]])>>
<<for _k = 0; _k < _itemkeysattributes.length; _k++>>
<<if $items[_itemkey[_i]][_itemkeys[_j]][[_itemkeysattributes[_k]]].class == "item">>
<tr>
<td><<print [_itemkeys[_j]] >> <<print [_itemkeysattributes[_k]]>></td>
<td><<print $items[_itemkey[_i]][_itemkeys[_j]][[_itemkeysattributes[_k]]].cost>></td>
<td><<print $items[_itemkey[_i]][_itemkeys[_j]][[_itemkeysattributes[_k]]].owned>></td>
@@#button;<<button "Use on self">>
**<<run $items[_itemkey[_i]][_itemkeys[_j]][[_itemkeysattributes[_k]]].owned -= 1>> **
<</button>>@@ 
</tr>
<<endif>>
<</for>>
<</for>>
<</for>>
</table>

Table with buttons

Anyone know how I can get the button to reduce the owned quantity for each item used?

  • If you can, use a debugger to step through your code. If can't, this is a good reason not to use this kind of view processing. Otherwise, you can use alerts or logs to write out the value to some kind of display. Build your solution up from simple to more complicated expressions until you have your solution. – horace Dec 18 '22 at 05:29

0 Answers0