World/Location Window

World/Location Window
Version: 1.0
By: Sailerius

Introduction

I wrote this for a game a friend of mine is making. It's nothing special, but it's my first script. So, I'm looking for comments on how to improve it and what I did right/wrong. I'm not too familiar with scripting conventions.

It shows a window at the top left of the screen that displays the name of the current world (or country, or island...) and below it the name of the map. The window can be hidden and displayed by manipulating a switch, whose ID number you specify.

Screenshots

Image

Script

script Spoiler
  1.  
  2. #==============================================================================
  3. # ** Window_Location
  4. #------------------------------------------------------------------------------
  5. #  This window displays universe name and map name.
  6. #  Version 1.0
  7. #  Written by Sailerius
  8. #
  9. #  Instructions: Define the constants below.
  10. #  You can make the location window visible/invisible by toggling the visibility switch.
  11. #  It will display the name of the current map.
  12. #  You can define what it says for each universe below.
  13. #==============================================================================
  14. class Window_Location < Window_Base
  15.   #--------------------------------------------------------------------------
  16.   # * Object Initialization
  17.   #--------------------------------------------------------------------------
  18.   def initialize
  19.     #--------------------------------------------------------------------------
  20.     # * Constants: Edit these values to customize
  21.     #--------------------------------------------------------------------------
  22.     $VISIBILITY_SWITCH = 1 # Enter the ID number of the switch used to determine window visibility
  23.     $WORLD_VAR = 1 # Enter the ID number of the variable used to determine current universe
  24.     #--------------------------------------------------------------------------
  25.     # * Values: Here, enter what you want the location window to display for each
  26.     # variable value.  For example, for WORLD[1], enter what you want the universe
  27.     # section of the window to say when the variable is set to 1.
  28.     # Feel free to add more.
  29.     # NOTE: If you set the variable to a number not defined here, it'll probably crash.
  30.     #--------------------------------------------------------------------------
  31.     $WORLD=[]
  32.     $WORLD[0] = "Undefined"
  33.     $WORLD[1] = "Universe 1"
  34.     $WORLD[2] = "Universe 2"
  35.     $WORLD[3] = "Universe 3"
  36.     $WORLD[4] = "Universe 4"
  37.     $WORLD[5] = "Universe 5"
  38.     $WORLD[6] = "Universe 6"
  39.     #--------------------------------------------------------------------------
  40.     # Don't edit anything below here.
  41.     #--------------------------------------------------------------------------
  42.     super(0, 0, 160, 64) #96
  43.     self.contents = Bitmap.new(width - 32, height - 32)
  44.     self.contents.font.size = 16
  45.     self.visible = $game_switches[$VISIBILITY_SWITCH]
  46.     refresh
  47.   end
  48.   #--------------------------------------------------------------------------
  49.   # * Refresh
  50.   #--------------------------------------------------------------------------
  51.   def refresh
  52.     self.contents.clear
  53.     self.contents.font.color = system_color
  54.     self.contents.draw_text(4, -8, 120, 32, $WORLD[$game_variables[$WORLD_VAR]])
  55.     self.contents.font.color = normal_color
  56.     self.contents.draw_text(4, 4, 120, 32, $game_map.name, 1)
  57.   end
  58. end
  59.   #==========================================================================
  60. ====
  61.   # * Game_Map
  62.   #==========================================================================
  63. ====
  64. class Game_Map
  65.   def name
  66.     $map_infos[@map_id]
  67.   end
  68. end
  69.   #==========================================================================
  70. ====
  71.   # * Scene_Title
  72.   #==========================================================================
  73. ====
  74. class Scene_Title
  75.   # Enables ability to show map name on screen
  76.   $map_infos = load_data("Data/MapInfos.rxdata")
  77.   for key in $map_infos.keys
  78.     $map_infos[key] = $map_infos[key].name
  79.   end
  80. end
  81.   #==========================================================================
  82. ====
  83.   # * Scene_Map
  84.   #==========================================================================
  85. ====
  86. class Scene_Map
  87.   attr_accessor:location_window
  88.   #--------------------------------------------------------------------------
  89.   # * Main Processing
  90.   #--------------------------------------------------------------------------
  91.   def main
  92.     # Make sprite set
  93.     @spriteset = Spriteset_Map.new
  94.     # Make message window
  95.     @message_window = Window_Message.new
  96.     # Make location window
  97.     @location_window = Window_Location.new
  98.     @location_window.x = 0
  99.     @location_window.y = 0
  100.     # Transition run
  101.     Graphics.transition
  102.     # Main loop
  103.     loop do
  104.       # Update game screen
  105.       Graphics.update
  106.       # Update input information
  107.       Input.update
  108.       # Frame update
  109.       update
  110.       # Abort loop if screen is changed
  111.       if $scene != self
  112.         break
  113.       end
  114.     end
  115.     # Prepare for transition
  116.     Graphics.freeze
  117.     # Dispose of sprite set
  118.     @spriteset.dispose
  119.     # Dispose of message window
  120.     @message_window.dispose
  121.     # Dispose of location window
  122.     @location_window.dispose
  123.     # If switching to title screen
  124.     if $scene.is_a?(Scene_Title)
  125.       # Fade out screen
  126.       Graphics.transition
  127.       Graphics.freeze
  128.     end
  129.   end
  130.   #--------------------------------------------------------------------------
  131.   # * Player Place Move
  132.   #--------------------------------------------------------------------------
  133.   def transfer_player
  134.     # Clear player place move call flag
  135.     $game_temp.player_transferring = false
  136.     # If move destination is different than current map
  137.     if $game_map.map_id != $game_temp.player_new_map_id
  138.       # Set up a new map
  139.       $game_map.setup($game_temp.player_new_map_id)
  140.       @location_window.refresh
  141.     end
  142.     # Set up player position
  143.     $game_player.moveto($game_temp.player_new_x, $game_temp.player_new_y)
  144.     # Set player direction
  145.     case $game_temp.player_new_direction
  146.     when 2  # down
  147.       $game_player.turn_down
  148.     when 4  # left
  149.       $game_player.turn_left
  150.     when 6  # right
  151.       $game_player.turn_right
  152.     when 8  # up
  153.       $game_player.turn_up
  154.     end
  155.     # Straighten player position
  156.     $game_player.straighten
  157.     # Update map (run parallel process event)
  158.     $game_map.update
  159.     # Remake sprite set
  160.     @spriteset.dispose
  161.     @spriteset = Spriteset_Map.new
  162.     # If processing transition
  163.     if $game_temp.transition_processing
  164.       # Clear transition processing flag
  165.       $game_temp.transition_processing = false
  166.       # Execute transition
  167.       Graphics.transition(20)
  168.     end
  169.     # Run automatic change for BGM and BGS set on the map
  170.     $game_map.autoplay
  171.     # Frame reset
  172.     Graphics.frame_reset
  173.     # Update input information
  174.     Input.update
  175.   end
  176.   #--------------------------------------------------------------------------
  177.   # * Frame Update
  178.   #--------------------------------------------------------------------------
  179.   def update
  180.     # Loop
  181.     loop do
  182.       # Update map, interpreter, and player order
  183.       # (this update order is important for when conditions are fulfilled
  184.       # to run any event, and the player isn't provided the opportunity to
  185.       # move in an instant)
  186.       $game_map.update
  187.       $game_system.map_interpreter.update
  188.       $game_player.update
  189.       # Update system (timer), screen
  190.       $game_system.update
  191.       $game_screen.update
  192.       # Abort loop if player isn't place moving
  193.       unless $game_temp.player_transferring
  194.         break
  195.       end
  196.       # Run place move
  197.       transfer_player
  198.       # Abort loop if transition processing
  199.       if $game_temp.transition_processing
  200.         break
  201.       end
  202.     end
  203.     # Update sprite set
  204.     @spriteset.update
  205.     # Update message window
  206.     @message_window.update
  207.     # Update location window
  208.     @location_window.update
  209.     # If game over
  210.     if $game_temp.gameover
  211.       # Switch to game over screen
  212.       $scene = Scene_Gameover.new
  213.       return
  214.     end
  215.     # If returning to title screen
  216.     if $game_temp.to_title
  217.       # Change to title screen
  218.       $scene = Scene_Title.new
  219.       return
  220.     end
  221.     # If transition processing
  222.     if $game_temp.transition_processing
  223.       # Clear transition processing flag
  224.       $game_temp.transition_processing = false
  225.       # Execute transition
  226.       if $game_temp.transition_name == ""
  227.         Graphics.transition(20)
  228.       else
  229.         Graphics.transition(40, "Graphics/Transitions/" +
  230.           $game_temp.transition_name)
  231.       end
  232.     end
  233.     # If showing message window
  234.     if $game_temp.message_window_showing
  235.       return
  236.     end
  237.     # If encounter list isn't empty, and encounter count is 0
  238.     if $game_player.encounter_count == 0 and $game_map.encounter_list != []
  239.       # If event is running or encounter is not forbidden
  240.       unless $game_system.map_interpreter.running? or
  241.              $game_system.encounter_disabled
  242.         # Confirm troop
  243.         n = rand($game_map.encounter_list.size)
  244.         troop_id = $game_map.encounter_list[n]
  245.         # If troop is valid
  246.         if $data_troops[troop_id] != nil
  247.           # Set battle calling flag
  248.           $game_temp.battle_calling = true
  249.           $game_temp.battle_troop_id = troop_id
  250.           $game_temp.battle_can_escape = true
  251.           $game_temp.battle_can_lose = false
  252.           $game_temp.battle_proc = nil
  253.         end
  254.       end
  255.     end
  256.     # If B button was pressed
  257.     if Input.trigger?(Input::B)
  258.       # If event is running, or menu is not forbidden
  259.       unless $game_system.map_interpreter.running? or
  260.              $game_system.menu_disabled
  261.         # Set menu calling flag or beep flag
  262.         $game_temp.menu_calling = true
  263.         $game_temp.menu_beep = true
  264.       end
  265.     end
  266.     # If debug mode is ON and F9 key was pressed
  267.     if $DEBUG and Input.press?(Input::F9)
  268.       # Set debug calling flag
  269.       $game_temp.debug_calling = true
  270.     end
  271.     # If player is not moving
  272.     unless $game_player.moving?
  273.       # Run calling of each screen
  274.       if $game_temp.battle_calling
  275.         call_battle
  276.       elsif $game_temp.shop_calling
  277.         call_shop
  278.       elsif $game_temp.name_calling
  279.         call_name
  280.       elsif $game_temp.menu_calling
  281.         call_menu
  282.       elsif $game_temp.save_calling
  283.         call_save
  284.       elsif $game_temp.debug_calling
  285.         call_debug
  286.       end
  287.     end
  288.   end
  289. end
