Using player input to change a variable based on their choice (harlowe 2.1)

0 votes
asked Jun 5 by sirjohnthetall (160 points)
edited Jun 5 by sirjohnthetall
In this case, the player is choosing a fantasy race, which I want to change one of their attributes.  At first, I was going to use if/else, but when I checked that, I learned it had to be Boolean, and I couldn't just use set to change the variable.  I've tried unsuccessfully to use a couple things, but I'm not getting anywhere.

Advise or suggestions?

Edit:  I hope this doesn't come off as snarky, but after reading the first answer, I thought I'd put a clarification:  I know that "if/else" statements can't be used in conjunction with "set" statements.  I'm trying to get a player to input a fantasy race which will cause a change to a numerical value.  So if they choose "elf" the value of the player's dexterity increase by 2.

Sorry, if that seems snarky.

2 Answers

0 votes
answered Jun 5 by Chapel (46,530 points)
edited Jun 5 by Chapel

The expression has to yield a boolean value, and that's it. 

You can use comparison operators, like is, >, <, >=, <=, contains, etc to compare two values or variables and get a boolean. 

{
(if: $race is 'elf')[
    You're an elf! 
](else:)[
    You're not an elf. 
]
}

While it's true that (if:) only accepts boolean values, like in most programming languages, it can evaluate an expression to get to that value. 

+1 vote
answered Jun 5 by greyelf (88,790 points)

Please use the Question Tags to state the name and full version number of the Story Format you are using, as it helps with filtering & searching. Adding this information to the Question Title is of little benefit.

You didn't supply an example of how you are initialising your current player variable nor how you place to track their dexterity, and there are a number of different ways this could be done (distinct variables for each attribute; a single Data-Map containing all the attributes, etc...). I'm guessing you may be using the second option so I have used that in my solution.

1. Initialise your player related attributes within your protect's startup tagged special passage.

(set: $player to (dm:
	"Name", "Abcde",
	"Race", "Uknown",
	"Dexterity", 0
))

2. Add TwineScript like the following to the Passage containing the Race selection. It displays the current values of each of the player's attributes and then uses a (link:) macro to allow the Reader to select which race the player is (1).
I have also included an example of how to use a (replace:) macro combined with a named hook to refresh the currently displayed attributes information.

<u>Player</u>
Name: (print: $player's "Name")
Race: [(print: $player's "Race")]<race|
Dexterity: [(print: $player's "Dexterity")]<dexterity|

Select a race for the player: {
	(link: "Elf")[
		(set: $player's "Race" to "Elf")
		(set: $player's "Dexterity" to it + 2)
		(replace: ?race)[(print: $player's "Race")]
		(replace: ?dexterity)[(print: $player's "Dexterity")]
	]
}

(1) I have only implemented the "Elf" link but you should be able to use it as a guide you adding other races to the selection.

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.
...