Howdy, Stranger!

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

Problem with integers in an array and print

I'm using Twine 2.0.11 and SugarCube 2.7.2

In the StoryInit passage I have the line
<<set $Strength to [1,0]>>
The first entry in the array is supposed to be the strength score, the second one the current training level for increasing it.

In the character sheet I'm using to keep track of variables while working I'm using
Strength <<print $Strength[0]>>, Training <<print $Strength[1]>>
to display the current values whenever I visit that passage.

Sadly, the second entry doesn't seem to exist. In fact, when I look at it in the debug view, the entire <<print...>> seems to be missing without any error messages.

I've just started to use arrays and widgets instead of ... less elegant solutions, but this is an error I can't seem to figure out.

Comments

  • edited September 2016
    Update:
    Just for the fun of it I just tried adding a third value, so now it's [1,0,0]. The first and third are displayed, for the second the <<print...>> part is still missing.

    I've retyped it, replaced the $Strength[1] with $Name (which does give me the <<print...>> block and displays the name).

    Is this maybe a bug?

    And another update before going to bed:
    I tried making a five-field array [1,2,3,4,5] to see if maybe uneven index numbers were a problem, but 0 and 2-4 work fine, just field 1 is weird. So I'll work around it by avoiding that.

    Of course now there's the problem how to report this. I can't even tell if it's a problem with Twine or SugarCube.
  • edited September 2016
    I doubt it's an issue with Twine 2.

    I also doubt that it's an issue with SugarCube 2.7.2, for two reasons. First, you're talking about very basic code here—meaning, if it were a SugarCube issue, everyone would be complaining. Second, I tried the exact code shown above and I cannot repro your issue—it works exactly as it should for me, using Play or Test mode.

    Is that the actual code from your project or an example? If it is the actual code, was it copy-pasted directly from your project or did you retype it?

    EDIT: Also, what does the following print?
    <<=JSON.stringify($Strength.map(function (v) { return String(v); }))>>
    
  • edited September 2016
    Since I'm still awake anyways...

    Now that I've tried to use the third field, that one is not working and the second one is working fine. =/
    I did retype it, but here's those lines as c&p in their current state.:

    From the StoryInit:
    <<set $Strength = [1,0,0,0]>>
    

    From the character sheet: EDIT: I know I'm not using an index here, I just wanted to see the whole array to maybe get a hint for the error right now.
    Strength <<print $Strength>>, \
    Training <<print $Strength[2]>>
    

    And, just to be complete, since it shouldn't have run before doing any training, the widget I want to use for the levelup:
    <<widget RaiseStat>><<nobr>>
    	<<set $args[0][2] to $args[0][2] + $args[1]>>
    	<<if ($NeedsFood is true and $Nutrition gte 1) or $NeedsFood is false>>
    		<<set $Threshold to 10>>
    		<<if $args[0][0] lte 10>>
    			<<set $Threshold to $args[0][0]*10>>
    		<<elseif $args[0][0] lt 20>>
    			<<set $Threshold to $args[0][0]*20>>
    		<<elseif $args[0][0] gte 20>>
    			<<set $args[0][2] to 0>>
    		<<endif>>
    		<<if $args[0][2] gte $Threshold>>
    			<<set $args[0][0] to $args[0][0] + 1; $args[0][2] to $args[0][2] - $Threshold>>
    		<<endif>>
    	<<endif>><</nobr>>
    <</widget>>
    

    Here's the line I use to call the widget:
    <<RaiseStat "$Strength", 1>>
    

    And here's the output on the sheet (with the name of the next attribute included):
    Strength 1, 0, , 0, Training 
    Intelligence
    

    Now that the problem has moved with me changing which index I use, I suspect I'm doing something wrong in the widged, but, again, it shouldn't have run between starting the test and opening the character sheet.

    EDIT: Also, I've updated to SugarCube 2.8.0 now. That release must have happened a day or two after I found Twine. The update didn't change anything though.
  • Just saw your edit, the output is:
    ["1","0","0","0"]
    

    Buuut, I'm also an idiot. When pasting your line into the start passage I saw that I had forgotten about a call to run the widget from when I started working on it earlier. That call only had the variable name and no number to increase the training field with, so that's what the error was.

    Of course now I see that the training is not working (or at least not the way I want it to), but that I'll have to look at myself first.
  • Your widget is a wreck and you're invoking it improperly.

    The invocation should be:
    <<RaiseStat "$Strength" 1>>
    
    Note the absence of a comma. Macro arguments are separated by whitespace.

    As for the widget itself, try the following:
    <<widget RaiseStat>><<silently>>
    	<<run _stat to Wikifier.getValue($args[0])>>
    	<<set _stat[2] to _stat[2] + $args[1]>>
    	<<if ($NeedsFood and $Nutrition gte 1) or not $NeedsFood>>
    		<<set $Threshold to 10>>
    		<<if _stat[0] lte 10>>
    			<<set $Threshold to _stat[0] * 10>>
    		<<elseif _stat[0] lt 20>>
    			<<set $Threshold to _stat[0] * 20>>
    		<<elseif _stat[0] gte 20>>
    			<<set _stat[2] to 0>>
    		<</if>>
    		<<if _stat[2] gte $Threshold>>
    			<<set _stat[0] to _stat[0] + 1>>
    			<<set _stat[2] to _stat[2] - $Threshold>>
    		<</if>>
    	<</if>>
    	<<run Wikifier.setValue($args[0], _stat)>>
    <</silently>><</widget>>
    
  • edited September 2016
    *facepalms*

    Thanks, I'd have probably taken at least a day or two to notice the invocation was wrong. Other than that, my new (and now, with the correct invocation, working) version was this:
    <<widget RaiseStat>><<nobr>>
    	<<set $args[2] += $args[3]>>
    	<<if ($NeedsFood is true and $Nutrition gte 1) or $NeedsFood is false>>
    		<<set $Threshold to 10>>
    		<<if $args[1] lte 10>>
    			<<set $Threshold to $args[1]*10>>
    		<<elseif $args[1] lt 20>>
    			<<set $Threshold to $args[1]*20>>
    		<<elseif $args[1] gte 20>>
    			<<set $args[2] to 0>>
    		<<endif>>
    		<<if $args[2] gte $Threshold>>
    			<<set $args[1] += 1; $args[2] -= $Threshold>>
    		<<endif>>
    	<<endif>><</nobr>>
    	<<switch $args[0]>>
    		<<case "$Strength">>
    			<<set $Strength[0] = $args[1]; $Strength[1] = $args[2]>>
    	<</switch>>
    <</widget>>
    

    with
    <<RaiseStat "$Strength" $Strength[0] $Strength[1] 1>>
    
    as the (corrected) invocation.

    Yours does seem better of course. I think so far I haven't even looked at Wikifier.

    Thanks again!
Sign In or Register to comment.