Howdy, Stranger!

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

Infinite replace link with SugarCube replace macro set

edited June 2015 in Help! with 1.x
Is an infinite <<replacelink>> macro possible using this macro set? As in it forms a loop, so that once you get to the third choice, it cycles back to the first choice. A player can then continuously click on the macro to set a state.

I can do this with a widget stored inside a <<click>> macro, and then put the looping code inside the widget, but the problem that poses is the click does not animate, so there's no visual feedback for the player. What I really want is the behaviour of a permanent <<click>> macro with the animation of a <<replace>> macro.

Comments

  • edited June 2015
    No worries, I figured out how to do this.

    Create two widgets, one is a replacelink macro that leads to a second widget, and the second widget has a replacelink macro that leads back to the first widget.

    They'll keep replacing each other forever.
  • Genius!
  • You really do not want to use nested <<replacelink>> macros. You'll end up with a layer of nested markup for each cycle the player makes.

    Couldn't you do something like the following:
    @\
    <</widget>>
    
    <<widget "settingcycle1">>\
    <<set $state to 1>>\
    <<click "A">><<replace "#link-cycle">><<settingcycle2>><</replace>><</click>>\
    <</widget>>
    
    <<widget "settingcycle2">>\
    <<set $state to 2>>\
    <<click "B">><<replace "#link-cycle">><<settingcycle3>><</replace>><</click>>\
    <</widget>>
    
    <<widget "settingcycle3">>\
    <<set $state to 3>>\
    <<click "C">><<replace "#link-cycle">><<settingcycle1>><</replace>><</click>>\
    <</widget>>
    
    Usage:
    <<settingcycle>>
    

    I realize that's probably not as sexy as your <<replacelink>> version, but your players also won't end up buried in nested markup.
  • edited June 2015
    What do you mean by nested markup?

    It doesn't show on screen, so is it just a backend function that could impact performance?

    This is basically the widget I was using:
    <<widget "infinite1">><<replacelink>><<click "My function">><<dostuffwidget>><</click>><<becomes>><<infinite2>><<endreplacelink>><</widget>>
    
    <<widget "infinite2">><<replacelink>><<click "My function">><<dostuffwidget>><</click>><<becomes>><<infinite1>><<endreplacelink>><</widget>>
    
  • Claretta wrote: »
    What do you mean by nested markup?
    That macro set (and probably the others like it by Leon; e.g. <<cyclinglink>>), all inject a wrapper element and various sub elements into the page. By calling a new instance of the macro from within the original call, essentially calling it recursively, you are nesting the markup generated by the second call within the markup of the first. The third call will be nested within the second, the fourth within the third, etc, etc, ad infinitum.

    Run through several cycles and check the generated markup in your browser's inspector. As an example, it will look something like the following:

    During the first cycle:
    <wrapper>
    	<initial>…</initial>
    	<first>…</first>
    	<second>…</second>
    </wrapper>
    
    During the second cycle:
    <wrapper>
    	<initial>…</initial>
    	<first>…</first>
    	<second>
    		<wrapper>
    			<initial>…</initial>
    			<first>…</first>
    			<second>…</second>
    		</wrapper>
    	</second>
    </wrapper>
    
    During the third cycle:
    <wrapper>
    	<initial>…</initial>
    	<first>…</first>
    	<second>
    		<wrapper>
    			<initial>…</initial>
    			<first>…</first>
    			<second>
    				<wrapper>
    					<initial>…</initial>
    					<first>…</first>
    					<second>…</second>
    				</wrapper>
    			</second>
    		</wrapper>
    	</second>
    </wrapper>
    

    If a player cycles that thing a lot, then yes you might see some issues (though it would likely need to be a very large number; then again, you can't predict what a player will do). I'm unsure if that will get reset occasionally, but if it does not, then the player wouldn't even need to cycle it all at once, since it would continue to build over time.
  • Mmm, fair enough. :) I'll try your suggestion.
  • edited June 2015
    I don't quite understand your example widget, and it gives me the javascript error of "no elements match the selector #link-cycle" when I try to select it. #link-cycle also appears in bright yellow text beside the link on the page.

    I did some tests using the developer tools. I can see what you mean about nested elements, though it also seems to reset and clear them if I leave the passage. I.e. nest a few, then leave passage, then return, and the stack is reset.

    I'll still use your widget if I can get it to work, though.
  • I believe that is a feature of the new version of SugarCube 2, but I could be wrong.
  • Hmm. Yes, I believe you're correct, greyelf.

    Okay then, simply changing from the custom style markup () to a <span> should make it SugarCube 1.x compatible. For example (you only need to change the starter widget):
    <<widget "settingcycle">>\
    <span id="link-cycle"><<settingcycle1>></span>\
    <</widget>>
    

    Or, as another example, something closer to your example:
    <<widget "startcycle">>\
    <span id="link-cycle"><<cycle1>></span>\
    <</widget>>
    
    <<widget "cycle1">>\
    <<dostuffwidget>>\
    <<click "My function (1)">><<replace "#link-cycle">><<cycle2>><</replace>><</click>>\
    <</widget>>
    
    <<widget "cycle2">>\
    <<dostuffwidget>>\
    <<click "My function (2)">><<replace "#link-cycle">><<cycle3>><</replace>><</click>>\
    <</widget>>
    
    <<widget "cycle3">>\
    <<dostuffwidget>>\
    <<click "My function (3)">><<replace "#link-cycle">><<cycle1>><</replace>><</click>>\
    <</widget>>
    
  • Thanks, works great. But I had to put my <<dostuffwidget>> inside the <<click>> macro, because it was executing my <<dostuffwidget>> on passage load as written.

    But with that small alteration it works.
  • edited June 2015
    I lied, turns out I can't use the in-built replace version after all because it has no animation. The animation of the link fading after each click is very important.

    Any way to add this in to make it have animation like the <<replacelink>> macro?

    I believe this is the css that does that macro's animation:
    .revision-span-in { opacity: 0; }
    .revision-span:not(.revision-span-out) { transition: 500ms; -webkit-transition: 500ms; -moz-transition: 500ms;}
    .revision-span-out { position: absolute; opacity: 0; }
    
  • You should be able to wrap each cycle within a <span> which bears the appropriate classes and, post-replace, remove the trigger class to start the animation.

    For example (reuses the .revision classes):
    <<widget "animatecycle">>\
    <<script>>setTimeout(function () { $("#link-cycle>span").removeClass("revision-span-in"); }, 20);<</script>>\
    <</widget>>
    
    <<widget "startcycle">>\
    <span id="link-cycle"><<cycle1>></span>\
    <<animatecycle>>\
    <</widget>>
    
    <<widget "cycle1">>\
    <span class="revision-span revision-span-in">\
    <<click "My function (1)">>
    	<<replace "#link-cycle">><<cycle2>><</replace>>
    	<<animatecycle>>
    <</click>>\
    </span>\
    <</widget>>
    
    <<widget "cycle2">>\
    <span class="revision-span revision-span-in">\
    <<click "My function (2)">>
    	<<replace "#link-cycle">><<cycle3>><</replace>>
    	<<animatecycle>>
    <</click>>\
    </span>\
    <</widget>>
    
    <<widget "cycle3">>\
    <span class="revision-span revision-span-in">\
    <<click "My function (3)">>
    	<<replace "#link-cycle">><<cycle1>><</replace>>
    	<<animatecycle>>
    <</click>>\
    </span>\
    <</widget>>
    
  • Seems to work, thanks!
Sign In or Register to comment.