New to this, better ways to do this?

Twine 2.3.5 and Sugar Cube 2.30.0

So this is kinda what I want, however its clearly a really long and inefficient way to do it.

<<set $chance to random(1,5)>>
		<<if $chance == 1>>
			<<set $UsedSlot1 = "Nissan SKYLINE GT-R (R32) '89">>
				<<set $chance to random(1,5)>>
					<<if $chance == 1>>
						<<set $UsedSlot1Colour = "Gun Gray Metallic">>
					<<elseif $chance == 2>>
						<<set $UsedSlot1Colour = "Black Pearl Metallic">>
					<<elseif $chance == 3>>
						<<set $UsedSlot1Colour = "Red Pearl Metallic">>
					<<elseif $chance == 4>>
						<<set $UsedSlot1Colour = "Jet Silver Metallic">>
					<<elseif $chance == 5>>
						<<set $UsedSlot1Colour = "Dark Blue Pearl">>
					<</if>>
		<<elseif $chance == 2>>
			<<set $UsedSlot1 = "Toyota SUPRA RZ '97">>
				<<set $chance to random(1,8)>>
					<<if $chance == 1>>
						<<set $UsedSlot1Colour = "Super White II">>
					<<elseif $chance == 2>>
						<<set $UsedSlot1Colour = "Silver Metallic Graphite">>
					<<elseif $chance == 3>>
						<<set $UsedSlot1Colour = "Grayish Green Mica Metallic">>
					<<elseif $chance == 4>>
						<<set $UsedSlot1Colour = "Black">>
					<<elseif $chance == 5>>
						<<set $UsedSlot1Colour = "Super Red IV">>
					<<elseif $chance == 6>>
						<<set $UsedSlot1Colour = "Super Bright Yellow">>
					<<elseif $chance == 7>>
						<<set $UsedSlot1Colour = "Blue Mica Metallic">>
					<<elseif $chance == 8>>
						<<set $UsedSlot1Colour = "Peridot Pearl Mica">>
					<</if>>
		<<elseif $chance == 3>>
			<<set $UsedSlot1 = "Toyota SPRINTER TRUENO GT-APEX (AE86) '83">>
				<<set $chance to random(1,2)>>
					<<if $chance == 1>>
						<<set $UsedSlot1Colour = "High-Tech Two-Tone">>
					<<elseif $chance == 2>>
						<<set $UsedSlot1Colour = "High-Flash Two-Tone">>
					<</if>> 
		<<elseif $chance == 4>>
			<<set $UsedSlot1 = "Acura INTEGRA TYPE R '01">>
		<<elseif $chance == 5>>
			<<set $UsedSlot1 = "Honda BALLADE SPORTS CR-X 1.5i '83">>
		<</if>>

It’s a used car dealership that gets a new lot of cars everyday and each one should have its own colour possibly other attributes, I want to be able to make a lot profiles for each car on their possible colours, amount of colours etc but I’m unsure how I could do this and then refer to it.
Any help or opinions would be greatly appreciated :slight_smile:

Sorry about the clump of code, not sure how to keep the tab spaces.

2 Likes

I edited your post and changed that block from a quote to code ( </> in the formatting bar) to preserve your tabs and spaces.

2 Likes

Ah thank you :slight_smile:

1 Like

You could collapse one layer of <<if>> statements by putting the colours in a temporary array:

<<set _colours = ["Gun Gray Metallic", "Black Pearl Metallic", "Red Pearl Metallic", "Jet Silver Metallic", "Dark Blue Pearl"]>>
<<set $UsedSlotColour1 to _colours[random(_colours.length-1)]>>

This is just a first step: you could certainly get fancier and make up a data structure with all the cars and all their options, and write code to randomly choose a car and all its options. Don’t know how far you want to go with that (I would go quite a ways, but I’m a programmer, so…).

It might also be worth making the chosen car an obect and storing all its parts in a single variable, like:

<<set $UsedSlot1 to {
	name: "Nissan SKYLINE GT-R (R32) '89",
	colour: "Gun Gray Metallic"
}>>

And then access the parts with $UsedSlot1.name or $UsedSlot1.colour.

2 Likes

Thank you! I think this is exactly what I was looking for, and I think I’ll have a look at the variable for each car as well so thanks for adding that too :smiley:

1 Like

You can actually simplify this even more:

<<set $UsedSlotColour1 = either("Gun Gray Metallic", "Black Pearl Metallic", "Red Pearl Metallic", "Jet Silver Metallic", "Dark Blue Pearl")>>
2 Likes