Adding a durability system in an RPG game

Harlowe 3.2.2

I’m making an RPG game, and attempting to add a durability system. After thinking of a few ways I could implement this, I decided to start using arrays. For example

You mercilessly slaughter the ants, and pocket their corpses.
(set: $sdb to it-(a:“1”))
(set: $ant to $ant + (a:“1”,“1”,“1”,“1”,“1”,“1”,“1”,“1”,“1”,“1”))
(if: $sdb’s length > 0)[(set: $wep to it - (a:“wooden stick”))]
(else:)[You sniff out more ants. They make easy prey.]
You have (print: $ant’s length) ant corpses.

(if: $wep’s length > 0)[[[Kill More|Y]]]
(else:)[[[Kill More|broken stick]]]


This is the code I’m having issues with. Undoubtedly, it requires some explanation. I have an array set up, that is called “$sdb” (stick durability). This array has 5 different items in it, all of which are just "1"s. Basically, there are 5 different 1’s in this array, acting as different durability. In this code, each time the paragraph loads, it’s supposed to remove a 1 from the array.

There’s a second array, called “$wep” (weapons). This is just an array used for storage of the stick.
The line of code -

(if: $sdb’s length > 0)[(set: $wep to it - (a:“wooden stick”))]

is meant so that when all of the durability points are taking from the $sdb array, the stick will automatically be removed from its array.

Finally, when you run out of durability and no longer have a weapon, the “kill more” button will instead take you to another paragraph, which informs you that your weapon has broken. This, however, is not the case. I’ve been looking at the code for a decent amount of time now, and can’t seem to actually find what’s wrong with it. Could anyone help tell me what the actual problem is? That alone would help tremendously. Furthermore, if anyone has the actual code i could use instead, it would be appreciated. If anyone has an alternative method to the “array durability” method im attempting to use, feel free to share it. Thank you.

There’s no real reason to use these awkward arrays instead of values. Rather than popping a 1 from the array when a point of durability is lost, just set durability to a value and decrement the value by 1 whenever you need to.

I understand what you mean. However, I’m very new to coding, and even newer to the harlowe language. This was the first idea I had, but was unable to do it because I couldn’t find out how to actually do it, and the manual honestly doesn’t make sense to me, as I’m not very knowledgeable with the language, or coding at all. How exactly do I convert it to a value?

1 Like

Hey, buddy. If I would do it, I would make a variable for durability. Something like this (set: $stickdur to 5). Everytime you fight the ants, you make another set (set: $stickdur to $stickdur-1) and an If (If: $stickdur is <=0)[Your stick broke or something like that]. That’s my suggestion. I’m also new to twine so maybe someone has a better solution.


I figured out the problem about 10 hours after this post the other day, and I decided I would share it for other people who attempt to add a durability system.

Essentially, when I would remove a single “1” from the durability, it would instead remove ALL of the 1’s from the array. I’m not quite sure why it does this, but it does. Anyways, here’s the (obvious) solution I used, and should have used from the beginning.

I decided to use datamaps, for each weapon instead. So now, it would read like this:

(set: $woodenstick to (dm: "durability", 5)

In this datamap, you could obviously apply any other stats you’d like, such as damage. I then used this later to send the player to a different passage using this code:

(set: woodenstick's durability to it - 1) (if: woodenstick's durability = 0)[(go-to: "broken stick")]

This sets it so that upon loading of the passage, 1 durability is automatically removed from the stick. When all the durability is removed, you’ll instantly be sent to a new passage.

As a new coder, this was far more difficult to figure out than it should have been. I know I’m not the only new coder using this forum to increase their skills, so I hope anyone else having trouble with a similar system stumbles upon this and finds it useful.