Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Replace problem (sugracube 2.11.0)

edited November 2016 in Help! with 2.0
I'm trying to construct a passage where the player can 'take' a couple of items. (I don't use a physical inventory system as such - I just check for variables instead).

For this I'm trying to use the replace macro so that the sentence:
A filthy narrow bed lies unmade against one wall. On the other a small table on which there's half a loaf of stale bread and a knife.
Becomes:
A filthy narrow bed lies unmade against one wall. On the other a small table on which there's a knife.
When the 'bread' link is clicked.
And:
A filthy narrow bed lies unmade against one wall. On the other a small table lies empty.
When the knife is taken.

I could do this by giving each its own passage, but I'd like to do it with replace if possible, but I can't get my head around the logic of it all.

Comments

  • In general, you have two built-in options for something like this: a target + <<link>> + <<replace>> or <<linkreplace>>. The former is more flexible, the latter simpler.

    In this specific case, however, things are a bit more complicated since you want to remove each bit of item text, conjunctions, and replace the entire set with "lies empty" after all items have been removed. As a tradeoff, you could simplify things considerably by making the list of items less integrated with the flow of your descriptive text.

    Anyway, for now, something like the following should do what you want:
    A filthy narrow bed lies unmade against one wall. On the other a small table
    \ <span id="items">on which there's
    \<span id="items-bread"> half a loaf of stale <<link "bread">>
    	<<set $hasBread to true>>
    	<<if $hasBread and $hasKnife>>
    		<<replace "#items">>lies empty<</replace>>
    	<<else>>
    		<<remove "#items-and">>
    		<<remove "#items-bread">>
    	<</if>>
    <</link>></span>
    \<span id="items-and"> and</span>
    \<span id="items-knife"> a <<link "knife">>
    	<<set $hasKnife to true>>
    	<<if $hasBread and $hasKnife>>
    		<<replace "#items">>lies empty<</replace>>
    	<<else>>
    		<<remove "#items-and">>
    		<<remove "#items-knife">>
    	<</if>>
    <</link>></span>
    \</span>.
    
    As you can see, that's fairly involved and adding additional items would only complicate things. You could solve most of the logic issues that come with adding additional items by using a temporary variable as an item counter, but you'd still have to deal with the grammatical issues.
  • Man I have so much to learn! I'd never have got this in a lifetime.

    I'm going to have to run my head through this before I even begin to understand what's going on, but as always a massive thank you for going to the trouble.
Sign In or Register to comment.