Switch Case in While Loop runs very slow per Switch item

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
tomlib
Giant
Posts: 195
Joined: Mon Dec 29, 2008 10:48 am

Switch Case in While Loop runs very slow per Switch item

Post by tomlib »

I'm trying to make a random treasure generator that looks at a long list of items of different categories and then loops randomly through them. I've shortened the treasure groups here. There are about 400 separate items right now.

The problem comes in during the Switch statement. If I have 1 Switch group it runs fine but it gets exponentially slower each Switch item added. At 4 items it takes about ten seconds. At six items it takes longer than a man can wait and not starve to death.

I'm getting the length of the array for the dice roll to randomly pick an item. I manually feed in how many times it should loop through the Switch. That doesn't seem to add length. I can loop through ten times or twenty times. The time lag comes only when I add more Switch items. Right now four are listed but I'd obviously like to have more.

The Switch is calling an image from a table for each treasure type. So there is a small table with a 1. Coins Image. 2. Bevarages Image, etc. Then it rolls a die based on the length of the array and picks an item. Finally it adds a row to the output table that lists in the chat box. It's weird because I say I want 20 treasure items but there is only 1 switch case. Boom instant. But if I saw I want 1 treasure item but there are 4 switch cases, ten second wait.

There is also an IF statement near the top if the creature has no treasure. It just shoots out a smaller table.

I feel like I'm doing something stupid with my loops that's causing problems but maybe it's just retrieving information from the Lists takes longer and longer?

Any ideas?

Thanks!

Tom

Code: Select all

[h: gemsJSON = json.fromList("Agate,Agate Geode,Zircon")]
[h: beveragesJSON = json.fromList("Flask of Sparkling Cider,Flask of Spicy Rum,Flask of Egg Nog,Flask of Lemonade")]

[h: coinsJSON = json.fromList("Copper,Silver,Gold,Platinum")]

[h: herbsJSON = json.fromList("Case of Cigarettes,Case of Thick Cigars,Pouch of Oolong Tea,Pouch of White Tea")]

[h: jewelleryJSON = json.fromList("Gold Amulet,Silver Toe Ring,Silver Torc")]

[h: foodJSON = json.fromList("Bag of Black Licorice,Bag of Red Licorice,Sourdough")]

[h: cookingJSON = json.fromList("Fondue Kit,Rolling Pin,Fork,Iron Skillet,Wine Carafe")]

[h: lengthGems = json.length(gemsJSON)]

[h: lengthBeverages = json.length(beveragesJSON)]

[h: lengthCoins = json.length(coinsJSON)]

[h: lengthHerbs = json.length(herbsJSON)]

[h: lengthJewellery = json.length(jewelleryJSON)]

[h: lengthFood = json.length(foodJSON)]

[h: lengthCooking = json.length(cookingJSON)]

[h: me = getSelected()]
[h: switchToken(me)]
[h: booty = getSpeech("Booty",me)]

[h, if (booty=="No"), CODE: {
	[h: treasureFinal = '<table border=1><tr><th>Creature</th></tr><td>The creature has no treasure.</td></tr></table>']
	[h: setSpeech("Booty",treasureFinal)]
};{

[h: rollingDice = input("Dice")]
 [h: abort(rollingDice)]

[h: loopTreasure = Dice]

[h: equipment = getProperty("Equipment",me)]
[h: treasureFinal = '<table border=1><tr><th>Item</th><th>Image</th></tr>']

[h, FOREACH (item,equipment), CODE: {
	[h: treasureLine = '<tr><td>' + item + '</td><td> </td></tr>']
	[h: treasureFinal = treasureFinal + treasureLine]
}]

[h, WHILE(loopTreasure>0), CODE :{

[h: treasureRoll = d4]

[h, switch(treasureRoll), CODE:
case 1: { 
	[h: treasureImage = strformat("<img src='%s' />", tableImage("treasureTypeImages",1,50))]
	[h: roll = roll(lengthGems)]
	[h: item = json.get(gemsJSON,roll-1)]
	[h: treasureFinal = treasureFinal + '<tr><td>' + item + '</td><td>' + treasureImage + '</td></tr>']
};
case 2: { 
	[h: treasureImage = strformat("<img src='%s' />", tableImage("treasureTypeImages",2,50))]
	[h: roll = roll(lengthBeverages)]
	[h: item = json.get(beveragesJSON,roll-1)]
	[h: treasureFinal = treasureFinal + '<tr><td>' + item + '</td><td>' + treasureImage + '</td></tr>']
};
case 3: { 
	[h: treasureImage = strformat("<img src='%s' />", tableImage("treasureTypeImages",3,50))]
	[h: roll = roll(lengthCoins)]
	[h: item = json.get(coinsJSON,roll-1)]
	[h: treasureFinal = treasureFinal + '<tr><td>' + item + '</td><td>' + treasureImage + '</td></tr>']
};
case 4: { 
	[h: treasureImage = strformat("<img src='%s' />", tableImage("treasureTypeImages",4,50))]
	[h: roll = roll(lengthHerbs)]
	[h: item = json.get(herbsJSON,roll-1)]
	[h: treasureFinal = treasureFinal + '<tr><td>' + item + '</td><td>' + treasureImage + '</td></tr>']
};

default: {[h: "test"]} ]

[h: loopTreasure = loopTreasure - 1]

}]

[h: treasureFinal = treasureFinal + '</table>']

[h: setSpeech("Booty",treasureFinal)]

}]

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: Switch Case in While Loop runs very slow per Switch item