Expand to see the code.


Instructions

Paste the script into a new slot above Main. Change the constants at the top to define what switch you're using for visibility and what variable you're using to store current world. Change the names of the worlds to whatever you want.

FAQ

---

Compatibility

Probably doesn't work with anything that modifies Scene_Map.

Author's Notes

I don't expect anyone to have a use for this. Just looking for feedback on how to script better.

Terms and Conditions

Free to use however you want. I don't know why you'd want to. Just be sure to credit me and let me know so I can see your game!
Good script, neat coding and commenting. My only concern is that you overwrote the Map scene, instead of including the whole Scene_Map, try aliasing. E.G:

  1.  
  2. Scene_Map
  3.  alias oldmain main
  4.   def main
  5.    oldmain
  6.   your new script here
  7.   end
  8. end
  9.  
Expand to see the code.


Hope it helps :D
Very nice! ^_^
I agree with Eragon, aliasing would be nice, and might make it more compatible with anything that modifies Scene_Map.
Also, just a nitpick, you refer to the variables in your script as 'constants' when they are, in fact, global variables, denoted by the '$' before them. Constants have identifiers that just start with capital letters, such as WORLD_VAR. The '$' is not really needed for that type of thing unless you intend to reference the variable from another class, in which, in a constant's case, can be done with ClassName::Constant.
Keep up the good work though! Glad to see more scripters around.

EDIT:
I feel that I should also mention that constants cannot be defined within a method itself.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=65694