Documentation has failed me again. Every reference I can find to temporary variables seems to indicate that they are invoked exactly the same way as permanent variables, except they start with _ instead of #. My permanent variables are working fine, but every usage of a temporary variable is throwing an “undefined” error.
Do I have to initialize temporary variables in some particular way, and if so, why would that be different from a normal <<set>> statement or equivalent?
One example is this for loop:
<<set _temp = random(1,$dirlist.count)>>
<<for _i to 0; _i < $dirlist.count; _i++>>
<<if _i is _temp>>
<<set $dirlist[_i] = $curw>><<endif>><</for>>
Result:
Error: <<set>>: bad evaluation: assignment to undeclared variable _temp
Error: <<for>>: bad init expression: assignment to undeclared variable _i
Replacing _i with $i works fine and clears the _i error. I could just not use temporary variables, replacing them all with permanent ones, but I want to save memory and understand why this isn’t working.
<<endif>> is deprecated, you should use <</if>> instead
(also wrapping the macro with a → ` ← will make sure the post won’t eat it )
Normally a loop works just fine with a temporary _i
What are you trying to attempt here with the loop?
Where is the loop code placed?
And how is $dirlist and $curw set?
Ok, noted! I last used Twine some years ago, but I’m using what I knew then on a new project.
I did use the Preformatted text markup on my code block. Did you mean something else?
I have a list of items in an array, and I am assigning one of them to each of the directions corresponding to the dirlist index. One direction is randomly chosen to hold a specific item and the other three are randomly pulled from the list. The for loop is a standard iteration over the dirlist indices (from 0 to $dirlist.count - 1).
The loop code is placed in a passage following some additional set statements designed to make temporary copies of the relevant data arrays. Those temporary variables aren’t working either.
$dirlist is set by the for loop. $curw is actually a placeholder variable I used for the purposes of making my code snippet more standalone for my help question. It’s really _curword, which is being set as a temporary variable above the for loop to identify the correct item from another array.
After the items are set, I’m displaying the available directions with their items in a table layout so the player can choose which way to go.
For those:
~~
It would be useful to see how you set those variables, as in the <<set $var....>> line. (It makes it easier for us to test potential solution on more complex type of code.
How is that set as well?
I think in this case, it would be easier if you showed the code you currently have
I meant the whole code, including how the relevant variable in the loop are sets. You have explained how they are set, but not shown the code (the one in your file).
Especially the :
Okay, I tried the following in its own separate passage and tested the passage directly, but I still get undeclared errors for both _temp and _i:
<<set $dirlist to ['one', 'two', 'three','four']>>
<<set _temp = random(1,$dirlist.length)>>
<<for _i to 0; _i < $dirlist.length; _i++>>
<<if _i is _temp>>
<<set $dirlist[_i] = "test">><<endif>><<print $dirlist[_i]>><br><</for>>
Errors:
Error: <<set>>: bad evaluation: assignment to undeclared variable _temp
Error: <<for>>: bad init expression: assignment to undeclared variable _i
Edit: I also tried a completely new (Sugarcube 2) project with just that passage alone, and I get the same thing. So it isn’t anything else in my project clashing with the temporary variable definitions or anything. Temporary variables just aren’t working for me, even if I strip everything else out. What could be causing this?
Can you publish the new project with just the test passage, and attach the html here? (you probably have to zip it up, IIRC the forum doesn’t allow direct html attachments)
Edit: by any chance are you using a very old version of Sugarcube? It looks like temporary variables were added in v2.3.0, but that was from the beginning of 2016, so…
It’s very possible. My Sugarcube zip is from January 2016 and is labeled 2.1.2. I didn’t know the changes since then were so extensive. I will try to update both Twine and Sugarcube and see if that helps. Thanks.
Edit: I updated to the latest Twine with the latest Sugarcube, but now of course Twine is giving me all sorts of other errors because it doesn’t like how I’ve done things in the older version. For instance, I thought I could add whatever tags I wanted to passages to identify them as different categories; at least, that’s how I’d been using tags previously, but it’s complaining that “init” isn’t a valid tag, that a particular passage doesn’t exist when it, in fact, does exist, etc. I guess I will have to start over again and recreate everything, because the story doesn’t even run at all now, nevermind the variables.
I’m a little surprised I can’t use the init tag manually, though. The documentation says it’s for “pre-story-start initialization tasks, like variable initialization”. That’s exactly what I’m doing in these passages. Yes, I’m also using the StoryInit passage, which is then <<include>>-ing these other passages in order to split out the different types of data that I’m initializing, to keep it organized. Seems like that’s exactly what the init tag should be for.
The init tag makes passages behave very similar to StoryInit passage, they’re automatically processed. Processing them manually would run them again, which you really don’t want.
That said, that’s not what the error is about. You didn’t give the full error so I can’t tell you exactly what’s wrong, but it should fall into three possible categories:
You tagged the starting passage with special tags that aren’t legal on it.
You tagged a special passage with special tags that aren’t legal on it.
You combined multiple special tags that aren’t legal together.
The error you received should have explained which of those you ran afoul of.
I misunderstood the error, I think. I tried it again and noticed it was specifically complaining about the StoryInit passage having the init tag. I had thought it was complaining about all of them. So now I understand: StoryInit will run automatically anyway, and anything else I tag with init will also run automatically before StoryInit. Works for me. Thanks!