Howdy, Stranger!

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

Update varaible after (display:)- twine 2

Hi all,


I have a passage named 'rolldice' with this code:
(set:$roll to (random: $dice, $dice*$side))
You roll a : $roll

In an another passage 'named 1), i have :
Somethings going on !!

(set: $dice to 2, $side to 6)

[You must roll the dice]<result|(click: ?result)[(display: "rolldice")]

$roll

passage 'rolldice' print $roll correctely.
But $roll in passage'1' is not updated, so i can't a if esle to do something with it.

any idea ?

Comments

  • I don’t have an answer to this, but I can verify the issue… Whenever I need random number generation in a passage, I have to put it there and not use (display:).

    Maybe this is a bug, though — I never had time to investigate or file a report at https://bitbucket.org/klembot/twinejs/issues?status=new&status=open.
  • Interesting . . .

    Can we get an HTML file?

    Here's my little test:
    dice
    
    ::dice
    Roll 1: (display: "roll1")
    
    Roll 2: (display: "roll2")
    
    Roll 3: (display: "roll3")
    
    Rolls 4-6: (display: "dice2")
    
    [[Again->dice]]
    
    ::roll1
    (set: $foo to (random: 1, 1000))$foo
    
    ::roll2
    (set: $bar to (random: 1, 1000))$bar
    
    ::roll3
    (set: $baz to (random: 1, 1000))$baz
    
    ::dice2
    (set: $qux to (random: 1, 1000))$qux
    (set: $quux to (random: 1, 1000))$quux
    (set: $corge to (random: 1, 1000))$corgee
    
  • What you have is a timing issue:

    The content of the (click:) macro's container is not run until after the Reader clicks on the link, so the $roll variable has not been assigned a value (so defaults to zero) by your rolldice passage when the main passage is rendered (displayed to Reader) and the current value of $roll is printed.

    You would see a different behavior if you just displayed rolldice without the (click:)
    Somethings going on !!
    
    (set: $dice to 2, $side to 6)
    
    (display: "rolldice")
    
    $roll
    

    If you want the rolldice passage to update what is displayed on the main passage then you will need to use a hook and a (replace:) macro.
    note: you have name the hook whatever makes more sense to you.
    Somethings going on !!
    
    (set: $dice to 2, $side to 6)
    
    [You must roll the dice]<result|(click: ?result)[(display: "rolldice")(replace: ?output)[$roll]]
    
    |output>[]
    
  • That is/was exactly my problem! Thanks, @GreyElf.

    I hope that's @gruik's problem is too.
  • Thanks to all and sorry for the late response. I figured that's what it was (timing issue).

    I try to adapt a solo rpg adventure (initiation to a tabletop game) in twine 2 and I would like to stay closer from the original game.
    That's why i want to have a subroutine to roll any kind of dice.

    Maybe in javascript. (Harlowe or Sugarcube).

    It would be great If someone have an example of custom macros.

    I'm not fluent in english, sorry if i'm not very clear.
  • gruik wrote: »
    It would be great If someone have an example of custom macros.

    SugarCube's Macro API documentation explains how to create custom macros using Javascript.
    You can also use SugarCube's <<widget>> macro to create custom macros.

    Harlowe does not currently allow Author's to create their own custom macros.

    Both story formats allow you to create Javascript functions like the following:
    window.hello = function() {
    	console.log('hello');
    };
    
  • I finally solved my problem. It is a little twisted but it's working.
    I use a flag to know if i use rolldice.
    You can see how it works with html file.
  • gruik wrote: »
    I finally solved my problem. It is a little twisted but it's working.
    I use a flag to know if i use rolldice.
    You can see how it works with html file.

    when I click the html file, I see a whole bunch of code, which I imagine is not what you intended.

  • It's a twine archive. so download and import
  • If anyone is interested, i made a custom sugarcube macro. It was a good exercise for me to understand how to make macro.(hard that was for me)

    to use it, you must have a variable $roll, then call the macro:

    <<rolldice 1 6>> The first number is the number of dice and second the number of sides.

    for example:<<rolldice 3 4>> roll 3D4 and the result is between 3 - 12.

    result is passed to variable $roll.

    macro:
    /* Usage: <<rolldice dice side>> */
    macros.add("rolldice", {
    	version : { major : 1, minor : 0, revision : 0 },
    
    	handler : function () {
    		var mavar;
    		var dice = this.args[0];
    		var side = this.args[1];
    	
    		mavar = random(dice, side * dice);
    		state.active.variables["roll"]=mavar;
    
    	}
    });
    

    passage:
    <<set $roll to null>>
    <<click "Roll dice">><<rolldice 1 6>><<replace "#dice">><<print $roll>><</replace>><</click>>
    
    You roll a: <span id = "dice"><<print $roll>></span>
    
    <<rolldice 3 4>>
    <<print $roll>>
    

    Of course i could've use random macro to do the same thing but i wanted to keep th spirit of th game.


Sign In or Register to comment.