Re: Using Arrays in Twine Macros

1,166 views
Skip to first unread message

Chris

unread,
Jan 31, 2013, 11:23:39 PM1/31/13
to twee...@googlegroups.com
Hey Shades,

Declaring an array, at least, is somewhat straightforward:

<<set $inventory = ['stone', 'hammer']>>
<<print $inventory>>
<<print $inventory[0]>>
<<set $inventory[0] = ['ice']>>
<<print $inventory>>

This will print the following:
stone,hammer
stone

ice,hammer

Where this gets dicey is trying to find specific items.

What I'm about to suggest is slightly voodoo, so I should also point out that I'm working on an engine that has direct <<pickup>> and <<putdown>> style macros. (If you're interested in that send me an email!) In the meantime...

<<set $carrying={}>>
<<set $carrying['axe'] = true>>
<<if $carrying['sword']>>
You are carrying a sword
<<endif>>
<<if $carrying['axe']>>
You are carrying an axe
<<endif>>

On Thursday, January 31, 2013 11:01:32 PM UTC-5, Shades wrote:
Hi there,

I'm new to both Twine and Google groups so forgive me in advance if I'm overlooking something obvious. I'm attempting to write a small gamebook style adventure using Twine and would like to use an array to keep track of some inventory items. Is this possible, and if so, how does one go about creating and accessing the array?

Thank you! :)

Porpentine

unread,
Feb 1, 2013, 9:56:55 AM2/1/13
to twee...@googlegroups.com
someone just made a thing for javascript arrays in Twine!

http://www.glorioustrainwrecks.com/node/5034

Christopher Liu

unread,
Feb 1, 2013, 2:30:58 PM2/1/13
to twee...@googlegroups.com
Cool!

The main issue I've found with JS arrays for inventory is that an array is like a long list of cubby boxes. If you drop an axe, you either have to have a specific numbered slot it's in, or a way of searching every single cubby for the axe, to drop it.

I would recommend JS objects for inventory, mainly because $carrying['axe'] doesn't require you to know whether the axe is the first or last item in the array. I'll post this so people off the list can see it: http://forums.adventurecow.com/index.php?topic=51.0

On Fri, Feb 1, 2013 at 9:56 AM, Porpentine <aliend...@gmail.com> wrote:
someone just made a thing for javascript arrays in Twine!

http://www.glorioustrainwrecks.com/node/5034

--
You received this message because you are subscribed to the Google Groups "Tweecode / Twine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tweecode+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Christopher Liu

unread,
Feb 1, 2013, 2:45:41 PM2/1/13
to twee...@googlegroups.com
It's an associative array. Twine is Javascript under the hood.





With some annoying changes - I would much rather we were honest about using == instead of this "eq" business. :)

On Fri, Feb 1, 2013 at 2:38 PM, Shades <wolfe....@email.com> wrote:
Thanks for the fast replies! As it turns out, I might not need a full blown inventory setup for the little test adventure I'm doing now (not sure, still working it out), but I'll definitely be needing it for the more advanced games I want to make.

Btw, Chris -- that hack of yours uses something that looks like what Perl calls an associative array. Is that something Javascript does also, or am I looking at something completely different there?

HarmlessTrouble

unread,
Feb 1, 2013, 5:48:04 PM2/1/13
to twee...@googlegroups.com


On Friday, February 1, 2013 8:45:41 PM UTC+1, Chris wrote:

With some annoying changes - I would much rather we were honest about using == instead of this "eq" business. :)

You can still use == or any standard javascript operators in your if statement macros. The Twee operators are optional.

-Henry

Christopher Liu

unread,
Feb 1, 2013, 5:58:48 PM2/1/13
to twee...@googlegroups.com
Indeed, something I'm relieved to have!

--

Miguel Garza

unread,
Dec 18, 2014, 2:46:44 PM12/18/14
to twee...@googlegroups.com
Non-programmer question:

"The main issue I've found with JS arrays for inventory...I would recommend JS objects for inventory"

Are you saying there is a difference? I followed the link you provided, and the syntax you use looks exactly the same as that used at the glorioustrainwrecks link ("How to use Javascript arrays in Twine"). Is using JS objects instead simpler? If so, how do you remove items from the inventory?

I think JS arrays work just fine, but as a non-programmer, its hard to remember the verbiage needed to remove an item from inventory. If you're saying using JS objects is simpler, I hope you mean the syntax is a little bit less tortured!

Tobias Beer

unread,
Dec 18, 2014, 4:11:07 PM12/18/14
to twee...@googlegroups.com
 
glorioustrainwrecks link ("How to use Javascript arrays in Twine"). Is using JS objects instead simpler? If so, how do you remove items from the inventory?

It's neither simpler nor harder. The only differece is how the internal accessors work which...

CREATE
READ
UPDATE
DELETE

...items in that inventory.

In really old fashioned systems you were possibly given a number of slots, e.g. 20. The reason why is plain simple: someone implemented a basic array whereas in every numerically indexed slot there was a reference to something it was supposed to contain.

In that sense, object oriented notation such as...

inventory.axe

is much more straight forward, although you begin asking: What if I needed more than just one, axe?!? ...e.g.:

inventory.health-potion

?

Exactly... so, it all boils down to a programmer implementing the required accessors that give you the information you need and allow you to create read, update, delete, just that.

Best wishes, Tobias.

Miguel Garza

unread,
Dec 20, 2014, 11:54:06 PM12/20/14
to twee...@googlegroups.com
Thanks. I was inspired by these various posts to do a little Googling around, a slight bit of self-education on Javascript, and I came to opposite conclusion of poster (Chris) who said objects are better than arrays for inventories. As you point out, its harder to have more than one of a thing with the same name if you are using object properties rather than array values. I think, like you point out, Chris prefers objects because of what happens under the hood. But I still think syntax of arrays is more user-friendly when it comes to inventories specifically. Admittedly, I know very little about all of this, but each one has their preferences!

Thanks again!
Reply all
Reply to author
Forward
0 new messages