Help please - how do I set random links sending a list of variables

Hello,
I am new to Twine and working on my first project, a job-simulation game. For this I am trying to set up a list of job offers which is randomly created from a multidimensional array of 20 different jobs:

<<set $job to [
["Teacher", "School", "Public Service", 3000],
...
]
<<set $num = random(2,5)>>\
<<set $i = 0>>\
<<set _list to []>>

<<for $i is 0; $i <($num); $i+=1>>\
<<set $choice to random (0, 19)>>\ 
<<set $jobChoice to $job[$choice]>>\ 
<<set _list.push($jobChoice)>> \
<<link " _list[$i][0]" "Job-Auswahl">>\
<<set $jobTitle to list[$i][0]>>\
<</link>>\
<<print  _list[$i][1] +  ", " +   _list[$i][2]  + ", " +  _list[$i][3] + " $ salary" >>\
<</for>>

This is just one of the different variants I tried out.

I want the job information to be sent to the next passage but for the life of me I just can’t make it work. I’ve been trying on my own for the last four days and I am literally crying - even ChatGPT couldn’t help me.

I am using Twine 2.7.1 and SugarCube 2.36.1 - any help would be most appriciated

Yeah… unfortunately ChatGPT will not help with Twine code. It will usually give code so wrong, you’d need to learn the format from scratch to untangle all the issues…

Could you paste the whole array of $job?

For the looping, you don’t need set the $i, a temporary variable should work

<<for _1 to 0; _1 < $num; _1++>>
   Your code...
<</for>>

(you could even make $num into a temporary variable… _num)

That looks pretty complex otherside for a first Twine games, so good luck :stuck_out_tongue:

Here is the array:

<<set $job to [
	["Verwaltungsfachangestellte(m/w/d)"," Mittlerer Dienst", "Verwaltung", 1400 ],
    ["Fachangestellte fĂĽr Medien- und Informationsdienste"," Mittlerer Dienst", "Kultur", 1400],
    ["Fremdsprachenkorrespondent/-in"," Mittlerer Dienst", "Kultur", 1450],
    ["Fachinformatiker (m/w/d)"," Mittlerer Dienst", "Technik", 1500],
    ["Rechtsanwaltsgehilfe (m/w/d)","Mittlerer Dienst", "Verwaltung", 1400],
    ["Vorzimmerdame"," Mittlerer Dienst", "Verwaltung", 1200],
    ["Sicherheitskraft (m/w/d)"," Einfacher Dienst", "Verwaltung", 1000],
    ["Techniker (m/w/d)"," Mittlerer Dienst", "Technik", 1620],
    ["Kurierfahrer (m/w/d)"," Einfacher Dienst", "Verwaltung", 1100],
	["Sachbearbeiter (m/w/d)"," Gehobener Dienst", "Verwaltung", 2300],
    ["Programmierer (m/w/d)","Gehobener Dienst", "Technik", 2850],
    ["Techniker (m/w/d)"," Gehobener Dienst", "Technik", 2900],
    ["Ăśbersetzer/-in (m/w/d)"," Gehobener Dienst", "Kultur", 2500],
    ["Personalsachbearbeiter (m/w/d)"," Gehobener Dienst", "Verwaltung", 2400],
    ["Politikwissenschaftler/-in (m/w/d)"," Gehobener Dienst", "Kultur", 2500],
    ["Ăśbersetzer/-in (m/w/d)"," Gehobener Dienst", "Kultur", 2500],
    ["Bibliothekar/-in (m/w/d)"," Gehobener Dienst", "Kultur", 2500],
    ["Programmierer (m/w/d)","Höherer Dienst", "Technik", 4250],
  	["Referent/-in (m/w/d)","Höherer Dienst", "Verwaltung", 3250],
    ["Referent/-in (m/w/d)","Höherer Dienst", "Kultur", 3250],
    ["Dolmetscher/-in (m/w/d)","Höherer Dienst", "Kultur", 3450],
    ["Personalreferent/-in (m/w/d)","Höherer Dienst", "Verwaltung", 3250]
]>>\

I wrote the example in english, but I’m programming in german, that’s why I didn’t post it .
ChatGPT helped me write a solution without the random number of job offers and it seems to work. Probably gonna stick with this for now:

<<set $num = 3>> \
<<set $i = 0>>\
<<set _list to []>>

<<set _choices = []>>\
<<set $choice1 to random(0, $job.length - 1)>>\
<<set $choice2 to random(0, $job.length - 1)>>\
<<set $choice3 to random(0, $job.length - 1)>>\
<<set _choices.push($job[$choice1])>>\
<<set _choices.push($job[$choice2])>>\
<<set _choices.push($job[$choice3])>>\
<<set $jobTitle1 to _choices[0][0]>>\
<<set $laufbahn1 to _choices[0][1]>>\
<<set $fachrichtung1 to _choices[0][2]>>\
<<set $salary1 to _choices[0][3]>>\
<<set $jobTitle2 to _choices[1][0]>>\
<<set $laufbahn2 to _choices[1][1]>>\
<<set $fachrichtung2 to _choices[1][2]>>\
<<set $salary2 to _choices[1][3]>>\
<<set $jobTitle3 to _choices[2][0]>>\
<<set $laufbahn3 to _choices[2][1]>>\
<<set $fachrichtung3 to _choices[2][2]>>\
<<set $salary3 to _choices[2][3]>>\
<<link "$jobTitle1">>
  <<set $jobTitle to $jobTitle1>>\
  <<set $laufbahn to $laufbahn1>>\
  <<set $fachrichtung to $fachrichtung1>>\
  <<set $salary to $salary1>>\
  <<goto "Job-Auswahl">>
