Howdy, Stranger!

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

More efficient version of "$var = $var + 1" ?

I mainly use Sugarcube. What happens is I often write <<set $var = $var + 1>>, but the $var changes very often. Rather than having to re-name both variables, is there a function like <<set $var += 1>> or something similar, where I only have to name it (thus re-name it for different instances) once, and still have the effect be the same?

Any help is appreciated.

Comments

  • edited August 2015
    If I understand correctly, you want a macro who increment the value of a specific variable?

    This on Javascript sheet
    macros.add("Variable",{
    	version: {major:1, minor:0, patch:0},
    	skipArgs : true,
    	handler : function() {
    		state.active.variables["VariableName"] +=1;
    	}
    });
    

    And this on passage
    <<set $VariableName to 0>>
    <<VariableName>>
    

    And VariableName grows +1

    I hope that helps ;)
  • Klain wrote: »
    If I understand correctly, you want a macro who increment the value of a specific variable?

    This on Javascript sheet
    macros.add("Variable",{
    	version: {major:1, minor:0, patch:0},
    	skipArgs : true,
    	handler : function() {
    		state.active.variables["VariableName"] +=1;
    	}
    });
    

    And this on passage
    <<set $VariableName to 0>>
    <<VariableName>>
    

    And VariableName grows +1

    I hope that helps ;)

    I would like something that increases a given variable without having to write it out twice. in <<set $var = $var + 1>>, you have to write $var twice.

    I have a lot of variables with different names. So if I wanted to copy and paste the long string of code I had, I'd have to replace $var with $var2 in two positions, because I've written $var twice.

    If I had something like <<set $var +=1>> then it would increase the variable and I would only have to re-write $var once, because it's only been written once.

    I'm going to change the name of $var often, but I would rather not have to correct it twice for every time.

    I'm not sure if this is possible with Twine's limitations but it's a java function, which Twine is based on, so I'm holding out hope.
  • Is something like <<set $var = self + 1>> possible?

    <<set $var1 = self + 1>>
    <<set $var2 = self + 1>>
    <<set $var3 = self + 1>>
    <<set $var4 = self + 1>>

    Rather than having to write out each variable twice
  • Try these examples:
    /* Add 1 to $variable. */
    <<set $variable += 1>>
    
    /* Add 5 to $variable. */
    <<set $variable += 5>>
    
    /* Subtract 1 from $variable. */
    <<set $variable -= 1>>
    
    /* Subtract 5 from $variable. */
    <<set $variable -= 5>>
    

    Alternatively, if you simply need to increment or decrement the $variable by 1, then you could also do this:
    /* Increment $variable by 1. */
    <<set $variable++>>
    
    /* Decrement $variable by 1. */
    <<set $variable-->>
    
  • edited August 2015
    You might say every time the player fires a shot, a bullet is used. You could do that like this:
    <<set $gun.ammo = $gun.ammo - 1>>
    or
    <<set $gun.ammo -= 1>>

    via http://twinery.org/forum/discussion/1516/objects-are-your-friends-how-why-to-use-javascript-objects-for-total-newbies
  • Try these examples:
    /* Add 1 to $variable. */
    <<set $variable += 1>>
    
    /* Add 5 to $variable. */
    <<set $variable += 5>>
    
    /* Subtract 1 from $variable. */
    <<set $variable -= 1>>
    
    /* Subtract 5 from $variable. */
    <<set $variable -= 5>>
    

    Alternatively, if you simply need to increment or decrement the $variable by 1, then you could also do this:
    /* Increment $variable by 1. */
    <<set $variable++>>
    
    /* Decrement $variable by 1. */
    <<set $variable-->>
    

    I swear on my life I tried that in Twine 2.0.8 before asking the question and it didn't work, but copying and pasting your example does. I'm a little irked the answer was this "right in front of me" the whole time, but that doesn't matter because it works now, and my problem's been solved. Thank you.
Sign In or Register to comment.