• 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.
  • Dawn, Gloria, Juliana, or Summer - which Pokémon protagonist is your favorite? Let us know by voting in our poll!
  • 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!
  • 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] Collecting Variables from a Random Encounter

  • 1
    Posts
    6
    Years
    • Seen Apr 11, 2019
    So I'm very, VERY, RIDICULOUSLY new to scripting, and I'm wondering two questions:
    1. Can you change a variable or switch when defeating a Pokémon from a random encounter (or any encounter)?
    2. Can you detect what Pokémon was last defeated and store its name or Pokédex number in a variable?
    I'm trying to create something where something occurs when you defeat different species of Pokémon. Thanks in advance!
     
    The answer to both of those questions is yes!
    The catch is the code is slightly spread out, but it's not too complex, we aren't going poking around anywhere.
    So, first thing first, you can change the value of a game switch or game variable in the scripts by setting $game_switches[XXX] or $game_variables[XXX] for switch XXX and variable XXX respectively.

    There are also two Event handlers that we are interested in. The first is Events.onStartBattle. It gives us a pokemon object, which we can look at and possibly modify, or nil if it's a trainer battle. There's an example in Script Section PField_EncounterModifiers.
    Now we just want to store the species, so here's a little quick thing that does it for us.
    Code:
    Events.onStartBattle+=proc {|sender,e|
       pokemon=e[0]
       $game_variables[XXX]= (pokemon)? pokemon.species : 0
    }
    This just stores the species in variable XXX or 0 if it was a trainer battle. If you wanted form too, you could store an array ([pokemon.species,pokemon.form]) or use multiple variables.
    The other bit is in Events.onEndBattle. This tells us the decision made for the battle.
    Code:
    # Results of battle:
    #    0 - Undecided or aborted
    #    1 - Player won
    #    2 - Player lost
    #    3 - Player or wild Pokémon ran from battle, or player forfeited the match
    #    4 - Wild Pokémon was caught
    #    5 - Draw
    That's what the numbers mean, and one of those is the decision.
    You'll want to look at that for how you want your script to turn out.
    Code:
    Events.onEndBattle+=proc {|sender,e|
      decision = e[0]
      canlose  = e[1]
      if (decision ==  1 || decision ==  4) # fainted or caught the pokemon
        if $game_variables[XXX]!=0 # was it a wild battle?
          # do something
        end
      end
    }
    Not really sure what you want to do, but this is a start I'd suppose.

    This sounds pretty similar to my rescue chain script if you want to take a look at that. I wouldn't blame you for rolling your own though, it's good to learn how to script.

    Good luck and have fun!
     
    Back
    Top