Howdy, Stranger!

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

Evaluating expressions within function arguments

So this is hopefully a really stupid and easy-to-answer question.

I am upgrading a twine game from 1.4 to 2 in Sugarcube.

Previously, I was able to use stuff like:
<<set $foo = 25 + Math.floor( $bar / 2 ) >>

But now I cannot put an expression like "$bar / 2" into a function call, apparently?

Can I surround the expression with some character or something that will allow it to be evaluated and then passed to the function?

Thanks much!

Comments

  • You don't state which version of SugarCube you are using, so I am going to assume it is either SugarCube 1.0.34 which comes with the on-hosted release of Twine 2 or SugarCube 1.0.32 which comes with the installable release.

    Are you sure it is not working because the following test worked for me in the 1.0.32, 1.0.34 and 2.3.1 versions of SugarCube
    <<set $bar to 11>>
    <<set $foo = 25 + Math.floor($bar / 2) >>
    
    foo should be 30: $foo
    (11/2 is 5.5, when floored is 5, 25 + 5 is 30)
    

    note: you can use parenthesis to control the order an expression gets evaluated in, the following forces the $bar / 2 to be evaluated before the 25 + Math.floor
    <<set $foo = (25 + Math.floor(($bar / 2))) >>
    
  • I'm using 1.0.32.
  • edited February 2016
    Okay, it appears it's a completely different issue — apparently in Twine 1 an undefined variable would evaluate as 0 (<<if $foo is 0>>), but now in Twine 2 it does not. Is that accurate?
  • The default value of an undefined variable has nothing to do with the version of Twine, it is the story format the determines how that works. If fact how anything/everything works is defined by the story format.

    I can't remember when/if SugarCube 1 changed so that an undefined variable is not equal to zero, but it has always been a good idea to assign a default value to your variables before the story starts.

    You can do this in SugarCube by creating a special passage named StoryInit like so:
    <<set $bar to 0>>
    <<set $playerKissedNemesis to false>>
    ... etc ...
    
  • Cool, thanks.
  • greyelf wrote: »
    I can't remember when/if SugarCube 1 changed so that an undefined variable is not equal to zero, but it has always been a good idea to assign a default value to your variables before the story starts.
    SugarCube has never had that particular "feature", as I think it's a bad idea all around. Twine developers are far, far better served by getting into the habit of initializing their variables, IMO. Better a little friction up front, than subtle bugs later on.
Sign In or Register to comment.