Save a map "state" and return to it after loading another map

Hello !
I'm currently working with the GTBS script of VX ace which allows to have tactical battles. It has however some issues that I am trying to fix.
One of the major ones to me is the fact that it loads a map (which can be the current one) and then returns to the original one which reload the original map from the start: events that moved or were deleted will be restored to their starting position, music will not be resumed as the previous music but rather as the map music etc.

Is there a way to load a map (as a GTBS one) while saving the previous one and then returning to the previous one as if we just left a battle scene ?
I am not asking to look at how GTBS works as it is a complicated script in my opinion, I am rather asking what kind of stuff could be done to save a map status and then returns it when reloading it, or maybe have two maps loaded but one that is "disabled" ?

Have a good day 🙂
You want to keep both stuff as fully separate mechanism, but that's virtually impossible.
Yes, you can get a script that would let you store all that data, but the scripter would have to know WHEN exactly the script call should be used BEFORE and AFTER any GTBS battle. This means the scripter certainly has to know a bit about how that BS works. If you already know that script calls for battle and for ending the battle session and can post that stuff here, that might prove to be quite helpful for both the scripter and you as the requestor.

There's also a real chance to work on your request blindly and hope for the best, yet, the patch might fail to accomplish your goal or you could ignore when you're supposed to make the script call.
Hi, thank you for your answer. I guess you are right, I need to add more details. For WHEN the script is called, I can deal with it by adding the script calls at the right places which is why I did not want to bother you with it.

Currently, the battle scene of GTBS works like this:
-You start a battle via event calling (fight this troop blabla) which will activate Scene_Battle_TBS instead ofScene_Battle
-It loads a map via script calling $game_map.setup(battle_map_id) (battle_map can be the same map as the current one) and then do other initialisation calls that are specific to GTBS
-you do the battle on this new map
-at the end of the battle it calls exit_battle
-exit_battle calls $game_map.setup(original_map_id) then move the player in it, play the music of the map etc.

I think there should be two functions/methods:
- save_map_state() which would store the current state of the events of the map (right before a call from $game_map.setup(battle_map_id)when used by GTBS) and the current map id.
- retrieve_map_state() which should be called when you want to load the map back (should replace or be used after a $game_map.setup(original_map_id)), it would check that the map we just loaded or intend to load is the map saved, and then calls the retrieving of the states of the events or call the original setup if it was not the right map.
Below is more details on the code of the scripts called:
Spoiler: Code
Ruby:
class Scene_Battle_TBS < Scene_Base
  def start
    super
    #I would add save_map_state() here or at the beginning of prepare_GTBS
    prepare_GTBS #will call $game_map.setup(battle_map_id) after finding the id of the battle map and initialize a few variables, then it will call function on the new map
  end

  #----------------------------------------------------------------
  # Call Battle Exit Command
  #----------------------------------------------------------------
  def exit_battle
    return_to_map, return_x, return_y, return_dir = GTBS::battle_exit_info(@map_id)
    if return_to_map.nil? || return_to_map == false || return_to_map == 0
      return_to_map = @map_id
    end
    if (return_to_map == @map_id)
      $game_map.setup(@map_id )
    else
      $game_map.setup(return_to_map)
    end
    #I would say that this is here that there should be a script call to put the events back to their positions
    $game_player.moveto(return_x, return_y) if return_x != nil
    $game_player.set_direction(return_dir) if return_dir != nil
    $game_party.clear_summons
    $game_party.clear_neutrals
    $game_troop.clear_summons
    @map_id = nil
    SceneManager.return
    $game_map.autoplay #and I would comment this line in order to not have the musics from the map start again
    $game_player.refresh
    $game_player.center($game_player.x, $game_player.y)
    Graphics.freeze
    Graphics.fadeout(30)
  end
end

I think the best way to test it without GTBS would be to have 2 maps (1 and 2), with 1 having events that move with patterns, have some that are deleted, that have their looks changed etc. and one event with the following instructions:
- script call to save_map_state()
- teleport the player via event calling to map 2
- get back to map 1 with retrieve_map_state() and moving the player character with $game_player.moveto(return_x, return_y) (the last instruction could be done inside the event or through one from map 2)
OK, here's my first attempt at saving and restoring such map data. Just like any other patch, it should be pasted in a script section BELOW the original BS script(s).

Ruby:
# * Old Map Data for GTBS * #
#   Plug & Play Script
#   Scripter : Kyonides Arkanthes
#   2024-08-19

