1

First of all here's my previous question so you see what I'm trying to make.

We are trying to make a Small game. We made a sort of monster fighting game. We made items to the game but I want them to drop by the monster. made different types and would like to know how to code to get a Drop Chance on the Items... like

So now I know how that works I'm stuck to get a random item from my list.

So what I actually want is to get a Random item of my "NormalType" list when i print it..

protocol NormalType {
    var name: String { get }
}

class knife: NormalType {
    let name = "Knife"
    let Str = 10
}
class sword: NormalType {
    let name = "Sword"
    let Str = 20
}
class katana: NormalType {
    let name = "Katana"
    let Str = 30
}


class RareType {
    class Knife: RareType {
        var Str = 10
        var Hp = 10
    }
    class sword: RareType {
        var Str = 20
        var HP = 15
    }
    class Katana: RareType {
        var Str = 30
        var Hp = 20
    }
}

class LegendaryType {
    class Knife: LegendaryType {
        var Str = 10
    }
    class sword: LegendaryType {
        var Str = 20
    }
    class Katana: LegendaryType {
        var Str = 30
    }
}
var Knife = knife()
var Sword = sword()
var Katana = katana()

var Items: [NormalType] = [Knife, Sword, Katana]
var randomnumber = (arc4random_uniform(2))

print(Items[randomnumber])
3
  • I suggest you have a look on my quite detailed answer to you previous question: it showed how (after presumably slaying a monster) you could call the .dropItem() method of class Monster, and it would return, randomly, one of the cases of a an enum holding your different rarities. You should be able to generalize this method to drop not only a random rarity, but also a random item. See stackoverflow.com/questions/34645338/drop-chance-in-swift/… Commented Jan 7, 2016 at 23:45
  • Might try it, but i'm kinda starting with all this so... your answer whas kinda hard to understand for me so i whas hoping on a easyer answer :P just started with this all 10 days ago.. :) Commented Jan 7, 2016 at 23:51
  • I see :) Anyway, I added also item types to the example in the previous thread (for say, inspiration), but I saw you got what you needed for this question. Good luck with your game! Commented Jan 8, 2016 at 0:08

1 Answer 1

1

So you made a little mistake here's the code:

You have forgot to convert to Int the : (arc4random_uniform(2))

import UIKit

protocol NormalType {
    var name: String { get }
}

class knife: NormalType {
    let name = "Knife"
    let Str = 10
}
class sword: NormalType {
    let name = "Sword"
    let Str = 20
}
class katana: NormalType {
    let name = "Katana"
    let Str = 30
}



class RareType {
    class Knife: RareType {
        var Str = 10
        var Hp = 10
    }
    class sword: RareType {
        var Str = 20
        var HP = 15
    }
    class Katana: RareType {
        var Str = 30
        var Hp = 20
    }

}

class LegendaryType {
    class Knife: LegendaryType {
        var Str = 10
    }
    class sword: LegendaryType {
        var Str = 20
    }
    class Katana: LegendaryType {
        var Str = 30
    }

}
var Knife = knife()
var Sword = sword()
var Katana = katana()

var Items: [NormalType] = [Knife, Sword, Katana]
var randomnumber =   Int(arc4random_uniform(2))



print(Items[randomnumber])
Sign up to request clarification or add additional context in comments.

1 Comment

got my first working test in playground! thanks alot! iswift.org/playground?KbK28M

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.