0 votes
by (600 points)

I'm using RChapel's excellent 'Simple Inventory' (https://github.com/ChapelR/custom-macros-for-sugarcube-2/blob/master/docs/simple-inventory.md#the-simple-inventory-system-v2x) and want to drop items from one inventory, automatically adding them to a second inventory. What I'd like to happen is to see something like this with items followed by a Drop/Get link:
You have:
a hammer [Drop]
a saw [Drop]
a clamp [Drop]

On the floor is:
a box of screws [Get] 
a screwdriver [Get]

Using R Chapel's simple inventory I can use this to achieve it:
<<newinventory "$inventory" "a hammer" "a saw" "a clamp">>
<<newinventory "$floor" "a box of screws" "a screwdriver">>

You have:
<<linkedinventory "Drop" "$inventory" "$floor">>

On the floor is:
<<linkedinventory "Get" "$floor" "$inventory">>

This produces the list of items in each inventory with the appropriate Drop or Get link next to each BUT
it doesn't update on the page after clicking each button to make the transfer. Items disappear from each list but don't appear in the other list. Currently I have a 
<<link "Update" `passage()`>><</link>> to refresh the page and display the correct items in each inventory.

Anyone have any ideas how I can fix this without needing that 'Update' link?

1 Answer

+1 vote
by (63.1k points)
selected by
 
Best answer

I should probably make this easier to do. I typically use dialogs to achieve this effect, which you can see in the demo at the link, along with the relevant source code. If you don't want to do that, here's a messier way:

You have:
<span class='auto-update' data-macro='<<linkedinventory "Drop" "$inventory" "$floor">>'>\
    <<linkedinventory "Drop" "$inventory" "$floor">>\
</span>

On the floor is:
<span class='auto-update' data-macro='<<linkedinventory "Get" "$floor" "$inventory">>'>\
    <<linkedinventory "Get" "$floor" "$inventory">>\
</span>

Then in JavaScript:

$(document).on(':inventory-update', function (ev) {
    $('.auto-update').each( function (i, el) {
        var $el = $(el);
        var content = $el.attr('data-macro');
        $el.empty().wiki(content);
    });
});

I'll work on getting enough data into the macro's wrapper so that users will be able to easily recreate the call via an event without essentially needing to write the call twice.

by (600 points)
Thanks - Your 'messier way' works brilliantly and does exactly what I need.
...