Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 1

Role Game Effect/Potion Generator in Swift

First of all, excuse me for my (sometimes) bad English, I'm french. I'm actually working on a role game project. The goal is to create a little macOS software (a generator) using Swift and Xcode that can create a potion (an effect associated with a potency) (like : "Fortify Speed 7/10") from multiple parameters. The app allows the user to enter two parameters : ingredients (selected in pop-up menus) and a dice roll (put in a text field). If those two (or more) ingredients have an effect in common, the soft makes an addition of the value of both effects and return this : ""effect in common" + (value of the 1st ingredient effect + value of 2nd ingredient effect)". The result can change during the operation depending on the dice roll, but it doesn't matter here. Here is an exemple to understand what I'm saying :


1st ingredient : "Cat's Claw Bark" -> Resist Lightning (4/10); Resist Psychic (3/10); Acid Damage (3/10); Fear (3/10); Invisibility (1/10). || 2nd ingredient : "Grape Holly" -> Cure Disease (5/10); Resist Cold (4/10); Heighten Medicine (4/10; Resist Lightning (3/10); Force Damage (2/10.


Because both ingredients have an effect in common (Resist Lightning), the soft returns that effect with an addition of the potency of the two effects. So it returns : "Resist Lightning (7/10)"


Here is my first question : How can I convert this into Swift code ? I've tried many ways but nothing useful. I tried to use arrays, dictionaries, arrays into dictionaries, but I'm still facing the same problem : How can I attribute multiple parameters (effects in this case) and types (String for the name of the effect and Int for its potency) to an instance ? (If it's the right question). My second question is : How can I compare keys (not values) of a dictionary to match a result ?


Maybe like this, using a dictionary, but I'll need to display in the result not only the value of the key in the dictionary, but the key itself. Also, I'll need to compare the keys, not the values :


 let grapeHolly = ["Cure Disease": 5, "Resist Cold": 4, "Heighten Medicine": 4, "Resist Lightning": 3, "Force Damage": 2]


And by doing that I get a "Cannot use instance member 'grapeHolly' within property initializer" error. And if I put that line inside the body of a 'viewDidLoad()' function, it isn't recognize by the code that is not inside the function.


Capture d’écran 2018-11-28 à 16.11.34.pngThe UI of the app

To be clear and comprehensible, I want that when the user selects two or more ingredients from the second pop-ups menu (the first line of pop-ups are made to categorize ingredients into three categories "Common/Uncommon/Rare"), and when he push the "Make a potion" button, the result text field displays the result of the combination of both effects values, if they have an effect in common of course.


Tell me if you need me to be more precise, I'll be very happy to find help :)


Here is the code (don't care about the length of the ingredients list) :


import Cocoa

class ViewController: NSViewController {

@IBOutlet weak var ingredientRarityNorthlands: NSPopUpButton!
@IBOutlet weak var ingredientListNorthlands: NSPopUpButton!

    @IBAction func ingredientRarityNorthlands(_ sender: NSPopUpButton) {
    if ingredientRarityNorthlands.titleOfSelectedItem == "Common" {
        ingredientListNorthlands.addItems(withTitles: ingredientListNorthlandsCommon)
    if  ingredientRarityNorthlands.titleOfSelectedItem == "Uncommon" {
        ingredientListNorthlands.addItems(withTitles: ingredientListNorthlandsUncommon)
    if ingredientRarityNorthlands.titleOfSelectedItem == "Rare" {
        ingredientListNorthlands.addItems(withTitles: ingredientListNorthlandsRare)

let ingredientListNorthlandsCommon = ["Black Pepper", "Bolean Violet", "Brittlebush Flower", "Brittlebush Resin", "Butterfly Moss", "Cardamom", "Cinnamon",  "Cloves", "Colic Yellowcress", "Comb Mint", "Coriander (Leaves)", "Coriander (Seeds)", "Cork Tree Bark", "Crow Hood", "Cumin (Seeds)", "Desert Shrub", "Eucalyptus (Oil)", "Eucalyptus (Gum)", "Eucalyptus (Leaves)", "Eucalyptus (Seeds)", "Fire Itchweed", "Four-Leafed Colwort", "Frukmoot", "Ginger (Root)", "Jasmine (Flower)", "Life's Collard", "Lotus (Petals)", "Lotus (Seeds)", "Mango (Leaves)", "Mango (Taproot)", "Marigold (Flower)", "Marigold (Leaves)", "Mustard (Flower)", "Mustard (Greens)", "Mustard (Seeds)", "Nutmeg (Oil)", "Nutmeg (Seed)", "Radiant Azolla", "Red Chili (Seeds)", "Wall Snakeberry"]
let ingredientListNorthlandsUncommon = ["Aloe Vera", "Ash Gallberry", "Baby Zinnia", "Black Salt", "Buzzard Bile", "Corea Mint", "Dead Sage", "Dog Tongue", "Fennec Fox Droppings", "Giant Lizard Venom", "Marsupial Wolf Blood", "Noxious Laurel", "Ocort", "Orange Thyme", "Pale Penny Gilliflower", "Phoenix Briar", "Pocan Hemp", "Prickly Cane", "Serpent Rye", "Shivering Laurel", "Snage", "Two-Tongued Scorpion Tail", "Viper Venom"]
let ingredientListNorthlandsRare = ["Blackheart Flower", "Burrowing Vee Spider", "Jee Redzin Scorpion Eggs", "Kreetlee Leaves", "Olaart Buds", "Smouse Oil", "Wretch Salts", "Zettin Seeds"]

    @IBAction func makeAPotion(_ sender: NSButton) {
    if potionButton.isEnabled {
        resultField.stringValue = result()


func result() -> String {
    return "\(effect) \(value)"


Ps: I think it's not necessary but I put here the dice roll system :


var firstEffect = 10..<12
var secondEffect = 12..<15
var thirdEffect = 15..<20
var fourthEffect = 20..<25
var fifthEffect = 25...

var firstEffectEnabler: Bool = false
var secondEffectEnabler: Bool = false
var thirdEffectEnabler: Bool = false
var fourthEffectEnabler: Bool = false
var fifthEffectEnabler: Bool = false

@IBAction func diceRoll(_ sender: NSTextField) {
    if diceRoll.stringValue == "\(firstEffect)" {
        firstEffectEnabler = true
    if diceRoll.stringValue == "\(secondEffect)" {
        secondEffectEnabler = true
    if diceRoll.stringValue == "\(thirdEffect)" {
        thirdEffectEnabler = true
    if diceRoll.stringValue == "\(fourthEffect)" {
        fourthEffectEnabler = true
    if diceRoll.stringValue == "\(fifthEffect)" {
        fifthEffectEnabler = true