• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • 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] Need help with a piece of my random item code

6
Posts
78
Days
  • Seen Mar 1, 2024
so i found this (Giving a random item from a list) and i tweaked it a bit
But i've been testing it in game and the only Items i was getting was from the first array (commonItems), and i dont know if i did it correctly
Ruby:
def chooseRandomItemM(qty=1)
  # Array of items
  commonItems = [
  :IRONBUTTON,
  :IRONORE,
  :IRONORE,
  :EXPCANDYXS
  ]

  rareItems = [
  :ENERGYROOT,
  :ENERGYROOT,
  :BIGNUGGET,
  :NUGGET,
  :NUGGET,
  :TMMATERIAL,
  :TMMATERIAL,
  :ICYROCK,
  :HEATROCK,
  :EXPCANDYS
  ]
 
  evoItems = [
  :DAWNSTONE,
  :DUSKSTONE,
  :FIRESTONE,
  :ICESTONE,
  :LEAFSTONE,
  :MOONSTONE,
  :SUNSTONE,
  :THUNDERSTONE,
  :WATERSTONE,
  :EXPCANDYL
  ]
 
  fossilItems = [
  :ARMORFOSSIL,
  :CLAWFOSSIL,
  :COVERFOSSIL,
  :FOSSILIZEDBIRD,
  :FOSSILIZEDDINO,
  :FOSSILIZEDDRAKE,
  :FOSSILIZEDFISH,
  :HELIXFOSSIL,
  :JAWFOSSIL,
  :OLDAMBER,
  :PLUMEFOSSIL,
  :ROOTFOSSIL,
  :SAILFOSSIL,
  :EXPCANDYM
  ]
 
  megaItems = [
  :ABOMASITE,
  :ABSOLITE,
  :AERODACTYLITE,
  :AGGRONITE,
  :ALAKAZITE,
  :ALTARIANITE,
  :AMPHAROSITE,
  :BANETTITE,
  :BEEDRILLITE,
  :BLASTOISINITE,
  :BLAZIKENITE,
  :CAMERUPTITE,
  :CHARIZARDITEX,
  :CHARIZARDITEY,
  :DIANCITE,
  :GARCHOMPITE,
  :GARDEVOIRITE,
  :GENGARITE,
  :GLALITITE,
  :HERACRONITE,
  :HOUNDOOMINITE,
  :KANGASKHANITE,
  :LATIASITE,
  :LATIOSITE,
  :MAWILITE,
  :MEDICHAMITE,
  :METAGROSSITE,
  :MEWTWONITEX,
  :MEWTWONITEY,
  :PINSIRITE,
  :SABLENITE,
  :SALAMENCITE,
  :SCEPTILITE,
  :SCIZORITE,
  :SHARPEDONITE,
  :SLOWBRONITE,
  :SWAMPERTITE,
  :TYRANITARITE,
  :VENUSAURITE,
  :EXPCANDYXL,
  :EXPCANDYXL
  ]
 
  # Randomly select item from above array
m = rand(1..13)
if m = (1..4)
  pbReceiveItem(commonItems[rand(0...commonItems.length)],qty)
elsif m = (5..7)
  pbReceiveItem(rareItems[rand(0...rareItems.length)],qty)
elsif m = (8..10)
  pbReceiveItem(fossilItems[rand(0...fossilItems.length)],qty)
elsif m = (9..12)
  pbReceiveItem(evoItems[rand(0...evoItems.length)],qty)
else
  pbReceiveItem(megaItems[rand(0...megaItems.length)],qty)
end
end
 
Last edited:
1,677
Posts
8
Years
  • Age 23
  • Seen yesterday
So first, why the code isn't working.
This m = (1..4) means set m to (1..4), rather than check it against it, with ==. Since everything in Ruby is truthy, except false and nil, the first if is always the one that runs.

I would replace this with a case when statement, as I know that works with ranges (I'm not sure m == (1..4) works).
Ruby:
case rand(1..13)
when 1..4
  pbReceiveItem(commonItems[rand(commonItems.length)],qty)
when 5..7
  pbReceiveItem(rareItems[rand(rareItems.length)],qty)
when 8..10
  pbReceiveItem(fossilItems[rand(fossilItems.length)],qty)
when 9..12
  pbReceiveItem(evoItems[rand(evoItems.length)],qty)
else
  pbReceiveItem(megaItems[rand(megaItems.length)],qty)
end
 
6
Posts
78
Days
  • Seen Mar 1, 2024
So first, why the code isn't working.
This m = (1..4) means set m to (1..4), rather than check it against it, with ==. Since everything in Ruby is truthy, except false and nil, the first if is always the one that runs.

I would replace this with a case when statement, as I know that works with ranges (I'm not sure m == (1..4) works).
Ruby:
case rand(1..13)
when 1..4
  pbReceiveItem(commonItems[rand(commonItems.length)],qty)
when 5..7
  pbReceiveItem(rareItems[rand(rareItems.length)],qty)
when 8..10
  pbReceiveItem(fossilItems[rand(fossilItems.length)],qty)
when 9..12
  pbReceiveItem(evoItems[rand(evoItems.length)],qty)
else
  pbReceiveItem(megaItems[rand(megaItems.length)],qty)
end
thanks so much
 
Back
Top