Post by wolph42 »

that makes little sense, the switch statement is usually really fast. can you share the campaign file with some instructions, id like to have a look (share e.g. through dropbox)

tomlib
Giant
Posts: 195
Joined: Mon Dec 29, 2008 10:48 am

Re: Switch Case in While Loop runs very slow per Switch item

Post by tomlib »

wolph42 wrote:that makes little sense, the switch statement is usually really fast. can you share the campaign file with some instructions, id like to have a look (share e.g. through dropbox)
Hey, Wolph42,

Thanks for the reply. I think it might have been all the looping. I took the While, Switch out of the wrapping IF and all time issues vanished.

I just called the IF and aborted it if the condition worked. Then ran the rest outside.

I'll load the file up on my OneDrive if you want to take a look to see what was going on.

Library Map > Lib:Character Token > CharacterManipulation Group: Treasure Loop button.

https://1drv.ms/u/s!AkeyHBiLgMBUnhjDHre5f7QkGF03

If you don't have time I understand as the issue is resolved.

Thanks again!

Tom

Code: Select all

[h: gemsJSON = json.fromList("Agate,Agate Geode,Almandine Garnet,Amazonite,Amethyst,Amethyst Geode,Ametrine,Ammolite,Andalusite,Andesine Labradorite,Apatite,Aquamarine,Aventurine,Azurite,Beryl Gems,Black Opal,Bloodstones,Boulder Opal,Carnelian,Cats Eye Apatite,Cats Eye Aquamarine,Chalcedony,Charoite,Chocolate Opal,Chrome Diopside,Chrome Tourmaline,Chrysoberyl,Chrysoberyl Cats Eye,Chrysocolla,Chrysoprase,Citrine,Citrine Geode,Color Change Garnet,Color Change Sapphire,Color-Change Diaspore,Color-Change Fluorite,Color-Change Gems,Coral,Danburite,Demantoid Garnet,Dendritic Agate,Dendritic Chalcedony,Diamond,Druzy Azurite,Dumortierite Quartz,Emerald,Enstatite,Fire Agate,Fire Opal,Fluorite,Fossil Coral,Garnet ,Golden Beryl,Hawks Eye,Hematite,Hemimorphite,Hemimorphite Druzy,Hessonite Garnet,Howlite,Idocrase Gems,Imperial Topaz,Iolite,Jade Gemstones,Jadeite,Jasper,Kornerupine,Kunzite,Kyanite,Labradorite,Lapis Lazuli,Larimar,Malachite,Malaya Garnet,Mali Garnet,Maw-Sit-Sit,Moonstone,Morganite,Mother of Pearl,Mystic Topaz,Obsidian,Opal,Pearl,Peridot,Pietersite,Prehnite,Pyrope Garnet,Quartz,Quartz Cats Eye,Rainbow Moonstone,Rainbow Pyrite,Rhodochrosite,Rhodolite Garnet,Rose Quartz,Rubellite Tourmaline,Ruby,Rutile Quartz,Sapphire,Scapolite,Seraphinite,Serpentine,Sillimanite,Sillimanite Cats Eye,Smithsonite,Smoky Quartz,Sodalite,Spessartite Garnet,Sphalerite,Sphene,Spinel,Star Cabochons,Star Garnet,Star Lemon Quartz,Star Moonstone,Star Rose Quartz,Star Sapphire,Sugilite,Tanzanite,Tigers Eye,Tigers Eye Matrix,Topaz,Tourmaline,Tsavorite Garnet,Turquoise,Uvarovite Garnet Drusy,Variscite,Zircon")]
[h: beveragesJSON = json.fromList("Flask of Sparkling Cider,Flask of Spicy Rum,Flask of Hard Gin,Flask of Dark Whiskey,Flask of Curdled Milk,Flask of Apple Cider,Flask of Stout,Flask of Pilsner,Flask of Rice Wine,Flask of Red Wine,Flask of White Whine,Flask of Grape Juice,Flask of Orange Juice,Flask of Coconut Juice,Flask of Tequila,Flask of Sangria,Flask of Sparkling Wine,Flask of Brandy,Flask of Cognac,Flask of Vodka,Flask of Coffee,Flask of Sweat Tea,Flask of Green Tea,Flask of Black Tea,Flask of White Tea,Flask of Oolong Tea,Flask of Sake,Flask of Chocolate Milke,Flask of Egg Nog,Flask of Lemonade")]
[h: coinsJSON = json.fromList("Copper,Silver,Gold,Platinum")]
[h: herbsJSON = json.fromList("Case of Cigarettes,Case of Thick Cigars,Case of Thin Cigars,Jar of Anise,Jar of Basil,Jar of Caper,Jar of Caraway,Jar of Cardamom,Jar of Cayenne,Jar of Chicory,Jar of Cilantro,Jar of Cinnamon,Jar of Clove,Jar of Coriander,Jar of Cumin,Jar of Dill,Jar of Ginger,Jar of Horseradish,Jar of Juniper Berries,Jar of Lavender,Jar of Lemon Basil,Jar of Mustard,Jar of Nutmeg,Jar of Oregano,Jar of Paprika,Jar of Parsley,Jar of Pepper,Jar of Peppermint,Jar of Rosemary,Jar of Saffron,Jar of Salt,Jar of Sugar,Jar of Vanilla,Pouch of Black Tea,Pouch of Dark Tobacco,Pouch of Green Tea,Pouch of Green Tobacco,Pouch of Oolong Tea,Pouch of White Tea")]
[h: jewelleryJSON = json.fromList("Gold Amulet,Gold Anklet,Gold Armlet,Gold Bangles,Gold Belly Chain,Gold Bracelet,Gold Brooch,Gold Cameo,Gold Choker,Gold Cuff Links,Gold Earring,Gold Fascinators,Gold Hairpins,Gold Hatpin,Gold Locket,Gold Medallion,Gold Necklace,Gold Pendant,Gold Pin,Gold Prayer Beads,Gold Ring,Gold Sarpech,Gold Toe Ring,Gold Torc,Silver Amulet,Silver Anklet,Silver Armlet,Silver Bangles,Silver Belly Chain,Silver Bracelet,Silver Brooch,Silver Cameo,Silver Choker,Silver Cuff Links,Silver Earring,Silver Fascinators,Silver Hairpins,Silver Hatpin,Silver Locket,Silver Medallion,Silver Necklace,Silver Pendant,Silver Pin,Silver Prayer Beads,Silver Ring,Silver Sarpech,Silver Toe Ring,Silver Torc")]
[h: foodJSON = json.fromList("Bag of Black Licorice,Bag of Red Licorice,Bag of Peanuts,Pizza Cheese,Pizza Pepperoni,Pizza Sausage,Pizza Veggie,Pizza Extra Sauce,Pizza Pepperoni Vanquisher,Pizza Stuffed,Balony Stick,Cheese Guoda,Cheese Swiss,Cheese Parmesan Grated,Rotten Tomatoes,Cheese Ricotta,Cheese Limburger,Cheese Gorgonzola,Cheese Feta,Cheees Mozzarella,Bacon Rasher,Mortadella Log,Blood Pudding Tin,Haggis,Monkey Brains,Turkey Leg,Bag of Beef Jerky,Slices of Pastrami,Fruit cake,Honey Pie,Peanut Butter,Rice,Rye,Frenchbread,Bagel,Baguette,Potato Breads,Sourdough")]
[h: cookingJSON = json.fromList("Fondue Kit,Rolling Pin,Fork,Spoon,Spork,Salad Fork,Cheese Grater,Iron Skillet,Wine Carafe")]
[h: lengthGems = json.length(gemsJSON)]
[h: lengthBeverages = json.length(beveragesJSON)]
[h: lengthCoins = json.length(coinsJSON)]
[h: lengthHerbs = json.length(herbsJSON)]
[h: lengthJewellery = json.length(jewelleryJSON)]
[h: lengthFood = json.length(foodJSON)]
[h: lengthCooking = json.length(cookingJSON)]


[h: me = getSelected()]
[h: switchToken(me)]
[h: booty = getSpeech("Booty",me)]

[h, if (booty=="No"), CODE: {
	[h: treasureFinal = '<table border=1><tr><th>Creature</th></tr><td>The creature has no treasure.</td></tr></table>']
	[h: setSpeech("Booty",treasureFinal)]
	[h: abort(0)]
}]

[h: rollingDice = input("Dice")]
 [h: abort(rollingDice)]

[h: loopTreasure = Dice]

[h: equipment = getProperty("Equipment",me)]
[h: treasureFinal = '<table border=1><tr><th>Item</th><th>Image</th></tr>']

[h, FOREACH (item,equipment), CODE: {
	[h: treasureLine = '<tr><td>' + item + '</td><td> </td></tr>']
	[h: treasureFinal = treasureFinal + treasureLine]
}]

[h, WHILE(loopTreasure>0), CODE :{

[h: treasureRoll = d7]

[h, switch(treasureRoll), CODE:
case 1: { 
	[h: treasureImage = strformat("<img src='%s' />", tableImage("treasureTypeImages",1,50))]
	[h: roll = roll(lengthGems)]
	[h: item = json.get(gemsJSON,roll-1)]
	[h: treasureFinal = treasureFinal + '<tr><td>' + item + '</td><td>' + treasureImage + '</td></tr>']
	[h: loopTreasure = loopTreasure - 1]
};
case 2: { 
	[h: treasureImage = strformat("<img src='%s' />", tableImage("treasureTypeImages",2,50))]
	[h: roll = roll(lengthBeverages)]
	[h: item = json.get(beveragesJSON,roll-1)]
	[h: treasureFinal = treasureFinal + '<tr><td>' + item + '</td><td>' + treasureImage + '</td></tr>']
	[h: loopTreasure = loopTreasure - 1]
};
case 3: { 
	[h: treasureImage = strformat("<img src='%s' />", tableImage("treasureTypeImages",3,50))]
	[h: roll = roll(lengthCoins)]
	[h: item = json.get(coinsJSON,roll-1)]
	[h: treasureFinal = treasureFinal + '<tr><td>' + item + '</td><td>' + treasureImage + '</td></tr>']
	[h: loopTreasure = loopTreasure - 1]
};
case 4: { 
	[h: treasureImage = strformat("<img src='%s' />", tableImage("treasureTypeImages",4,50))]
	[h: roll = roll(lengthHerbs)]
	[h: item = json.get(herbsJSON,roll-1)]
	[h: treasureFinal = treasureFinal + '<tr><td>' + item + '</td><td>' + treasureImage + '</td></tr>']
	[h: loopTreasure = loopTreasure - 1]
};
case 5: { 
	[h: treasureImage = strformat("<img src='%s' />", tableImage("treasureTypeImages",5,50))]
	[h: roll = roll(lengthJewellery)]
	[h: item = json.get(jewelleryJSON,roll-1)]
	[h: treasureFinal = treasureFinal + '<tr><td>' + item + '</td><td>' + treasureImage + '</td></tr>']
	[h: loopTreasure = loopTreasure - 1]
};
case 6: { 
	[h: treasureImage = strformat("<img src='%s' />", tableImage("treasureTypeImages",6,50))]
	[h: roll = roll(lengthFood)]
	[h: item = json.get(foodJSON,roll-1)]
	[h: treasureFinal = treasureFinal + '<tr><td>' + item + '</td><td>' + treasureImage + '</td></tr>']
	[h: loopTreasure = loopTreasure - 1]
};
case 7: { 
	[h: treasureImage = strformat("<img src='%s' />", tableImage("treasureTypeImages",7,50))]
	[h: roll = roll(lengthCooking)]
	[h: item = json.get(cookingJSON,roll-1)]
	[h: treasureFinal = treasureFinal + '<tr><td>' + item + '</td><td>' + treasureImage + '</td></tr>']
	[h: loopTreasure = loopTreasure - 1]
};

default: {[h: "test"]} ] 

}] 

[h: treasureFinal = treasureFinal + '</table>']

[h: setSpeech("Booty",treasureFinal)]

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: Switch Case in While Loop runs very slow per Switch item

Post by wolph42 »

i had a short look at the fiel you sent me and ran the macro you suggested, however (except an trusted error report that was easily fixed) nothing happened. The macro ran instantly without any output. So there was little for me to check or test.
the only thing i can say is that you use too little indentation, making your code unreadable and hard to debug.

Post Reply

Return to “Macros”