0 votes
by (170 points)
closed by

Hi, I am quite new to this so bare with me.

I want to start a quest if the player has an specific item, but the text to start the quest is not showing.

I have atm this:

<<set $inv = []>>

<<set $SchoolTimeTable = {
	"name" : "School Time Table", 
	"Description" : "Shows you what class you have at what time.",
}
>>
	
<<set $Phone = {
	"name" : "Phone",
	"Description" : "Your Phone",
}
>>

<<set $Keys = {
	"name" : "Keys",
	"Description" : "Your house Keys",
}
>>


<<set $inv.push($SchoolTimeTable, $Phone, $Keys)>>

This all shows in the inventory.

But this line shows nothing.

<<if $inv.contains($SchoolTimeTable)>>
	Start Quest!
<</if>>

Anyone have an idea what I am doing wrong, I'm not getting a error when I run it but nothing is showing.

closed with the note: Go it working

1 Answer

+1 vote
by (149k points)
edited by

When you assign an Object to a variable like so

<<set $Keys to {
	"name": "Keys",
	"Description": "Your house Keys"
}>>

... the variable actually contains an Object Reference to where the actual Object is being stored in memory, and you can't use the <array>.contains() function to look for an Object Reference.

Background information:
Each time a Passage Transition occurs then current state of all known story variables of copied, the original is added to the History system and then copy is made avaiable to the Passage about to be shown.

This means that after a Passage Transition your $Phone variable no longer references the same Object it did before that transition and the same is true for the $inv Array, which also means that the story variable and the Array are now referencing two different copies of the original 'Phone' Object.

This is why we recommend defining your Object using "keys" like so

<<set $items to {}>>

<<set $items["SchoolTimeTable"] to {
	"name": "School Time Table", 
	"Description": "Shows you what class you have at what time."
}>>
<<set $items["Phone"] to {
	"name": "Phone",
	"Description": "Your Phone"
}>>
<<set $items["Keys"] to {
	"name": "Keys",
	"Description": "Your house Keys"
}>>

... and to use those "keys" when doing things like tracking inventory like so

<<set $inv = []>>
<<set $inv.push("SchoolTimeTable", "Phone", "Keys")>>

You can now use those "keys" to determine if the $inv story variable Array contains a specific item, and also access the properties of a specific item.

<<if $inv.contains("SchoolTimeTable")>>
	The inventory contains a <<= $items["SchoolTimeTable"].name >>
<</if>>
<<if $inv.contains("Phone")>>
	The inventory contains a <<= $items["Phone"].name >>
<</if>>
<<if $inv.contains("Keys")>>
	The inventory contains a <<= $items["Keys"].name >>
<</if>>


NOTE:
If the properties of an Object do not change after it has been initialised then you should define it on the special setup Object instead, that way it doesn't consume storage space in either History or Saves.

<<set setup.items to {}>>
<<set setup.items["SchoolTimeTable"] to {
	"name": "School Time Table", 
	"Description": "Shows you what class you have at what time."
}>>
<<set setup.items["Phone"] to {
	"name": "Phone",
	"Description": "Your Phone"
}>>
<<set setup.items["Keys"] to {
	"name": "Keys",
	"Description": "Your house Keys"
}>>

<<set $inv = []>>
<<set $inv.push("SchoolTimeTable", "Phone", "Keys")>>

<<if $inv.contains("SchoolTimeTable")>>
	The inventory contains a <<= setup.items["SchoolTimeTable"].name >>
<</if>>
<<if $inv.contains("Phone")>>
	The inventory contains a <<= setup.items["Phone"].name >>
<</if>>
<<if $inv.contains("Keys")>>
	The inventory contains a <<= setup.items["Keys"].name >>
<</if>>

 

by (170 points)
That you very much this was very informative.
by (170 points)

When It comes to showing the name and the description of the objects on the inventory page it is showing nothing.

<<for $i = 0; $i < $inv.length; $i++>>
<<set $item = $inv[$i]>>
<<print $item.name>>
<</for>>

This did show what I wanted it to show but since I put the code in it no longer shows.

So I created a second array. The first array has the $variables in it, the second has the objects and when I want to .push I need to add a $veriable and a object. This way I can see the items I have in my inventory and use .contains to see if the play has the item to start the quest.

It might be the wrong way to do it but it works. haha.

Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...