I want to be able to assign one person to a room. I have these 3 rooms with clickable links:
where $room01, $room02, and $room03 will be the names of the people assigned to that room once selected (default is initialized to "none" when you begin). AssignRooms is the passage where you get to select the people.
I have the following people variables in an array (note: as the player progresses and meets new people, more names will be added here so it's not static):
<<set $temprooms = ["Lydia", "Desi", "Jereck", "Rawn", "Teleri"]>>
Here's what I have for the AssignRooms passage for room01:
Room #1: $room01
<<for $i to 0; $i lt $temprooms.length; $i++>>
<<link $temprooms[$i]>>
<<set $room01 to $temprooms[$i]>>
<<goto "Rooms">>
<</link>>
<</for>>
The problem is that $room01 literally returns "$room01" as a result and not a name when you go back to the Rooms passage. But all the << link $temprooms[$i] >> links do give the right name while still in the AssignRooms passage.
I tried changing up the code to this:
Room #1: $room01
<<for $i to 0; $i lt $temprooms.length; $i++>>
<<set $room01 to $temprooms[$i]>>
<<link $room01>>
<<goto "Rooms">>
<</link>>
<</for>>
and now no matter who I select, $room01 will always return "Teleri" as the result - always the last name in the array. But the << link >> macro still returns the right name. Is there a way to fix this?
Second question: Once a person is assigned to Room01 for example, I would like their name removed from the array so that they don't get selected for $Room02 or $Room03 anymore. Is that possible?
Thank you! This is for Twine 2 and Sugarcube 2
Comments
Also, in the future, please specify the full versions of the software involved—e.g. Twine v2.0.11 & SugarCube v2.11.0. Yes, it can make a difference.
That is not what is happening. The naked variable markup only replaces something which looks like a variable if a) such a variable actually exists and b) has a defined value. In your code above, the contents/interior of any of the <<link>> macros aren't evaluated until the player clicks upon them. By that point, your loop index variable $i is out-of-bounds, so $temprooms[$i] yields the undefined value. Since $room01 gets assigned undefined, it fails the second requirement of the naked variable markup and is not replaced.
What you need to do is to force the contents of the <<link>> macros to be evaluated as they're created within the loop. You currently have to do that using the Stupid Print Trick™.
Additionally, two other issues. You should be using temporary variables where you can, so that you do not bloat your story history unnecessarily—e.g. _i vs $i. You do not need the <<goto>> there, <<link>> is sufficient.
(example below)
Yes. You'll probably want to use the <Array>.deleteAt() method for that.
EXAMPLE:
Untested, but should work. NOTES:
It works for my first question, but not the second. When I choose for Room#2 though, it looks like I can still choose the same name that had already been selected for Room#1? It doesn't look like the name is removed from the list once selected, because I can get this result --
Should I need to add something else?
Beyond that, there are other issues, now that I'm thinking about it. If the player attempts to reassign a room, the original assignee won't be in the list and it's currently possible to exhaust the list thereby blocking the assignment of new rooms.
I'd suggest something like the following instead.
StoryInit special passage
Rooms passage NOTE: The $room story variable should be set to the name of the room variable you want to set, minus the $-sigil. This will allow AssignRooms to be simplified.
AssignRooms passage NOTE: The $room story variable is used to unassign the current room story variable and is printed so that it becomes the appropriate room story variable within the generated <<link>> macros.