<</link>>
<<print $laufbahn1 +", " + $fachrichtung1 +", " + $salary1 + " € Startgehalt">><br>

<<link "$jobTitle2">>
  <<set $jobTitle to $jobTitle2>>\
  <<set $laufbahn to $laufbahn2>>\
  <<set $fachrichtung to $fachrichtung2>>\
  <<set $salary to $salary2>>\
  <<goto "Job-Auswahl">>\
<</link>>\

<<print $laufbahn2 +", " + $fachrichtung2 +", " + $salary2 + " € Startgehalt">><br>

<<link "$jobTitle3">>\
  <<set $jobTitle to $jobTitle3>>\
  <<set $laufbahn to $laufbahn3>>\
  <<set $fachrichtung to $fachrichtung3>>\
  <<set $salary to $salary3>>\
  <<goto "Job-Auswahl">>\
<</link>>\

<<print $laufbahn3 +", " + $fachrichtung3 +", " + $salary3 + " € Startgehalt">><br>

It’s not elegant but as long as it does the job :slight_smile:

Okay, there are number of ways this could be improved, most especially the fact that there’s no need to break down the information you pass to the next passage into 3+ individual variables. A Story variable can contain an object or an array (as you do when setting $jobs in the first place) so you can just pass the entire job choice.

I’m also going to assume you are offering the player a choice of 3 jobs, so you want the picked job to be in the same variable regardless of which one is picked.

Finally, you can use <<nobr>> ... <</nobr>> to collapse whitespace across many lines, and save your self the use of dozens of \

<<nobr>>
<<set $jobs to [
	["Verwaltungsfachangestellte(m/w/d)"," Mittlerer Dienst", "Verwaltung", 1400 ],
    ["Fachangestellte fĂĽr Medien- und Informationsdienste"," Mittlerer Dienst", "Kultur", 1400],
    ["Fremdsprachenkorrespondent/-in"," Mittlerer Dienst", "Kultur", 1450],
    ["Fachinformatiker (m/w/d)"," Mittlerer Dienst", "Technik", 1500],
    ["Rechtsanwaltsgehilfe (m/w/d)","Mittlerer Dienst", "Verwaltung", 1400],
    ["Vorzimmerdame"," Mittlerer Dienst", "Verwaltung", 1200],
    ["Sicherheitskraft (m/w/d)"," Einfacher Dienst", "Verwaltung", 1000],
    ["Techniker (m/w/d)"," Mittlerer Dienst", "Technik", 1620],
    ["Kurierfahrer (m/w/d)"," Einfacher Dienst", "Verwaltung", 1100],
	["Sachbearbeiter (m/w/d)"," Gehobener Dienst", "Verwaltung", 2300],
    ["Programmierer (m/w/d)","Gehobener Dienst", "Technik", 2850],
    ["Techniker (m/w/d)"," Gehobener Dienst", "Technik", 2900],
    ["Ăśbersetzer/-in (m/w/d)"," Gehobener Dienst", "Kultur", 2500],
    ["Personalsachbearbeiter (m/w/d)"," Gehobener Dienst", "Verwaltung", 2400],
    ["Politikwissenschaftler/-in (m/w/d)"," Gehobener Dienst", "Kultur", 2500],
    ["Ăśbersetzer/-in (m/w/d)"," Gehobener Dienst", "Kultur", 2500],
    ["Bibliothekar/-in (m/w/d)"," Gehobener Dienst", "Kultur", 2500],
    ["Programmierer (m/w/d)","Höherer Dienst", "Technik", 4250],
  	["Referent/-in (m/w/d)","Höherer Dienst", "Verwaltung", 3250],
    ["Referent/-in (m/w/d)","Höherer Dienst", "Kultur", 3250],
    ["Dolmetscher/-in (m/w/d)","Höherer Dienst", "Kultur", 3450],
    ["Personalreferent/-in (m/w/d)","Höherer Dienst", "Verwaltung", 3250]
]>>
<<set _choices = $jobs.pluckMany(3)>>
<</nobr>>
<<for _choice range _choices>>
   <<link [[_choice[0]]]>><<set $job = _choice>><</link>>

   <<print _choice.join(', ')>>

<</for>>

Note that I’ve renamed the list of jobs to $jobs so that the story variable $job can be used to hold the job you pick.

I’ve also used the pluckMany() method that SugarCube provides to do your random selection of 3 jobs.

One final note, you might find it easier if each job was an object rather than an array, so that you didn’t have to keep the positions of the information in mind to use them. i.e. most people find this easier to work with:

{ job: "Teacher", location: "School", field: "Public Service", salary: 3000 }

Than this:

["Teacher", "School", "Public Service", 3000]

Because it’s much easier to know what $job.salary means than $job[3]

1 Like