Iterative names for variables

Twine Version: 2.3.7
Story Format: Sugarcube 2.31.1

So I’m new to both Twine and JS and I’m trying to figure out if it’s possible to create variables with iterative names. Something like (at least conceptually):

 <<set $var[var_num] {
"name" : "blah"
}>>

Where [var_num] is another variable that increases by one every time you create such a new variable. So as you do this during the game you get $var001, $var002, etc.

The idea is to generate randomized NPCs with different names, appearances, backstories, and so on, creating a variable unique to each one, and to be able to do that indefinitely. I can create and set up these NPC objects at an individual level no problem, but I’ve been digging around and can’t figure out a way to set variable names other than hardcoding the whole thing in with <>, or if I’ve found it I don’t understand it well enough to recognize it.

I can see how you could get close by just manually making a giant list of, say, $npc001 to $npc999, popping it into StoryInit, and then just randomizing their attributes when they come into the story rather than actually creating a new variable, but I imagine there’s a much more efficient way of doing that, (which maybe also doesn’t leave you with a maximum number).

Thanks! And apologies if I’m coming at this entirely wrongheadedly, lol.

What you’re describing is basically an array. So you could do that like this:

<<set $NPC = []>> /* Initialize the array */
<<set $NPC.push( { name: "Anne", HP: 100 } )>> /* Add $NPC[0] data. */
<<set $NPC.push( { name: "Bob", HP: 100 } )>> /* Add $NPC[1] data. */
<<set $NPC.push( { name: "Charlie", HP: 100 } )>> /* Add $NPC[2] data. */

You can then access that data like this:

NPC name: <<= $NPC[0].name>>

Which would print out: NPC name: Anne

So you might want to read up on how to use arrays at the link I gave above.

That said, I’d recommend not creating any NPCs like that until you need them, and then deleting them when you’re done with them. The reason why is that all of that data takes up space in the game’s history, and the more data that’s there, the slower that passage transitions, saves, and loads will be.

So, hopefully that helps you figure out how you can do things in a better way. :slight_smile:

P.S. This forum eats code within angle brackets unless you put it within a “preformatted text” block (use the </> button in the editor window).

Ohhh, okay, yeah I see. That’s far more direct and sensible than what I had in my head. Also thanks for telling me not to be ridiculous and try to have tons and tons of these sort of NPCs. Sometimes I get carried away with trying to leave options open in things, but I probably don’t really need more than a handful at a time and I needed someone to kind of smack me over the head about that (in a very friendly sort of way).

Thanks for the help!

I hope you don’t mind me jumping in on this, but to go off your comment-

I took a quick look through the link you posted but wasn’t quite sure- how would you go about deleting them when you’re done?

There are actually a bunch of ways to do it. You can see a list of them in the help file for my Universal Inventory System (UInv) in the “Arrays vs. Generic Objects” section.

If you’re making a Twine/SugarCube game which needs an inventory system, then you might want to check out UInv too while you’re at it.

Hope that helps! :slight_smile: