• 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] Ways to change a Pokemon's ball in-game (via NPC)

101
Posts
4
Years
  • As the title says. I've figured out how to add an option to change your Pokemon's poke ball from the party menu, but I'd prefer having a post-game NPC who lets you do that. Problem is, when I try to implement such an NPC, I get the following error when I select a pokemon in the party:
    Ways to change a Pokemon's ball in-game (via NPC)

    This is my NPC setup:
    Ways to change a Pokemon's ball in-game (via NPC)

    And the corresponding script (It's messy. I pulled it from the debug script, but this DOES work if I use it as an option from the party menu itself):
    Ways to change a Pokemon's ball in-game (via NPC)

    I assume it has something to do with me trying to call the event from the field and not from the party screen itself. I talk to the NPC, he lets me choose a pokemon, but the moment I do I get that error message.
    Any help is appreciated. Thank you!
     

    StCooler

    Mayst thou thy peace discover.
    9,301
    Posts
    4
    Years
    • Seen May 5, 2024
    Without enteering into details, it's because you use a variable called "pkmn" that isn't initialized.
    What you want is the Pokémon you chose, should be stored in the variable pkmn. However, I don't know v17.2 so I am not sure what the function pbChoosePokemon does. Can you post its code?
    Be careful, there are several functions that have this name; take the one that isn't in a class of module.
     

    Skystrike

    [i]As old as time itself.[/i]
    1,641
    Posts
    15
    Years
  • As said before, pkmn isn't initialized. If you'd rather call it from the overworld, either
    Code:
    pkmn = $Trainer.party[$game_variables[1]
    at the second line of the function or changing pbsetpokeball to have an argument such as it becoming
    Code:
    def pbsetpokeball(pkmnid)
    and then adding
    Code:
    pkmn = $Trainer.party[pkmnid]
    immediately under it, and calling pbsetpokeball with
    Code:
    pbsetpokeball($game_variables[1])
    in the event.

    With those changes you'll end up running into other problems, such as the @scene variable not being defined (since this is being called from the overworld) or pbShowCommands not having a certain variable defined. These are easily fixed by changing
    Code:
    cmd = @scene.pbShowCommands(_INTL("{1} used.",oldball),commands,cmd)
    to
    Code:
    cmd = pbShowCommands(nil,commands,cmd)
    You must also remove the loop function, otherwise the player will be unable to exit.

    This is how the script looked after I tried implementing it:
    Code:
    def pbSetPokeball(pkmnid)
      pkmn = $Trainer.party[pkmnid]
      commands = []; balls = []
      for key in $BallTypes.keys
        item = getID(PBItems,$BallTypes[key])
        balls.push([key.to_i,PBItems.getName(item)]) if item && item>0
      end
      balls.sort! { |a,b| a[1]<=>b[1] }
      cmd = 0
      for i in 0...balls.length
        if balls[i][0]==pkmn.ballused
            cmd = i; break
        end
      end
      for i in balls
        commands.push(i[1])
      end
        oldball = PBItems.getName(pbBallTypeToItem(pkmn.ballused))
        cmd = pbMessage(_INTL("Which Pokeball would you like to use?\nCurrent ball: {1}", oldball),commands,cmd)
        pkmn.ballused = balls[cmd][0]
      end

    I ended up changing pbShowCommands to pbMessage so we could easily include a message during the event. Also of note is that, as is, the player can change their Pokemon's Pokeball to any Pokeball the game for free, but I assume that's intended.
     
    101
    Posts
    4
    Years
  • Thank you both so much! Using Skystrike's code (with a few tweaks), I got it to work.

    The code I used is as follows:
    Code:
    def pbSetPokeball(pkmnid)
      pkmn = $Trainer.party[pkmnid]
      commands = []; balls = []
      for key in $BallTypes.keys
        item = getID(PBItems,$BallTypes[key])
        balls.push([key.to_i,PBItems.getName(item)]) if item && item>0
      end
      balls.sort! { |a,b| a[1]<=>b[1] }
      cmd = 0
      for i in 0...balls.length
        if balls[i][0]==pkmn.ballused
            cmd = i; break
        end
      end
      for i in balls
        commands.push(i[1])
      end
        oldball = PBItems.getName(pbBallTypeToBall(pkmn.ballused))
        cmd = Kernel.pbMessage(_INTL("Which Pokeball would you like to use?\nCurrent ball: {1}", oldball),commands,cmd)
        pkmn.ballused = balls[cmd][0]
      end

    And the corresponding event (bare-bones):
    Ways to change a Pokemon's ball in-game (via NPC)

    I might make it cost some money for the player to do so, but I just couldn't figure out the coding side of it. Thank you guys so much! It works amazingly now :)
     
    Back
    Top