Arbitarily long return reruns random() upon returning

If you are requesting technical assistance with Twine, please specify:
Twine Version: 2.34.1
Story Format: SugarCube

Hi all! Very similar to the javascript return issue mentioned here, I have storymenu buttons that the player can access at any time (i.e. ‘Appearance’). However, I noticed that return, back, and arbitarily long return do not stop the original cell from rerunning random().

Example origin cell:

<<set $RNG to random(1,100)>>
$RNG

I have tested this with everything I could find of return, back, and the following javascript coutesy of HiEv:

/* Trigger the following code at the start of navigation to a new passage. */
$(document).on(":passagestart", function (event) {
	/* Make sure the current passage doesn't have a "noreturn" tag. */
	if (!tags().includes("noreturn")) {
		/* If it doesn't, then set $return to the current passage name. */
		State.variables.return = passage();
	}
});

$RNG is always, always a different number each time and it’s causing complications :frowning:

Also, I’m unable to use HiEv’s SlideWin for now due to some storymenu buttons themselves containing links.

If you want the passage to act consistently, then you’ll need to set the values of things, like $RNG before entering the passage. Something like this in “Passage A”:

Whatever.

[[Passage B][$RNG = random(1,100)]

and then in “Passage B” you could do this:

$RNG

Now, if the player is in the “Passage B” passage, and they then go to a “noreturn” tagged passage, when they come back to “Passage B” the value of $RNG will remain the same.

Basically, if you’re using the “Arbitrarily long return” code, then, instead of setting values like $RNG in the current passage, you need to make sure you structure all of your code so that any values like that are set by the previous passage.

That’s a big part of why I created the “SlideWin Overlay”, since it helps avoid the problem of having to restructure your code around working with the “Arbitrarily long return” code.

Enjoy! :slight_smile:

Would this still be the recommended course of action if I have different values of random() to run, some of which to be run only if a previous random() is of a certain value? It’s beginning to look like I will have to rewrite many passages and I’d like to avoid that if I can since my inventory page isn’t compatible with SlideWin right now :frowning:

Example:

Do you want to explore this zone?

[[Explore][$RNG = random (1,5)]

Explore:

<<if $RNG is 5>>\
Oh no! You encounter a trap!

<<set $RNG to random (1,3)>>\
<<if $RNG is 3>>\
But you easily skip over it!

<<else>>\
...and it grabs you!

<</if>>

<<else>>\
You meet a strange merchant.

[[Talk]]
[[Leave]]

Yes. You basically have three options:

  • revamp all of your code to pre-generate values like that,
  • revamp your inventory page/storymenu buttons to make it compatible with SlideWin and then use that,
  • or just give up and accept this “cheat” system exists.

Personally, I’d just go with revamping the inventory page/StoryMenu buttons so that, instead of going to other passages, they just open other SlideWin overlays.

If you want a StoryMenu button to open a passage as a SlideWin overlay, just put something like this in the StoryMenu special passage:

<a onclick="slideWin('SlideWin Passage')">SlideWin</a>

That would add a “SlideWin” button to the StoryMenu buttons that, when clicked, would open the “SlideWin Passage” passage in a SlideWin overlay. I coded the slideWin() function to be globally available so that it would work in situations like this.

Hope that helps! :slight_smile:

Before I go any further, I just want you to know I absolutely appreciate your advice on all this :smiley:

Following up. Regarding StoryMenu Buttons:

  • Is there any way to disable a button under certain conditions? That way I could prevent people from traveling to a new passage from something containing random()

Regarding SlideWin:

  • Is there a way to enable ‘tooltips’ for items in SlideWin? I currently have 🛈 buttons which display additional information about an item that appears in a little box below the item line. In SlideWin, clicking these buttons do nothing. Hope this is not too clunky:
	<<if $chocmilk.count >= 1>>[[$chocmilk.name|Inventory][$Player.energy += $chocmilk.energy, $chocmilk.count -= 1]] ($chocmilk.count) <span title="Click for more info"><<click "🛈">><<toggleclass "#invchocmilk" "hide">><</click>></span>
	<div id="invchocmilk" class="hide expandInfo"><<print $chocmilk.description>></div><br><<endif>> 
  • Also referencing the code above, players may click on an item name to consume it. However, clicking neither the item name nor the information buttons do anything while in SlideWin. Any advice?

EDIT: I must confess to be terrible at coding, and am not able to comprehend the javascript magic that went into SlideWin

EDIT2: I just successfully revamped all my code to pre-generate values. Hooray! However, I’d love to be able to implement SlideWin for the coolness factor still. :smiley:

Yes, though it takes a bit of setup.

If you have your StoryMenu special passage set up like this:

<a id="slidewinbtn" onclick="!$(this).attr('disabled') ? slideWin('SlideWin Passage') : $.noop()" tabindex="0">SlideWin</a>
[[Test]]

now the new code there makes sure that the onclick event doesn’t trigger the SlideWin overlay if the link/button is disabled. (Technically it’s a link, but it’s displayed to look like a button, so I’m just going to call it a button for the rest of this post.)

Now, to toggle enabling/disabling the button, you could use the jQuery .attr() method like this:

<<run $("#slidewinbtn").attr("disabled", $("#slidewinbtn").attr("disabled") ? null : true)>>

Basically, it does .attr("disabled", true) to disable the SlideWin button, and .attr("disabled", null) to enable the button, through the use of a conditional operator (i.e. condition ? true result : false result).

To toggle the “Test” button it’s a bit trickier to target, but you could target it for toggling like this:

<<run $("#menu-story [data-passage='Test']").attr("disabled", $("#menu-story [data-passage='Test']").attr("disabled") ? null : true)>>

That uses the passage name in the data-passage attribute of the HTML element to find the right button to toggle among the StoryMenu buttons.

In addition to that, you’ll need to add this CSS to your Stylesheet so that the disabled buttons will always appear like they’re disabled when they’re disabled:

#menu li a:disabled, #menu li a[disabled], #menu li a:disabled:hover, #menu li a[disabled]:hover {
	color: grey;
	background-color: transparent;
	border-color: transparent;
}

Hopefully that’s clear enough, but feel free to ask if you have any questions on any of that.
 

I tested my <<hovertip>> Macro in a SlideWin overlay and it worked fine for me. You might want to try using that.
 

I can’t really help without knowing more clearly what you’re attempting to do. Some example code would be helpful.

Anyways, hope that helps! :slight_smile: