• Just a reminder that providing specifics on, sharing links to, or naming websites where ROMs can be accessed is against the rules. If your post has any of this information it will be removed.
  • Our friends from the Johto Times are hosting a favorite Pokémon poll - and we'd love for you to participate! Click here for information on how to vote for your favorites!
  • Akari, Selene, Mint, Solana - which Pokémon protagonist is your favorite? Let us know by voting in our poll!
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

[Scripting Question] Give Randomized Items?

PunkPhantom

"midna's a cat" -vinny, vinesauce
  • 77
    Posts
    11
    Years
    • Age 30
    • Seen Nov 20, 2016
    I'm trying to make something in my game that's similar to Pokestops in Pokemon Go, mainly they will give the player 3 or 4 random items a day or twice a day.

    What I don't know is how to generate random items. I'm sorry if this is already on the forum/wiki. How do I give the player a random item chosen from a specific selection of items?
     
    Last edited by a moderator:
    Code:
      def extractRandomItems
        maxItems=600 #Change 600 to your Item list maximum
        itemsArray=[]
        for i in 1...maxItems+1
          itemsArray[i]=i
        end
        items=[]
        loop do
          tempItem=itemsArray[rand(itemsArray.length)+1]
          items.push(tempItem)
          itemsArray.delete(tempItem)
          break if items.length==4
        end
        return items
      end
    If you call this function, it will returns items array. To store each item, you have to refer as follow:

    Code:
    item1=extractRandomItems[0]
    item2=extractRandomItems[1]
    item3=extractRandomItems[2]
    item4=extractRandomItems[3]
     
    Last edited:
    TheMaster's answer is very useful and complete. Just to add in the discussion if you need to give an item based on a specific default list of items you could do something like this:

    Code:
    def extractRandomItem
        itemsArray=["POKEBALL","MEGABALL","ULTRABALL"]
        items=[]
        tempItem=itemsArray[rand(itemsArray.length)]
        items.push(tempItem)
        item=items[0]
        $PokemonBag.pbStoreItem(item)
      end

    And call it with: "extractRandomItem" so that it will only choose between those three items. Of course this is just a small adjustment of his script.
     
    Back
    Top