class Game_Map
  alias :kyon_gtbs_mod_gm_map_setup_events :setup_events
  def setup_events
    if @tactical_battle
      setup_old_events
    else
      kyon_gtbs_mod_gm_map_setup_events
    end
  end

  def setup_old_events
    @events = @old_events
    @common_events = @old_common_events
    @old_events = @old_common_events = nil
    refresh_tile_events
  end

  def save_map_data
    @tactical_battle = true
    @old_bgm_pos = @map.bgm.pos unless @map.bgm.name.empty?
    @old_bgs_pos = @map.bgs.pos unless @map.bgs.name.empty?
    @old_events = @events
    @old_common_events = @common_events
  end

  def autoplay
    bgm_pos = @old_bgm_pos || 0
    bgs_pos = @old_bgs_pos || 0
    @old_bgm_pos = @old_bgs_pos = @tactical_battle = nil
    @map.bgm.play(bgm_pos) if @map.autoplay_bgm
    @map.bgs.play(bgs_pos) if @map.autoplay_bgs
  end
  attr_accessor :tactical_battle
end

class Scene_Battle_TBS
  def start
    super
    $game_map.save_map_data
    prepare_GTBS
  end

  def exit_battle
    return_to_map, mx, my, mdir = GTBS::battle_exit_info(@map_id)
    return_to_map = @map_id if !return_to_map || return_to_map == 0
    $game_map.setup(return_to_map)
    $game_player.moveto(mx, my) if mx != nil
    $game_player.set_direction(mdir) if mdir != nil
    $game_party.clear_summons
    $game_party.clear_neutrals
    $game_troop.clear_summons
    @map_id = nil
    SceneManager.return
    $game_map.autoplay
    $game_player.refresh
    $game_player.center($game_player.x, $game_player.y)
    Graphics.freeze
    Graphics.fadeout(30)
  end
end
Thanks, it works almost perfectly !
There is a small issue (commenting the line @old_events = @old_common_events = nil fix it but it is not satisfying):
setup_events and setup_old_events are called by game_map.setup which is called first for the battle map, meaning that the stored events are erased before retrieving the map causing an error.
It also will probably not work if return_to_map is not the original one (I think it can happen depending on the parameters of GTBS).

Still, I can adapt your such that there is a specific call for setup when returning from the battle map. Thank you !
Here's my second take on storing and restoring the map data:

Ruby:
# * Old Map Data for GTBS * #
#   Plug & Play Script
#   Scripter : Kyonides Arkanthes
#   v0.2.0 - 2024-08-19

class Game_Map
  alias :kyon_gtbs_mod_gm_map_setup_events :setup_events
  def setup_events
    if @tactical_battle_end
      setup_old_events
    else
      kyon_gtbs_mod_gm_map_setup_events
    end
  end

  def setup_old_events
    @events = @old_events.dup
    @common_events = @old_common_events.dup
    @old_events = @old_common_events = @tactical_battle_end = nil
    refresh_tile_events
  end

  def save_map_data
    @old_bgm_pos = @map.bgm.pos unless @map.bgm.name.empty?
    @old_bgs_pos = @map.bgs.pos unless @map.bgs.name.empty?
    @old_events = @events.dup
    @old_common_events = @common_events.dup
  end

  def autoplay
    bgm_pos = @old_bgm_pos || 0
    bgs_pos = @old_bgs_pos || 0
    @old_bgm_pos = @old_bgs_pos = nil
    @map.bgm.play(bgm_pos) if @map.autoplay_bgm
    @map.bgs.play(bgs_pos) if @map.autoplay_bgs
  end
  attr_accessor :tactical_battle_end
end

class Scene_Battle_TBS
  def start
    super
    $game_map.save_map_data
    prepare_GTBS
  end

  def exit_battle
    return_to_map, mx, my, mdir = GTBS::battle_exit_info(@map_id)
    return_to_map = @map_id if !return_to_map || return_to_map == 0
    $game_map.tactical_battle_end = true
    $game_map.setup(return_to_map)
    $game_player.moveto(mx, my) if mx != nil
    $game_player.set_direction(mdir) if mdir != nil
    $game_party.clear_summons
    $game_party.clear_neutrals
    $game_troop.clear_summons
    @map_id = nil
    SceneManager.return
    $game_map.autoplay
    $game_player.refresh
    $game_player.center($game_player.x, $game_player.y)
    Graphics.freeze
    Graphics.fadeout(30)
  end
end

I've moved this thread to RGSS3 Script Requests. Thank you.

I changed two lines to exit_battle to fix the remaining issues, otherwise it works as intended ! Thank you !
Ruby:
# * Old Map Data for GTBS * #
#   Plug & Play Script
#   Scripter : Kyonides Arkanthes
#   v0.2.1 - 2024-08-19

