Loop inside a link?

Hello everyone, and happy new year!

I’m offering the player to accept lists of names from some languages. For latine names I got the trouble there are very few first names, so I thought to add the option to merge first names and gents names in order to create a greater list of names.

In order to minimize the story variables I’m currently trying to create a loop inside a <<link>><</link>> macro but it doesn’t seem to work. I wonder if it’s doable, but in the event it is, what I’ve gathered up to now is that the loop itsef seems to work if not inside the aforementionned macro. Also I’m positive an <<if>><</if>> macro inside a <<link>><</link>> macro does work. So the main question seems like is it ok to place a <<for>><</for>> macro inside a <<link>><</link>>. I’d rather not make my temporary variable into a story one if possible, because in this case there wouldn’t be much need of an help.

What I’ve got so far.

StoryInit:

<<set $NomM_la to ["Agrippa","Ancus","Appius","Attius","Aulus","Caius","Caeso","Cnaeus","Decimus","Faustus","Gaius","Gnaeus","Hostus","Kaeso","Lucius","Mamercus","Manius","Marcus","Mettius","Nonus","Numa","Numerius","Octavius","Opiter","Paullus","Postumus","Primus","Proculus","Publius","Quartus","Quintus","Septimus","Sertor","Servius","Secundus","Sextus","Spurius","Statius","Tertius","Tiberius","Titus","Tullus","Vibius","Volero","Volesus","Vopiscus"]>>
<<set $NomG_la to ["Acili","Aeli","Aemili","Afrani","Anici","Anni","Antisti","Antoni","Appulei","Aquilli","Arri","Arrunti","Asini","Atili","Aufidi","Aureli","Baebia","Caecili","Caeli","Calpurni","Calvisi","Canini","Carvili","Cassi","Cati","Ceioni","Claudi","Cloeli","Coccei","Coeli","Comini","Corneli","Coruncani","Curi","Curti","Deci","Didi","Domiti","Duili","Fabi","Fanni","Flamini","Flavi","Fontei","Fulvi","Furi","Gabini","Gegani","Gelli","Genuci","Helvi","Herenni","Hermini","Horati","Hortensi","Hostili","Icili","Juli","Juni","Juventi","Laeli","Larci","Licini","Livi","Lucreti","Lutati","Maeni","Mamili","Manilius","Manli","Marci","Mari","Memmi","Meneni","Metili","Minici","Minuci","Muci","Mummi","Nauti","Noni","Norban","Nummi","Octavi","Oppi","Otacili","Papiri","Petroni","Pinari","Plauti","Poeteli","Pompei","Pomponi","Ponti","Popilli","Porci","Postumi","Quincti","Sallusti","Scriboni","Semproni","Septimi","Sergi","Servae","Servili","Sexti","Sicini","Sosi","Statili","Sulpici","Tarquini","Terenti","Titi","Ulpi","Valeri","Vergini","Veturi","Vibi","Vibulli","Viri","Vitelli","Volusi"]>>

Another passage ($Peuple_langue is set in the previous passage)

<<switch $Peuple_langue>><<case "latine">><<set $NomM to $NomM_la>><</switch>>
[...]
Votre peuple emploie la langue $Peuple_langue.<br>
<<if $Peuple_langue is "latine">><hr>Les prénoms latins sont très peu nombreux. Souhaitez-vous que les noms gentilices soient également utilisés pour dénommer les membres de votre peuple ?<table><tr><td><label><<radiobutton "_gentilices" "ajouté" checked>> Oui, je veux ajouter les noms gentilices aux prénoms.</label></td></tr>
<tr><td><label><<radiobutton "_gentilices" "rejeté">> Non, je ne veux pas ajouter les noms gentilices aux prénoms.</label></td></tr></table><</if>>
[...]
<<link "Mon choix est fait." "Votre peuple - structure sociale">><<if _gentilices is "ajouté">><<for _i to 0 ; _i < $NomG_la.length ; _i++>><<set $NomM.pushUnique($NomG_la[_i] + "us")>><<set $NomM.sort()>><</for>><</if>><</link>>

Any help would be greatly appreciated.

S

If I understand it correctly, you want to add the gents names (with an ending) to the list of first names? If that is so, you should change

<<set $NomM.pushUnique($NomG_la[_i] + "us")>>

to
<<set $NomM_la.pushUnique($NomG_la[_i] + "us")>>

because right now the for loop is trying to push things into an array which does not exist (unless you didn’t show us the entire contents of your StoryInit passage).

I tested this and it worked for me - the first names array was expanded and sorted.

1 Like

You’re perfectly right, Agnieszka, I’ve omitted a part of the code, the one where I create $NomM from $NomM_la. I add it in top post.

Okay, in this case, your code works for me as is – I copied it and in the passage it leads to, I put

<<for _i to 0; _i < $NomM.length; _i++>>\
$NomM[_i] 
<</for>>\

which prints out the extended list of names.

The only issue I had was that it expanded the $NomM_la array as well, but this can be fixed by using clone:

<<switch $Peuple_langue>><<case "latine">><<set $NomM to clone($NomM_la)>><</switch>>

EDIT: Oh, one more thing – since you’re not modifying the arrays from StoryInit and want to reduce the amount of data stored in story variables, you can change
$NomM_la to setup.NomM_la and $NomG_la to setup.NomG_la everywhere :slight_smile:

1 Like

I’m puzzled at why it did not work at first, maybe an ‘F5’ problem?

However many thanks to you. And thank you for the kind suggestion to cut the number of story variables.

1 Like