Error: "There isn't a temp variable named _? in this place."

Twine Version: web, version 2.3.15
Story Format: Harlowe 3.2.3

tl;dr: i call a passage via the (display:) function with a temporary variable and the passage that is doing the displaying yells at me.

the long version:
Sorry if this is a duplicate question but I’ve been searching for a day and haven’t found the answer ^^;
So, in the story I’m writing, every so often, an event happens and the character gets a random item. I made a passage called “item generator” and call it with the display function at random intervals throughout the story game.

The code in “item generator” looks like this:

{
  (set: _fullItemNames to (datanames: $allItems))
  (set: _fullItemDeets to (datavalues: $allItems))
  (set: _randItem to (random: 1,_fullItemNames's length))
  (set: _itemKey to _fullItemNames's (_randItem))
  (set: _itemValue to _fullItemDeets's (_randItem))
  
  (if:$invo does not contain _itemKey)
  [
	  	=><=
		(b4r-color:blue)+(b4r:"ridge")[You acquired the item "_itemKey".]
		(set: $invo to it + (dm:_itemKey,_itemValue))
  ]
}

This works fine!
But whenever a passage calls the “item generator” it will create the item and add it to the inventory and display the information as intended, but I also get the error “There isn’t a temp variable named _itemKey in this place.”

A sample passage (called “Test”) that it gets called on looks like this:

The road was (either: "long", "gloomy", "dark", "quiet", "bustling") and you weren't sure what to do.
(display:"item generation")
Maybe you should head into [[the city->City 1]] or [[the complex->Complex 1]]. Or maybe [[just walk|Test]]?

And every time the “You acquired _itemKey” message appears, so does the error.
I don’t understand why Test is looking for _itemKey when it was:

  1. created in “item generation” and
  2. isn’t referenced in the code for “Test” at all.
    I don’t want to have to replicate the code in “item generation” every time I use the function (especially since it’s uh… much larger than this but I’m parring it down so y’all don’t need to read several dozen lines of case uses) but I also don’t want to deal with this error every time so… help?

The (display:) macro injects the content of the referenced Passage into the content of the ‘current’ Passage being processed. And while the (display:) macro can be used to execute a “code only” Passage, it is not the equivalent of calling a function() that has it’s own variable scoping.

eg. So basically the contents of your Test passage…

The road was (either: "long", "gloomy", "dark", "quiet", "bustling") and you weren't sure what to do.
(display:"item generation")
Maybe you should head into [[the city->City 1]] or [[the complex->Complex 1]]. Or maybe [[just walk|Test]]?

…becomes the equivalent of…

The road was (either: "long", "gloomy", "dark", "quiet", "bustling") and you weren't sure what to do.
{
  (set: _fullItemNames to (datanames: $allItems))
  (set: _fullItemDeets to (datavalues: $allItems))
  (set: _randItem to (random: 1,_fullItemNames's length))
  (set: _itemKey to _fullItemNames's (_randItem))
  (set: _itemValue to _fullItemDeets's (_randItem))
  
  (if:$invo does not contain _itemKey)
  [
	  	=><=
		(b4r-color:blue)+(b4r:"ridge")[You acquired the item "_itemKey".]
		(set: $invo to it + (dm:_itemKey,_itemValue))
  ]
}
Maybe you should head into [[the city->City 1]] or [[the complex->Complex 1]]. Or maybe [[just walk|Test]]?

…which is why any error relating to a missing temporary variable is shown in your Test passage, because temporary variables are associated with the ‘current’ Passage being “visited”.

You didn’t supply details about the story variables you are using, but I created the following test Harlowe 3.2.3 project and I didn’t get an error when I viewed the Test passage.
(note: example written using TWEE Notation)

:: Startup [startup]
(set: $allItems to (dm: "Aaaaa", 1, "Bbbbb", 2, "Ccccc", 3))
(set: $invo to (dm:))

:: Start
[[Test]]

:: Test
The road was (either: "long", "gloomy", "dark", "quiet", "bustling") and you weren't sure what to do.
(display: "item generation")
Maybe you should head into [[the city->City 1]] or [[the complex->Complex 1]]. Or maybe [[just walk|Test]]?

:: item generation
{
  (set: _fullItemNames to (datanames: $allItems))
  (set: _fullItemDeets to (datavalues: $allItems))
  (set: _randItem to (random: 1, _fullItemNames's length))
  (set: _itemKey to _fullItemNames's (_randItem))
  (set: _itemValue to _fullItemDeets's (_randItem))
  
  (if: $invo does not contain _itemKey) [
	  	=><=
		(b4r-color: blue) + (b4r: "ridge")[You acquired the item "_itemKey".]
		(set: $invo to it + (dm: _itemKey, _itemValue))
  ]
}

thanks for commenting!
i didn’t provide my whole code 'cause it’s currently clocking in over 4k over 37 passages lol.
your test code worked great and i’m kind of at a loss why that’s fine but mine kept throwing errors. I eventually just… ended up rewriting it a bit and they stopped so, score? i’m thinking in one of the sections i was missing brackets.

thank you again! idk how to mark stuff as closed but this worked for me