class Game_Map
  alias :kyon_gtbs_mod_gm_map_setup_events :setup_events
  def setup_events
    if @tactical_battle_end
      setup_old_events
    else
      kyon_gtbs_mod_gm_map_setup_events
    end
  end

  def setup_old_events
    @events = @old_events.dup
    @common_events = @old_common_events.dup
    @old_events = @old_common_events = @tactical_battle_end = nil
    refresh_tile_events
  end

  def save_map_data
    @old_bgm_pos = @map.bgm.pos unless @map.bgm.name.empty?
    @old_bgs_pos = @map.bgs.pos unless @map.bgs.name.empty?
    @old_events = @events.dup
    @old_common_events = @common_events.dup
  end

  def autoplay
    bgm_pos = @old_bgm_pos || 0
    bgs_pos = @old_bgs_pos || 0
    @old_bgm_pos = @old_bgs_pos = nil
    @map.bgm.play(bgm_pos) if @map.autoplay_bgm
    @map.bgs.play(bgs_pos) if @map.autoplay_bgs
  end
  attr_accessor :tactical_battle_end
end

class Scene_Battle_TBS
  def start
    super
    $game_map.save_map_data
    prepare_GTBS
  end

  def exit_battle
    return_to_map, mx, my, mdir = GTBS::battle_exit_info(@map_id)
    return_to_map = @map_id if !return_to_map || return_to_map == 0
    $game_map.tactical_battle_end = true if return_to_map == @map_id #to avoid cases where original map differs from output map
    $game_map.setup(return_to_map)
    $game_map.tactical_battle_end = false #to avoid future map changes looking for old events
    $game_player.moveto(mx, my) if mx != nil
    $game_player.set_direction(mdir) if mdir != nil
    $game_party.clear_summons
    $game_party.clear_neutrals
    $game_troop.clear_summons
    @map_id = nil
    SceneManager.return
    $game_map.autoplay
    $game_player.refresh
    $game_player.center($game_player.x, $game_player.y)
    Graphics.freeze
    Graphics.fadeout(30)
  end
end
OK, if you need to apply this to the old map if it's identical to the battle map, then you can use this third version:

Ruby:
# * Old Map Data for GTBS * #
#   Plug & Play Script
#   Scripter : Kyonides Arkanthes
#   v0.3.0 - 2024-08-19

class Game_Map
  alias :kyon_gtbs_mod_gm_map_setup_events :setup_events
  def setup_events
    if @tactical_battle_end
      setup_old_events
    else
      kyon_gtbs_mod_gm_map_setup_events
    end
  end

  def setup_old_events
    @events = @old_events.dup
    @common_events = @old_common_events.dup
    @old_events = @old_common_events = @tactical_battle_end = nil
    refresh_tile_events
  end

  def save_map_data
    @old_bgm_pos = @map.bgm.pos unless @map.bgm.name.empty?
    @old_bgs_pos = @map.bgs.pos unless @map.bgs.name.empty?
    @old_events = @events.dup
    @old_common_events = @common_events.dup
  end

  def autoplay
    bgm_pos = @old_bgm_pos || 0
    bgs_pos = @old_bgs_pos || 0
    @old_bgm_pos = @old_bgs_pos = nil
    @map.bgm.play(bgm_pos) if @map.autoplay_bgm
    @map.bgs.play(bgs_pos) if @map.autoplay_bgs
  end
  attr_accessor :tactical_battle_end
end

class Scene_Battle_TBS
  def start
    super
    $game_map.save_map_data
    prepare_GTBS
  end

  def exit_battle
    return_to_map, mx, my, mdir = GTBS::battle_exit_info(@map_id)
    return_to_map = @map_id if !return_to_map || return_to_map == 0
    $game_map.tactical_battle_end = return_to_map == @map_id
    $game_map.setup(return_to_map)
    $game_player.moveto(mx, my) if mx != nil
    $game_player.set_direction(mdir) if mdir != nil
    $game_party.clear_summons
    $game_party.clear_neutrals
    $game_troop.clear_summons
    @map_id = nil
    SceneManager.return
    $game_map.autoplay
    $game_player.refresh
    $game_player.center($game_player.x, $game_player.y)
    Graphics.freeze
    Graphics.fadeout(30)
  end
end

The difference here is that it will turn flag on ONLY IF both map ID's are identical, otherwise it will be set to false (or nil that work exactly the same way here).

BTW, I deleted your second call after the map's setup call because it's not supposed to be needed at all. If the map ID is the right one, the same process it triggers will turn the flag off automatically. This happens right here:

Ruby:
@old_events = @old_common_events = @tactical_battle_end = nil
Did not see that you modified this line in version 2 ahah.
Okay, looks good to me, thanks!

Original thread: https://forums.rpgmakerweb.com/threads/save-a-map-state-and-return-to-it-after-loading-another-map.171214/