Educational > Twine

Twine hack: inventory

(1/2) > >>

One tricky thing with standard Twine is inventory carrying. Adventure Cow has custom macros for dropping and picking up items, but I thought it'd be interesting to see what can be done with Twine by itself:

Start with this line:
<<set $carrying={}>>

Then, if you want to pick up an item:
<<set $carrying['axe'] = true>>
(This is code for "I'm carrying a sword.")

Now you can check for carrying as follows:
<<if $carrying['sword']>>
You are carrying a sword

Notice this will work for items that aren't discovered yet.
<<if $carrying['axe']>>
You are carrying an axe

Got a quick question for you.  I don't quite understand why you need to start with the first line:

   <<set $carrying={}>>

   By start, do you mean put that in your start lexia/first page/thingy?  Basically, I don't get what the bent brackets {} mean.  Is that telling Twine that there will be various values added to $carrying in the future? and therefore prevent you from having to add a $carrying['axe'] = false in the beginning?  I'm hoping that's the case; it would be a lot more clean/compact.

   Still pretty new to all this, thanks in advance!


Hey Liyamu,

Yeah, that first line should go in start, before any mention of axes. Twine has a bit of JavaScript at its core, and the bent brackets basically tell Twine "hey, $carrying is a container for other properties." When we say $carrying['axe'], we mean "Hey, remember that container, $carrying? Let's look inside that container for the axe." If we haven't first initialized (created) that container, then we can't look inside it.

Hope that explains everything! Let me know if you have other questions.

Awesome!  Thanks so much.  I know it has its shortcomings, but the more I learn about Twine, the more I love it.



Is there a way to add more than one item to $carrying?

Is there a way to remove an item, but not all items, from $carrying?

Is there a way to see if one item out of several is in $carrying?


[0] Message Index

[#] Next page

Go to full version