Howdy, Stranger!

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

If statement, comparing previous() to couple passage titles utterly fails.

edited May 2016 in Help! with 2.0
Greetings
This is using Twine 2.0 with Sugarcube story format.
My game has buttons for entering inventoryScreen, charScreen, travelScreen, from any passage where it's allowable. After visiting the passage and reading the information presented there, at the end there is <<return>> button to return.
I've been trying to get around the loop that is created when, say, from inventoryScreen we would have clicked charScreen.

I'd done so already with simply disabling StoryMenu (holding the buttons) when on inventoryScreen passage and the others. But I require additional functionality on these passages now, and <<return>> and the loop 'bug' that comes with it is making it impossible for me.

So, I came up with this (for debugging whether it even works):
<<set $lastVisitedPassage = previous()>>
  <<if $lastVisitedPassage == "ScreenMenu" || "travelScreen" || "inventoryScreen" || "charScreen">>
  <<print "result 1">>
  <<print previous()>>
  <<else>>
  <<print "result 2">>
  <<print previous()>>
  <</if>>

and with this
<<set $lastVisitedPassage = previous()>>
  <<if $lastVisitedPassage == ("ScreenMenu" || "travelScreen" || "inventoryScreen" || "charScreen")>>
  <<print "result 1">>
  <<print previous()>>
  <<else>>
  <<print "result 2">>
  <<print previous()>>
  <</if>>

The first ALWAYS renders the result 1, the second ALWAYS the result 2. If I change == into != the situation get's reversed, but still always.
By always I mean no matter if print previous() shows any of the passageTitles I want to exclude or a random like desertVillage that I made.

How is that even possible? Of that I am very curious, if not frustrated with.

Most importantly, how to make it work? Or how to make another way of circumventing the <<return>> loop bug without disabling the possibility for the player to enter the inventoryScreen and other passages from any point in the game world?

Also, I'd tried changing the StoryMenu special passage to turn the buttons into setter links, like so:
[[Inventory->inventoryScreen][set $lastVisitedPassage = previous()]]
but an technical error pops up.

Best Regards!

Comments

  • edited May 2016
    With trial and error and checking the reference I have determined it's the || (or) that was messing it up. Problem solved, but I can't delete the thread now.
    Also I found a way to refresh without linking, through
    <<click "refresh">>
      <<script>>state.display(state.active.title, null, "back")<</script>>
    <</click>>
    

    Thanks to TheMadExile for that.
  • edited May 2016
    I'm unsure how you resolved the issue, however, the logic you were looking for was something like the following:
    /* Using JavaScript operators */
    <<if $lastVisitedPassage === "ScreenMenu" || $lastVisitedPassage === "travelScreen" || $lastVisitedPassage === "inventoryScreen" || $lastVisitedPassage === "charScreen">>
    
    /* Using TwineScript operators */
    <<if $lastVisitedPassage is "ScreenMenu" or $lastVisitedPassage is "travelScreen" or $lastVisitedPassage is "inventoryScreen" or $lastVisitedPassage is "charScreen">>
    
    Explanation: The logical-AND/-OR operators conjoin sub-expressions into one aggregate conditional expression. Each sub-expression must itself be a valid conditional expression.

    Rafael256 wrote: »
    Also, I'd tried changing the StoryMenu special passage to turn the buttons into setter links, like so:
    [[Inventory->inventoryScreen][set $lastVisitedPassage = previous()]]
    
    but an technical error pops up.
    You have a spurious set word in your setter. It should look something like the following:
    [[Inventory->inventoryScreen][$lastVisitedPassage = previous()]]
    
  • edited May 2016
    Just before I checked the thread with your answer replied I realized the very bug with the or issue, that is to do previous() == X or previous() == Y, not the easy shortcut that I did. The way I solved it before realizing was to separate if statements into separate checks, without using or at all.

    ...a spurious set word. I literally KNOW I am not supposed to put it there, yet I completely forgot about it a couple of hours ago and now again. Good job brain.

    So, in both cases my stupidity was the problem. Your answers are 100% correct and problem solving. I thank you for your time and help.
  • While I'm here.

    The usual way to resolve this particular issue in SugarCube v1 is to use a task and passage tags. For example, you'd put something like the following in your Story JavaScript to setup the task which records the last non-inventory/menu/etc passage:
    predisplay['record-return-passage'] = function () {
    	if (!tags().contains('noreturn')) {
    		state.active.variables.return = passage();
    	}
    };
    
    Then you'd tag all of the inventory/menu passages with the specified tag (noreturn in the above example).

    To return to the recorded passage, from however deep within the invetory/menus, you'd simply do something like the following:
    [[Return|$return]]
    
  • I thank you again for this. Previously, I used the tags with CSS to make the inventory/character/travel not accessible when inside one menu already, but this is way better.

    I shall think on it and try tomorrow, my head hurts from working with twine since morning.
Sign In or Register to comment.