• 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.

Switching Characters (storing temp party data)?

  • 13
    Posts
    10
    Years
    • Seen Nov 17, 2023
    So here's what I want to do: I want to be able to "switch" from one character in the game to another.

    Here's an example (I'll be using Red as a synonoym for one playable character and Blue for another playable character):

    You're playing as Red through the elite four.
    Once you've beaten Lance as Red the game transitions to Blue on top of Mt. Moon fighting a Tauros (or something, who cares).
    All of Red's Pokémon, items, money, badges, will be stored somewhere temporarily and the player will have all of Blue's Pokémon, items, money, badge.
    You defeat the Tauros and walk to the Elite 4 as Blue.
    You then switch to Red and obtains all of his Pokémon, items, money, badges.

    This might give you an idea of how I want the concept.

    So I did some research.

    https://pokemonessentials.wikia.com/wiki/Player#Trainer-related_information

    This showed me that there's a definition for each of the variables that defines a character.

    I can change the looks by using pbChangePlayer(X) e.g. I could have Red's outfit as Player1 and Blue's as Player2.

    I could change the name with pbTrainerName("Red")

    In the end badges, money and items aren't that important but my problem is the Pokémon.

    I don't know if the way the Battle Tower / Factory works can help figuring out a way to temporarily storing the Pokémon data. I don't know if this allows me to save 2 different sets.

    Anyways, this is my question. If you have any questions about my question leave a question (question, question, question, question, question, question).

    Thanks in advance!
     
  • 1,224
    Posts
    10
    Years
    Store your pokemon in a game variable
    Code:
    $game_variables[whatever number] = []
    for i in 0...$Trainer.party.length
    $game_variables[whatever number].push($Trainer.party[i])
    end
    $Trainer.party.clear
    #now give the new party

    Then do the reverse to give the party back
    Code:
    $Trainer.party.clear
    for i in 0...$game_variables[whatever number].length
    $Trainer.party.push($game_variables[whatever number][i])
    end
    $game_variables[whatever number].clear
     
  • 129
    Posts
    8
    Years
    • Seen Mar 23, 2023
    Create a second PokeBattle_Trainer (which includes the party, name, trainer id, etc., stored in $Trainer), PokemonStorage (the trainer's pokemon PC storage system, stored in $PokemonStorage), PokemonBag (the trainer's on-hand items, stored in $PokemonBag), PCItemStorage (the item PC, stored in $PokemonGlobal.pcItemStorage). If they share a PC, then you only need to create a second PokeBattle_Trainer and PokemonBag.

    Store them off in a location that will be saved to the save data (like in $PokemonGlobal or one of the $game_variables like mej71 mentioned). When you want to switch, swap them with the ones currently in those variables mentioned above.

    And bugtest the living hell out of it because you're going to run into a LOT of fun bugs that come from latent variables leftover from the previous trainer.

    I could change the name with pbTrainerName("Red")

    DO NOT DO THAT! Calling pbTrainerName is destructive! It resets the player's Bag and creates a new PokeBattle_Trainer object, and counts as beginning a whole new game! (If for some reason you need to change the player character's name, set a value $Trainer.name instead.) In fact, in your case, you may want to avoid using pbTrainerName entirely, and manually set up your two PokeBattle_Trainer instances, getting the names of those trainers from the player via two calls to pbEnterPlayerName, and initializing two PokemonBag instances.

    Essentials is not built for playing as two player characters, so there's going to be a lot of workarounds you will need to use, and you may not be able to use most of the standard player setup functions. If you do use pbTrainerName twice, (only in the game's setup), be sure to save off $Trainer and $PokemonBag before the second call, or you'll lose them entirely.
     
    Last edited:
  • 13
    Posts
    10
    Years
    • Seen Nov 17, 2023
    Store your pokemon in a game variable
    Code:
    $game_variables[whatever number] = []
    for i in 0...$Trainer.party.length
    $game_variables[whatever number].push($Trainer.party[i])
    end
    $Trainer.party.clear
    #now give the new party

    Then do the reverse to give the party back
    Code:
    $Trainer.party.clear
    for i in 0...$game_variables[whatever number].length
    $Trainer.party.push($game_variables[whatever number][i])
    end
    $game_variables[whatever number].clear

    Hey mej!

    Pardon me for bothering you, I can see how the above works. But I simply have no clue on how to use code in RPG Maker XP.

    Do I just have to write it under "Game_Variables"?

    I've used 546 hours in RPG Maker XP and I'm still this dumb when it comes to certain areas.
     
  • 13
    Posts
    10
    Years
    • Seen Nov 17, 2023
    Store them off in a location that will be saved to the save data (like in $PokemonGlobal or one of the $game_variables like mej71 mentioned). When you want to switch, swap them with the ones currently in those variables mentioned above.

    Once again I'm stuck at this part. I simply have no idea how to store off data in game variables :(
     
  • 129
    Posts
    8
    Years
    • Seen Mar 23, 2023
    Whenever your putting in code, you should always try and put it in a new script section just above "Main". You should avoid editing any of the script sections that make up the Essentials library if you don't know what you're doing. Go to your script editor and right click on "Main" and click "Insert". You can start putting new code in this new section. (If you want to make things tidy, you can put another script section with equals signs as the name to separate your scripts from others, like what 16.1 does.)

    The Game_Variables section is simply where the class that stores the game variables is defined. You don't need to add any more to that script section. If you want to store a variable in the game variables, you should use the method pbSet. This will set a value to the given variable number and tell the map to refresh based on that change. (People sometimes set it directly to the $game_variables[], and that's valid, but the Essentials library really likes when you use pbGet(id) and pbSet(id,value) instead.)

    So, here's an example script to define your two trainers, as I suggested. This would go into an event in the intro.

    Code:
    # We'll pretend we're storing the other trainer in global variable 22
    # This constant would be defined in a script section, not in an event script
    OTHER_TRAINER_VAR = 22
    
    #Ask the player for the first trainer's name
    pbTrainerName
    # The above creates a new trainer assigned to $Trainer and a new bag assigned to $PokemonBag
    
    # Ask player for second trainer's name
    trname=pbEnterPlayerName(_INTL("Your partner's name?"),0,7)
    # (assuming the second player's trainer type is defined right after the first)
    trainertype=pbGetPlayerTrainerType+1
    #Create the second trainer and his bag
    trainer2=PokeBattle_Trainer.new(trname,trainertype)
    bag2=PokemonBag.new
    
    #make sure the second trainer doesn't have the same id as the first
    trainer2.setForeignID($Trainer)
    
    # Now, set the second trainer and his bag to the variable chosen above, global variable 22
    pbSet(OTHER_TRAINER_VAR, [trainer2, bag2])

    Now, when you wish to switch trainers, you can make a function like this:

    Code:
    def pbSwitchTrainers()
      # Get the stored trainer data
      othertrainer = pbGet(OTHER_TRAINER_VAR)
      if (!othertrainer.is_a?(Array)) #Error checking
        raise "Something erased the other trainer! Panic!!"
      end
      
      # Prepare the current trainer data for storage
      currtrainer = [$Trainer, $PokemonBag]
      
      # Assign the other trainer to the current values
      $Trainer = othertrainer[0]
      $PokemonBag = othertrainer[1]
     
      # Store off the previously active trainer data
      pbSet(OTHER_TRAINER_VAR, currtrainer)
    end
    The player's party is kept in the trainer objects, so the party switch would be complete. Also, if you do want the two trainers to have the same id for some reason (which I wouldn't recommend simply for consistency with the pokemon world and the fun that might come from one trainer trying and failing to use a pokemon caught by the other trainer) then you can replace the setForeignID line with trainer2.id=$Trainer.id. Also note that the badges, money, and pokedex seen/own numbers are separately stored in the trainer objects as well.

    (Disclaimer: I apologize if there's any syntax errors in the above code. I don't have a way to run this at time of writing.)
     
    Last edited:
  • 13
    Posts
    10
    Years
    • Seen Nov 17, 2023
    I feel so dumb asking questions like this. But I simply have no idea how this corner of RPG Maker works :(

    Switching Characters (storing temp party data)?


    This is what I've wrote at the beginning of the game with the stuff off screen being:

    trname=pbEnterPlayerName(_INTL("Your partner's name?"),0,7)

    trainer2=PokeBattle_Trainer.new(trname,trainertype)

    A problem occurs when trying to create trainer2.

    Switching Characters (storing temp party data)?


    I've been trying to work around this for the past 30 minutes or so but with no luck.

    Once again, I'm really sorry for seeming so greedy. But I'll promise you I'll put this function to good use tustin :)
     
  • 129
    Posts
    8
    Years
    • Seen Mar 23, 2023
    I'm not sure where you got the idea to put all these conditional branches in there.

    The script I gave you should fit into one Script Command. (If it doesn't, run extendtext.exe from your project folder, and the dialog box will get bigger so you can fit more). No Conditional Commands needed for this. If you spread the script across multiple Script Commands, the local variables won't carry over, and that's the error you get.
     
  • 1,224
    Posts
    10
    Years
    I'm not sure where you got the idea to put all these conditional branches in there.

    The script I gave you should fit into one Script Command. (If it doesn't, run extendtext.exe from your project folder, and the dialog box will get bigger so you can fit more). No Conditional Commands needed for this. If you spread the script across multiple Script Commands, the local variables won't carry over, and that's the error you get.

    Actually, consecutive script commands are interpreted together, and can share local variables. This does not apply to scripts in conditional branches though.
     
  • 129
    Posts
    8
    Years
    • Seen Mar 23, 2023
    Actually, consecutive script commands are interpreted together, and can share local variables. This does not apply to scripts in conditional branches though.

    Ah, TIL. I've been using @instanceVariables to transfer data between script commands in an event. Those definitely work between Scripts and Conditionals, because I've set up Trainers in script commands and then called battles with them in the conditionals.
     
  • 1,224
    Posts
    10
    Years
    Ah, TIL. I've been using @instanceVariables to transfer data between script commands in an event. Those definitely work between Scripts and Conditionals, because I've set up Trainers in script commands and then called battles with them in the conditionals.

    It's actually storing those inside the Interpreter class. Be careful with using those, as the Interpreter class is only initialized once in a session, so those variables are shared among all events for that session, not just the currently executing one.
    Fun fact, "consecutive script commands are interpreted together, and can share local variables" is an Essentials feature, not included in RMXP. You can look in the interpreter, at def command_355 to see how/where it does this.
     
  • 129
    Posts
    8
    Years
    • Seen Mar 23, 2023
    It's actually storing those inside the Interpreter class. Be careful with using those, as the Interpreter class is only initialized once in a session, so those variables are shared among all events for that session, not just the currently executing one.

    Oh, that's unpleasant... I thought they were stored in the event object, which is created on every map load. I may have to rethink how I do that then. Thanks.
     
    Last edited:
  • 13
    Posts
    10
    Years
    • Seen Nov 17, 2023
    I've attempted your above solution tustin but it looks like mej debunked our attempt at storing it the wrong way. I suppose the only solution would be mej's right?

    I've managed to get mej's method to work and as you said I could use $Trainer.name="Tustin" to change the name :)

    So "the root" of my question was answered :) So if you didn't have another way to use your method tustin, then I'm fine off with mej's :) But thanks for the help both of you!
     
  • 129
    Posts
    8
    Years
    • Seen Mar 23, 2023
    I've attempted your above solution tustin but it looks like mej debunked our attempt at storing it the wrong way.

    That's untrue. We were talking about something somewhat unrelated. If you actually put all of my code I gave you into a single script command in the event, it'll work fine.

    But if you're content with what you've got, I'm not about to try and force my method on you. If it works, it works. Glad we could help.
     
  • 1,224
    Posts
    10
    Years
    Oh, that's unpleasant... I thought they were stored in the event object, which is created on every map load. I may have to rethink how I do that then. Thanks.

    You can test this by having an event that just runs the script
    Code:
    Kernel.pbMessage("#{@a}")
    And another that sets @a to something
    Code:
    @a="test"
    You'll see that they share the same data.
     
  • 129
    Posts
    8
    Years
    • Seen Mar 23, 2023
    You can test this by having an event that just runs the script
    Code:
    Kernel.pbMessage("#{@a}")
    And another that sets @a to something
    Code:
    @a="test"
    You'll see that they share the same data.

    Yeah I figured. I'm implementing an easy way to get to an event's temp variables for future use now. This seems to be working now:

    Code:
    class TempSwitchHash
      def initialize(hash)
        @hash = hash
      end
      def method_missing(m, *args)
        if /^(\w+)=$/ =~ m.to_s #setter
          @hash["#{$1}"] = args[0]
          return @hash["#{$1}"]
        end
        return @hash[m.to_s] #getter
      end
    end
    
    #In the Interpreter Mixins
      def my
        return TempSwitchHash.new(get_character(0).tempSwitches)
      end
    This way I can use things like my.trainer={ ... } and pxTrainerBattle(my.trainer) to set up complex trainers without polluting the Interpreter's variables. :)
     
    Back
    Top