0 votes
by (590 points)

Hello, I would like to modify a variable name within a replace command's hook. I'm not sure how to describe it, I think if i knew how to even describe the problem I could solve it, but here is what I have so far as an example:

(set: $a1 to "Hello")
(set: $a2 to "How are you?")
(set: $a3 to "Won't you talk to me?")
(set: $counter to 1)
(link-repeat: "Continue")[
(set: $counter to it + 1)(replace: ?speech)[$a$counter]

For this, only two lines would ever be displayed "$a1 and the continue link", which when clicked replaces those two lines with "$a2 and another continue link". it is trying to simulate the conversation system in use on many other games/rpgs.



/\ for my example, i get this to display initially, which is good. but when i click Continue, what displays is:


Ity evaluates $a$counter as 02. what i would like to happen is for (replace: ?speech)[$a$counter] to evaluate as "$a2" which would display that string's contents "How are you?".

I don't know how to do this though. i tried using parentheses in all different places:


i thought maybe it was the replace command limitation so i tried setting the evaluation to a string variable, and then having the replace: use that variable, but it's the same


all help appreciated as always,


1 Answer

+1 vote
by (63.1k points)

Use arrays. This is exactly what they're designed for. 

(set: $a to (a: "Hello", "How are you?", "Won't you talk to me?"))\
(set: $counter to 1)\
|speech>[(print: $a's 1st)]

(link-repeat: "Continue")[
    (if: $counter < 3)[
        (set: $counter to it + 1)
        (goto: 'next passage')
    (replace: ?speech)[
        (print: $a's ($counter))

Trying to use numbered variables is essentially just recreating the wheel. It is possible, but pointless. 

by (590 points)
oh man that's genius! i don't think i'd be able to understand arrays if not for this. thanks for baby feeding me.

also i was able to understand the function of { and } from your post, double thank you
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.