How do I make changing passages and Npc

Twine Version: 2.3.15
Story Format: Sugarcube v2

Hi, I'm working on my very first game and I'm very new to basically everything. 
My question is how do I get NPC to move passages or how do I show different events in passages?
The games that have this feature are the company and young maria. 
Should I be making the passages as I got to feature the characters? but then how does that work if I need the player to go to the kitchen multiple times and for different things to happen.

edit: also im really confused on how i would make this code work, its a shop of my game
Math Book $20 <<link "Buy" "passage()">><<set $money -= 20>><<addToInv "Mathematics Book">><</link>>

There’s literally an infinite number of different ways you could do this, so the best way to do it greatly depends upon exactly what you want to do.

I note that both “The Company” and “Young Maria” are Twine games, so you could actually just import their HTML files into the Twine editor to see their code and how they do different things.

Speaking very generally, you’ll use story variables to track data during the game, and then you can use macros, like the <<if>> macro, to make different things happen based on the data in those variables. You can also use the SugarCube random() function if you want to make things happen randomly.

Now, I apologize for getting technical for a moment, but this is useful information, and it should hopefully become clear by the end.

For example, you could create an NPC like this:

<<set $NPC = []>>  /* Creates an array for multiple NPCs */
<<run $NPC.push( { name: "Anne", location: "Passage X" } )>> /* Adds an NPC to the array */

That creates an array, and then adds a generic object to that array, which holds a name and location for that NPC. The .push() method is a way of adding data to the end of an array.

You can refer to elements in an array by using an index number, and the first index in an array is always 0. You can get values from the array like this: array[0], where 0 is the index of the value you want to get from the array. (Note: an array is a specific type of object.)

Generic objects, on the other hand, use keys (also called properties) to access the values. You can get the value of a key in an object by using that key/property name. This can be done using “dot notation” for simple key names (e.g. names composed of just letters) by doing this:, or using “bracket notation” by doing this: object["property"].

You can also put variables within the brackets of arrays and objects to determine which value you get back from them, based on the value of that variable. For example:

<<set _i = 0>>
<<set _key = "name">>
<<set _name = $NPC[_i][_key]>>

works the same as if you’d done this:

<<set _name = $NPC[0].name>>

That method is useful if, for example, you need to use a <<for>> loop to look through a whole array for something.

So, if you wanted to check to see if the first NPC in the $NPC array was in the current passage in Twine, you could use code like this:

<<if $NPC[0].location === passage()>>
	<<= $NPC[0].name>> is here.

And that would display that that NPC is present if the NPC’s location property matched the name of the current passage, which is gotten from the SugarCube passage() function.

That’s just a simple example to give you an idea of what is possible.

If there are complicated pieces of code you need to run in multiple passages, then you should look into turning them into a <<widget>> or a macro, so that you don’t need to copy-and-paste chunks of code repeatedly.

You’d need to have the story variable $money hold the amount of money the player has, and also create either a widget or a macro named “addToInv” which adds the named item to the player’s inventory, for that code to work. Other than those two things, it should work as-is (though, I’d recommend changing it to make sure that they don’t already have a “Mathematics Book” in their inventory and that they have at least $20 to spend).

If you’re looking for more specific help, then you’ll need to ask a more specific question.

Hope that helps! :slight_smile:

P.S. You should only put code within a code block, not your whole post. Putting your whole post in a code block like you did means we have to scroll to the right to see all of your text.