• 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.
  • Ever thought it'd be cool to have your art, writing, or challenge runs featured on PokéCommunity? Click here for info - we'd love to spotlight your work!
  • It's time to vote for your favorite Pokémon Battle Revolution protagonist in our new weekly protagonist poll! Click here to cast your vote and let us know which PBR protagonist you like most.
  • 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.

[Error] Infinite Loop in Trainer Generation

  • 288
    Posts
    6
    Years
    • Seen Feb 3, 2025
    Although not technically an error, I have stumbled upon what seems to be an infinite loop, but I'm not sure. When playtesting, after attempting to initiate a trainer battle (doesn't matter which trainer), it says the script took too long to load. I have made a couple of edits to the pbLoadTrainer method in the script section titled PTrainer_NPCTrainers, but am unsure as to why they would be causing problems. Maybe I changed something else that caused this. I think I've deduced the problem to be in the loop with the starting line "for poke in trainer[3]" after placing statements that would cause an error to check where the code would get caught. It will catch the error statement when placed right before the end of this loop but wont reach it if placed before the line "success=true". It is important to note that in this section, I changed the first line to be a conditional statement, making an optional setting with game switch 61 to randomly generate the Pokemon species. When this switch is on and the species is randomly generated, and the trainer battles occur fine. However, when the switch is off, that's where the problems happen. Here's the code for the method. Changes to the original method should be in red.
    Code:
    def pbLoadTrainer(trainerid,trainername,partyid=0)
      if trainerid.is_a?(String) || trainerid.is_a?(Symbol)
        if !hasConst?(PBTrainers,trainerid)
          raise _INTL("Trainer type does not exist ({1}, {2}, ID {3})",trainerid,trainername,partyid)
        end
        trainerid=getID(PBTrainers,trainerid)
      end
      success=false
      items=[]
      party=[]
      opponent=nil
      trainers=load_data("Data/trainers.dat")
      for trainer in trainers
        name=trainer[1]
        thistrainerid=trainer[0]
        thispartyid=trainer[4]
        next if trainerid!=thistrainerid || name!=trainername || partyid!=thispartyid
        items=trainer[2].clone
        name=pbGetMessageFromHash(MessageTypes::TrainerNames,name)
        for i in RIVALNAMES
          if isConst?(trainerid,PBTrainers,i[0]) && $game_variables[i[1]]!=0
            name=$game_variables[i[1]]
          end
        end
        opponent=PokeBattle_Trainer.new(name,thistrainerid)
        opponent.setForeignID($Trainer) if $Trainer
        for poke in trainer[3]
          [COLOR="Red"]if !$game_switches[61]
            species=poke[TPSPECIES]
          else
            species=1+rand(PBSpecies.maxValue)
          end[/COLOR]
          level=poke[TPLEVEL]
          pokemon=PokeBattle_Pokemon.new(species,level,opponent)
          pokemon.forcedForm = true if poke[TPFORM]!=0 && MultipleForms.hasFunction?(pokemon.species,"getForm")
          pokemon.formNoCall=poke[TPFORM]
          pokemon.trainerResetMoves
          pokemon.setItem(poke[TPITEM])
          if poke[TPMOVE1]>0 || poke[TPMOVE2]>0 || poke[TPMOVE3]>0 || poke[TPMOVE4]>0
            k=0
            for move in [TPMOVE1,TPMOVE2,TPMOVE3,TPMOVE4]
              pokemon.moves[k]=PBMove.new(poke[move])
              k+=1
            end
            pokemon.moves.compact!
          end
          pokemon.setAbility(poke[TPABILITY])
          pokemon.setGender(poke[TPGENDER])
          [COLOR="red"]trainerShiny=rand(65535)
          if poke[TPSHINY] || trainerShiny<655  # if this is a shiny Pokémon
            pokemon.makeShiny
          else
            pokemon.makeNotShiny
          end[/COLOR]
          pokemon.setNature(poke[TPNATURE])
          iv=poke[TPIV]
          for i in 0...6
            pokemon.iv[i]=iv&0x1F
            pokemon.ev[i]=[85,level*3/2].min
          end
          pokemon.happiness=poke[TPHAPPINESS]
          pokemon.name=poke[TPNAME] if poke[TPNAME] && poke[TPNAME]!=""
          if poke[TPSHADOW]   # if this is a Shadow Pokémon
            pokemon.makeShadow rescue nil
            pokemon.pbUpdateShadowMoves(true) rescue nil
            pokemon.makeNotShiny
          end
          pokemon.ballused=poke[TPBALL]
          pokemon.calcStats
          party.push(pokemon)
        end
        success=true
        break
      end
      return success ? [opponent,items,party] : nil
    end
     
    Thanks. I replaced the method with the original one and it worked, but somehow, when I added the new parts in red above, it worked fine. I have no idea what caused this mistake but it seems to be gone now.

    EDIT: I now found that the error was coming from the trainerResetMoves custom method that I defined. I forgot that this was part of the code, and it seemed to be causing the error.
     
    Last edited:
    Back
    Top