Author Topic: Twine hack: inventory  (Read 12587 times)

Chris

  • Administrator
  • Full Member
  • *****
  • Posts: 201
  • The Admin
    • View Profile
    • Adventure Cow
Twine hack: inventory
« on: February 01, 2013, 02:30:04 PM »
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
<<endif>>

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

Liyamu

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Twine hack: inventory
« Reply #1 on: April 20, 2013, 09:25:00 PM »
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!

   -Liyamu

Chris

  • Administrator
  • Full Member
  • *****
  • Posts: 201
  • The Admin
    • View Profile
    • Adventure Cow
Re: Twine hack: inventory
« Reply #2 on: May 02, 2013, 02:20:59 AM »
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.

Liyamu

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Twine hack: inventory
« Reply #3 on: May 03, 2013, 07:14:54 PM »
Awesome!  Thanks so much.  I know it has its shortcomings, but the more I learn about Twine, the more I love it.

   Cheers,

   -Liyamu

loopernow

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Twine hack: inventory
« Reply #4 on: December 18, 2014, 10:53:51 PM »
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?

Milici

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Twine hack: inventory
« Reply #5 on: May 20, 2017, 01:12:57 PM »
Is there a way to add more than one item to $carrying?


Hey guys does anyone know the answer to this?
« Last Edit: October 13, 2021, 01:23:53 PM by Milici »

Chris

  • Administrator
  • Full Member
  • *****
  • Posts: 201
  • The Admin
    • View Profile
    • Adventure Cow
Re: Twine hack: inventory
« Reply #6 on: July 15, 2017, 01:52:21 PM »
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?

Maybe this? I haven't tested it.

Code: [Select]
<<set $carrying={'potions':0}>>
(all numbered items must be counted)

<<set $carrying['potions'] += 1>>
(This is code for "I picked up 1 potion.")
<<set $carrying['potions'] -= 1>>
(This is code for "I dropped 1 potion.")

<<if $carrying['potions']>>
You are carrying at least 1 potion.
<<endif>>