0 votes
by (600 points)

I'm using R Chapel's excellent simple inventory and transfering items between inventories with instant updates being shown. (See https://twinery.org/questions/54404/moving-items-between-inventories-with-instant-updates?show=54404#q54404)
I'd now like to set a limit on the maximum number of items which can be added to an inventory - say 10. This would represent the maximum number of items which could be picked up and carried. Exceed that limit and something must be dropped first.

Ideally there would be two methods of doing this:

1. The user can't 'pick up' an object until he has dropped something first.

2 The user can pick up an additional object but a random item is dropped when they do so.

Any ideas?

1 Answer

0 votes
by (63.1k points)

You can use the inventory#count() method to determine how many items are currently in the inventory:

/* assuming the inventory is stored in the variable `$inv` */

<<if $inv.count() < 10>>
    <<pickup '$inv' 'something'>>
    ''You cannot carry any more items.''

/* To drop a random item. */

<<if $inv.count() >= 10>>
    <<drop '$inv' `$inv.toArray().random()`>>
<<pickup '$inv' 'something'>>