Hi. I want to create links from an array of objects in a loop and that when a link is selected, the selected object is passed to a variable. But something is going wrong… Most likely, the engine is not adapted for this… What kind of crutches are there or what was I wrong about?
The problem is that the contents of the link macro only execute on click, so your for loop has already run, and thus the value of $npcFreeArray[_i] isn’t what it was when the link was printed.
Luckily, this is exactly what the capture macro is for!
Also, a couple of things not directly related to your problem:
You can only print specific properties of an object; trying to print the entire contents of the object will just get you [object Object], so even when everything’s working properly, enemy $enemy will always show enemy [object Object]. enemy $enemy[0].race will display as (e.g.) enemy goblin, though, if that’s what you’re going for!
I don’t think the <<goto "Start">> in StoryInit is causing any problems as far as I can tell, but it’s not necessary. The player will start on whichever passage you’ve marked as the starting passage automatically.
It’s not just about _i… I have a suspicion that the engine does not see the array… it’s as if after working out the cycle, the functionality disappears.
Actually, I see it now — you’re also missing an opening bracket on <set $enemy to []>>. I did get an error message upon launching the game when I pasted the markup in initially, but you hadn’t mentioned anything like that, so I assumed it was my copy-paste mistake and fixed it. But I see that it is in fact in your original post, and when I delete that bracket again, if I dismiss the error message I get on launch and run the game anyway, then clicking the link does give me the same error you’ve been getting.
(But even with that fixed, your markup as written wouldn’t work without <<capture>>.)
Don’t be, it happens to the best of us! (It would help if SugarCube had syntax highlighting in the Twine app, but alas, it does not.)
Every time you hit Enter in a Twine passage it’s treated as an HTML line break even if the only thing on that line is markup. If you want to get rid of this universally, put <<set Config.passages.nobr = true;>> in StoryInit (and then put <br> wherever you do want a line break). If you have passages without much markup where you don’t want to have to worry about manually putting <br> in every time, then you can instead use the tag nobr on your markup-heavy passages (and again, put <br> wherever you do want a line break, but only in the nobr-tagged passages).
Also, I’m not sure why you need _o? The whole thing should work as intended with just the addition of <<capture _i>> to the markup in your original post — I tested it.