creating a new array from pieces of another [resolved]

ok, so i have this array: Sub::Towns and i want to pull out all of the items in that array that have the trait Sub::Towns[i].upper_level = "String" and put them into a new array. to be honest i have no idea where to even start.
If you want to delete items from one array, and move to the other,
don't do this:
  1. for item in my_arr
  2.  my_arr.delete(item) if (some_condition)
  3. end
Expand to see the code.


It'll destroy the flow of the loop and some items will never be checked.
Do something like:
  1.   for item in arr1
  2.     items_to_relocate.push(item) if (some_condition)
  3.   end
  4.   for item in items_to_relocate
  5.     arr1.delete(item)
  6.     arr2.push(item)
  7.   end
Expand to see the code.
i dont want to delete the data for i will be using it again later for something else. maybe if i supply my data structure that will help.
  1. class Data::Object
  2.   @@ids = {}
  3.   @@containers = {}
  4.   attr_accessor :id
  5.   def initialize
  6.     @@ids[self.class] = 0 unless @@ids.has_key?(self.class)
  7.     @@ids[self.class] += 1
  8.     @id = @@ids[self.class]
  9.     if @@containers.has_key?(self.class)
  10.       @@containers[self.class][@id] = self
  11.     end
  12.   end
  13.   def self.add_container(class_name, container)
  14.     @@containers[class_name] = container
  15.   end
  16. end
Expand to see the code.

  1. class Sub::Town < Data::Object
  2.   Data::Object.add_container(self, Sub::Towns)
  3.   attr_reader :screen_x
  4.   attr_reader :screen_y
  5.   attr_reader :map_id
  6.   attr_reader :map_x
  7.   attr_reader :map_y
  8.   attr_reader :direction
  9.   attr_reader :icon
  10.   attr_reader :town_name
  11.   attr_reader :switch
  12.   attr_reader :upper_level
  13.   def initialize(screen_x, screen_y, map_id, map_x, map_y, direction, icon, town_name, switch, upper_level)
  14.    super()
  15.     @screen_x = screen_x
  16.     @screen_y = screen_y
  17.     @map_id = map_id
  18.     @map_x = map_x
  19.     @map_y = map_y
  20.     @direction = direction
  21.     @icon = icon
  22.     @town_name = town_name
  23.     @switch = switch
  24.     @upper_level = upper_level
  25.   end
  26.   def self.screen_x
  27.     return @screen_x
  28.   end
  29.   def self.screen_y
  30.     return @screen_y
  31.   end
  32.   def self.map_id
  33.     return @map_id
  34.   end
  35.   def self.map_x
  36.     return @map_x
  37.   end
  38.   def self.map_y
  39.     return @map_y
  40.   end
  41.   def self.direction
  42.     return @direction
  43.   end
  44.   def self.icon
  45.     return @icon
  46.   end
  47.   def self.town_name
  48.     return @town_name
  49.   end
  50.   def self.switch
  51.     return @switch
  52.   end
  53.   def self.upper_level
  54.     return @upper_level
  55.   end
  56. end
Expand to see the code.

so what im trying to do, is pull out the data i need temporarily and put it into a new array so i can use it to draw all the info to the screen. the reason i need a new array is because i only want to draw the data based on which "upper_level" im looking at. that make more sense?
I'm confused.
What exactly is the problem? You don't have to delete anything, Just follow my example without the line: arr1.delete(item)
Also you don't need all the methods below initialize. The attr_reader lines give you access to screen_x, screen_y, map_id, map_x, map_y and upper_level. currently, your methods do nothing.
thanks for the info about the methods, but im still dont understand your logic for my arrays. the way i understand it all i need is:
  1.  
  2. for i in 1...Sub::Towns.size+1
  3.    if Sub::Towns[i].upper_level == @upper
  4.      @section.push(Sub::Towns[i])
  5.    end  
  6. end
Expand to see the code.

but that doesnt work...bleh. i really appreciate the help. i have to go to work now. hopefully when i get home it will all make sense :) if u know what im doing wrong let me know please :) have a good day/night
Your method looks ok.
The problem can be something simple, like the +1 in line 2.
no im 100% sure about line 1, it occurs like a 100 times in my script...im at work so i cant try mess with it atm. but if u think it looks good, then at least im on the right track :) thanks for help
so here is the method as im using it:
  1.  
  2. def make_section
  3.     for i in 1...Sub::Towns.size+1
  4.       if Sub::Towns[i].upper_level == @upper
  5.         @section.push(Sub::Towns[i])
  6.         print(@section.size.to_s)
  7.       end  
  8.     end  
  9. end
Expand to see the code.

i added print so i could see it add each section. but in another method down the line i get the: "undefined method 'size' for nil:NilClass" error. here is that method:
  1.  
  2. def draw_towns
  3.     for i in [email hidden] #line i get error on
  4.       <draw_towns logic>
  5.     end
  6.   end
Expand to see the code.

any advice? :)
It may help if you post the entire script here.
its quite long and i havent started to switch the format of any other methods, but here it is:
  1.  
  2. #==============================================================================
  3. # ** Sub_Map
  4. #------------------------------------------------------------------------------
  5. #  Script by            :   Plague180
  6. #  Last Update          :   April 26, 2010
  7. #  Version              :   1.0
  8. #  Contact Information  :   plague180(http://www.hbgames.org)
  9. #  Contact Information  :   [email hidden]
  10. #------------------------------------------------------------------------------
  11. # May 9, 2010 - Version 1.0 Release
  12. #------------------------------------------------------------------------------
  13.  
  14. class Sub_Map < World_Map
  15.   #--------------------------------------------------------------------------
  16.   # * Object Initialization
  17.   #--------------------------------------------------------------------------
  18.   def initialize(upper="")
  19.     super()
  20.     @section = []
  21.     self.contents = Bitmap.new(640-32, 480-32)
  22.     self.z = 2010
  23.     self.opacity = 0
  24.     @upper = upper
  25.     @draw_player = true
  26.     @start = $game_map.map_id
  27.     @pindex = first_check
  28.     @cursor = Sprite_Cursor.new()
  29.     @cursor.bitmap = RPG::Cache.icon(Map_Items::CURSOR)
  30.     @cursor.z = 2020
  31.     if Map_Items::CURSOR_TYPE == 2
  32.      @cursor.x = Sub::Towns[@pindex].screen_x + RPG::Cache.icon(Sub::Towns[@pindex].icon).width/4
  33.      @cursor.y = Sub::Towns[@pindex].screen_y + RPG::Cache.icon(Sub::Towns[@pindex].icon).height
  34.      @cursor.z = 2020
  35.    end  
  36.   if second_check != true
  37.     @draw_player = false
  38.   end  
  39.   make_section
  40.   refresh
  41. end
  42.   #--------------------------------------------------------------------------
  43.   # * find map to start on(for cursor)
  44.   #--------------------------------------------------------------------------
  45.   def make_section
  46.     for i in 1...Sub::Towns.size+1
  47.       if Sub::Towns[i].upper_level == @upper
  48.         @section.push(Sub::Towns[i])
  49.         print(@section.size.to_s)
  50.       end  
  51.     end  
  52.   end
  53.   #--------------------------------------------------------------------------
  54.   # * find map to start on(for cursor)
  55.   #--------------------------------------------------------------------------
  56.   def first_check
  57.     for i in 1...Sub::Towns.size+1
  58.       if $game_switches[Sub::Towns[i].switch] == true
  59.         return i
  60.       end
  61.     end  
  62.   end
  63.   #--------------------------------------------------------------------------
  64.   # * Check to see if the player is on a valid map(inside of Sub::Towns)
  65.   #--------------------------------------------------------------------------
  66.   def second_check
  67.     for i in 1...Sub::Towns.size+1
  68.       if Sub::Towns[i].map_id == $game_map.map_id
  69.         if $game_switches[Sub::Towns[i].switch] == true
  70.           return true
  71.         end  
  72.       end  
  73.     end  
  74.   end
  75.   #--------------------------------------------------------------------------
  76.   # * Move Player To New Map
  77.   #--------------------------------------------------------------------------
  78.   def move(index=0)
  79.       $game_system.se_play(Map_Items::MOVE_SE)
  80.       if Map_Items::CURSOR_TYPE == 1
  81.         index = @pindex
  82.       end
  83.       Audio.bgm_fade(800)
  84.       Audio.bgs_fade(800)
  85.       Audio.me_fade(800)
  86.       $game_temp.player_new_map_id = Sub::Towns[index].map_id
  87.       $game_temp.player_new_x = Sub::Towns[index].map_x
  88.       $game_temp.player_new_y = Sub::Towns[index].map_y
  89.       $game_temp.player_new_direction = Sub::Towns[index].direction
  90.       $game_temp.player_transferring = true
  91.       $game_map.autoplay
  92.       $scene = Scene_Map.new
  93.   end
  94.   #--------------------------------------------------------------------------
  95.   # * Check if can move(and do it if you can)
  96.   #--------------------------------------------------------------------------
  97.   def move_if_can
  98.     for i in 1...Sub::Towns.size+1
  99.       if @cursor.x > Sub::Towns[i].screen_x-Map_Items::PIXEL_OFFSET && @cursor.x < Sub::Towns[i].screen_x + RPG::Cache.icon(Sub::Towns[i].icon).width + Map_Items::PIXEL_OFFSET
  100.         if @cursor.y > Sub::Towns[i].screen_y-Map_Items::PIXEL_OFFSET && @cursor.y < Sub::Towns[i].screen_y + RPG::Cache.icon(Sub::Towns[i].icon).height + Map_Items::PIXEL_OFFSET
  101.           move(i)
  102.         end
  103.       end
  104.     end  
  105.   end  
  106.   #--------------------------------------------------------------------------
  107.   # * Check if you can click on town, if so show name
  108.   #--------------------------------------------------------------------------
  109.   def check_name
  110.       for i in 1...Sub::Towns.size+1
  111.       if @cursor.x > Sub::Towns[i].screen_x - Map_Items::PIXEL_OFFSET && @cursor.x < Sub::Towns[i].screen_x + RPG::Cache.icon(Sub::Towns[i].icon).width + Map_Items::PIXEL_OFFSET
  112.         if @cursor.y > Sub::Towns[i].screen_y - Map_Items::PIXEL_OFFSET && @cursor.y < Sub::Towns[i].screen_y + RPG::Cache.icon(Sub::Towns[i].icon).height + Map_Items::PIXEL_OFFSET
  113.           self.contents.clear
  114.           refresh
  115.           self.contents.font.size = 18
  116.           self.contents.font.color = text_color(Map_Items::TOWN_TEXT_COLOR)
  117.           cx = contents.text_size(Sub::Towns[i].town_name).width
  118.           if $game_switches[Sub::Towns[i].switch] == true #unlocked?
  119.             self.contents.draw_text(@cursor.x-cx/2,@cursor.y,cx,32,Sub::Towns[i].town_name)
  120.           end  
  121.         end
  122.       end
  123.     end
  124.   end  
  125.   #--------------------------------------------------------------------------
  126.   # * Check if you can click on town, if so show name window
  127.   #--------------------------------------------------------------------------
  128.   def check_name_window
  129.     for i in 1...Sub::Towns.size+1
  130.       if @cursor.x > Sub::Towns[i].screen_x - Map_Items::PIXEL_OFFSET && @cursor.x < Sub::Towns[i].screen_x + RPG::Cache.icon(Sub::Towns[i].icon).width + Map_Items::PIXEL_OFFSET
  131.         if @cursor.y > Sub::Towns[i].screen_y-Map_Items::PIXEL_OFFSET && @cursor.y < Sub::Towns[i].screen_y + RPG::Cache.icon(Sub::Towns[i].icon).height + Map_Items::PIXEL_OFFSET
  132.           if $game_switches[Sub::Towns[i].switch] == true #unlocked?
  133.             return Sub::Towns[i].town_name
  134.           end
  135.         end  
  136.       end
  137.     end
  138.     return ""
  139.   end
  140.   #--------------------------------------------------------------------------
  141.   # * Move Cursor Right
  142.   #--------------------------------------------------------------------------
  143.   def move_cursor_right
  144.       @pindex += 1
  145.       if @pindex > Sub::Towns.size
  146.         @pindex = 1
  147.       end
  148.       for i in 1...Sub::Towns.size+1
  149.         if $game_switches[Sub::Towns[@pindex].switch] == false
  150.           @pindex += 1
  151.           if @pindex > Sub::Towns.size
  152.             @pindex = 1
  153.           end
  154.         end
  155.       end
  156.      refresh
  157.    end
  158.   #--------------------------------------------------------------------------
  159.   # * Move Cursor Left
  160.   #--------------------------------------------------------------------------
  161.   def move_cursor_left
  162.       @pindex -= 1
  163.       if @pindex < 1
  164.         @pindex = Sub::Towns.size
  165.       end
  166.       for i in 1...Sub::Towns.size+1
  167.         if $game_switches[Sub::Towns[@pindex].switch] == false
  168.           @pindex -= 1
  169.            if @pindex < 1
  170.              @pindex = Sub::Towns.size
  171.            end
  172.         end
  173.       end
  174.       refresh
  175.   end  
  176.   #--------------------------------------------------------------------------
  177.   # * Move Cursor Up
  178.   #--------------------------------------------------------------------------
  179.   def move_cursor_up
  180.       @pindex += 1
  181.       if @pindex > Sub::Towns.size
  182.         @pindex = 1
  183.       end
  184.       for i in 1...Sub::Towns.size+1
  185.         if $game_switches[Sub::Towns[@pindex].switch] == false
  186.           @pindex += 1
  187.           if @pindex > Sub::Towns.size
  188.             @pindex = 1
  189.           end
  190.         end
  191.       end
  192.      refresh
  193.   end
  194.   #--------------------------------------------------------------------------
  195.   # * Move Cursor Down
  196.   #--------------------------------------------------------------------------
  197.   def move_cursor_down
  198.       @pindex -= 1
  199.       if @pindex < 1
  200.         @pindex = Sub::Towns.size
  201.       end
  202.       for i in 1...Sub::Towns.size+1
  203.         if $game_switches[Sub::Towns[@pindex].switch] == false
  204.           @pindex -= 1
  205.            if @pindex < 1
  206.              @pindex = Sub::Towns.size
  207.            end
  208.         end
  209.       end
  210.       refresh
  211.   end
  212.   #--------------------------------------------------------------------------
  213.   # * Draw Player
  214.   #--------------------------------------------------------------------------  
  215.   def draw_player
  216.       bitmap = RPG::Cache.icon(Map_Items::PLAYER_ICON)
  217.       @player_offset_x = Sub::Towns[@start].screen_x + ((RPG::Cache.icon(Sub::Towns[@start].icon).width / 2) - (bitmap.width / 2))
  218.       @player_offset_y = Sub::Towns[@start].screen_y - bitmap.height
  219.       self.contents.blt(@player_offset_x,@player_offset_y, bitmap, Rect.new(0,0,RPG::Cache.icon(Map_Items::PLAYER_ICON).height,RPG::Cache.icon(Map_Items::PLAYER_ICON).width))
  220.   end
  221.   #--------------------------------------------------------------------------
  222.   # * Draw Towns
  223.   #--------------------------------------------------------------------------
  224.   def draw_towns
  225.     for i in [email hidden]
  226.       if $game_switches[@section[i].switch] == true #unlocked?
  227.         bitmap = RPG::Cache.icon(@section[i].icon)
  228.         self.contents.blt(@section[i].screen_x, @section[i].screen_y, bitmap, Rect.new(0,0,bitmap.width,bitmap.height))
  229.       end
  230.     end
  231.   end
  232.   #--------------------------------------------------------------------------
  233.   # * Draw Cursor(i know this could be done better, will fix in update)
  234.   #   been waiting too long to release this.
  235.   #--------------------------------------------------------------------------
  236.   def draw_cursor
  237.      if Map_Items::CURSOR_TYPE == 1
  238.         bitmap = @cursor.bitmap
  239.         if $game_switches[Sub::Towns[@pindex].switch] == true
  240.           if Sub::Towns[@pindex].icon != nil
  241.             x = Sub::Towns[@pindex].screen_x + (RPG::Cache.icon(Sub::Towns[@pindex].icon).width/2)-(bitmap.width/2)
  242.             y = Sub::Towns[@pindex].screen_y + (RPG::Cache.icon(Sub::Towns[@pindex].icon).height+3)
  243.           end
  244.         if Sub::Towns[@pindex].icon != nil
  245.           self.contents.blt(x, y, bitmap, Rect.new(0,0,bitmap.height,bitmap.width))
  246.         else
  247.           self.contents.blt(Sub::Towns[@pindex].screen_x, Sub::Towns[@pindex].screen_y+bitmap.width, bitmap, Rect.new(0,0,bitmap.height,bitmap.width))
  248.         end
  249.         if Map_Items::NAME_STYLE == 1
  250.           self.contents.font.size = 18
  251.           cx = contents.text_size(Sub::Towns[@pindex].town_name).width
  252.           x = Sub::Towns[@pindex].screen_x+(RPG::Cache.icon(Sub::Towns[@pindex].icon).width/2)-cx/2
  253.           self.contents.font.color = text_color(Map_Items::TOWN_TEXT_COLOR)
  254.           self.contents.draw_text(x,y,cx,32,Sub::Towns[@pindex].town_name)
  255.         end
  256.        end
  257.       end
  258.       if Map_Items::CURSOR_TYPE == 2
  259.          bitmap = @cursor.bitmap
  260.          self.contents.blt(@cursor.x, @cursor.y, bitmap, Rect.new(0,0,bitmap.height,bitmap.width))
  261.       end  
  262.     end
  263.   #--------------------------------------------------------------------------
  264.   # * Check cursor bounds so it doesnt go off screen
  265.   #--------------------------------------------------------------------------
  266.   def check_cursor_bounds
  267.      if @cursor.x < 0
  268.        @cursor.x = 0
  269.      end
  270.      if @cursor.x > 640 - RPG::Cache.icon(Map_Items::CURSOR).width - 32
  271.        @cursor.x = 640 - RPG::Cache.icon(Map_Items::CURSOR).width - 32
  272.      end  
  273.      if @cursor.y < 0
  274.        @cursor.y = 0
  275.      end
  276.      if @cursor.y > 480 - RPG::Cache.icon(Map_Items::CURSOR).height - 32
  277.        @cursor.y = 480 - RPG::Cache.icon(Map_Items::CURSOR).height - 32
  278.      end
  279.   end  
  280.   #--------------------------------------------------------------------------
  281.   # * Refresh
  282.   #--------------------------------------------------------------------------
  283.   def refresh
  284.     self.contents.clear
  285.     check_cursor_bounds
  286.     #draws player
  287.     if Map_Items::SHOW_PLAYER == true
  288.       if @draw_player == true
  289.         draw_player
  290.       end  
  291.     end
  292.     #draws all towns
  293.     draw_towns
  294.     #draw cursor
  295.     draw_cursor
  296.    
  297.   end
  298. end #class
Expand to see the code.

please ignore any mistakes in comments, working on getting this last bug out for release :)
so....? any advice anyone?
It's weird you get the error only in make_section. initialize calls first_check before it calls make_section, and in line 57 first_check also refers to Sub::Towns.size
if Sub::Towns is nil, why don't you get an error on line 57?
Anyway, if the problem is doing .size on array set to nil, create a method (in Sub class) called self.towns_size that returns 0 when Towns is nil, and Towns.size otherwise.
Also, shouldn't you call make_section before calling first_check?
sub::Towns isnt nil it has 6 objects. i agree doing first_check first is smart, but doesnt effect the code in question. first check just finds if the town i start on is valid(because the overall script is a world_map script obviously and if the script only knows maps 1-7 and your on 8 it needs to know how to process this) hmmm, shall continue to play with code(moving make_section around and see if that helps, eta)
after aton of time spent researching, i think that the problem im getting is because of the class i inherit from because it calls refresh of the parent class before it calls make_section. so thats why it doesnt think that my array has any items, it hasent been filled yet. im switching over to Window_Base just as the parent class and gonna change everything and see if that helps, so far it seems to be working :( i feel so dumb ive only been tracing the last four moves before it crashed..going back 8 shows me the problem.

edit: tho that does bring up another issue. should i be able to do this: "@section[1].town_name" because it doesnt work... i hopw im just missing something
so we agree this is correct?
  1.   #--------------------------------------------------------------------------
  2.    # * find map to start on(for cursor)
  3.    #--------------------------------------------------------------------------
  4.    def make_section
  5.      for i in 1...Sub::Towns.size+1
  6.        if Sub::Towns[i].upper_level == @upper
  7.          @section.push(Sub::Towns[i])
  8.        end  
  9.      end  
  10.    end
Expand to see the code.

but how would i access them?
if the norm is Sub::Towns[index].town_name
wouldnt the new format be: @section[index].town_name
that would be my thought. my next thought is
@section[Sub::Towns.town_name] but i know that logic is wrong, best case scenario it gives me the wrong index
but i assume it wont work anyways. btw im rebuilding the script i lost it. but if u need it let me know. im close to having it back to where it was (all 2000 lines, i know u posted on my rant post. so far its only taken about 10 hours. plus i left some of the hard part in a email to my freind for help :))
Actually, you need Sub::Towns.size, not Sub::Towns.size + 1. This is because the size will actually be 0 when there are no elements in an array, and will be 1 with one element, 2, with two elements, etc. With a for loop, it actually goes from the first number to one before the last number, when you feed it a range as the second argument. So for i in 0...10 will cycle through the loop ten times, where i starts out as 0 in the first iteration, and ends up as 9 in the last iteration.
that still doesnt fix the problem(and i use "+1" by habit because more then half my script i need such a loop) im still confused as to how to use the objects i put in the new array

edit: glitch u posted on my other post about this script im working on :) if i can start getting use out of this code, my script will be done :) i so cant wait to show u all(allthough my graphics for demo suck, anyone know a quick artist?)
want me to just upload a demo? it all works except for sub sections(well they work, just not right)so u can see what my problem is
Please do. I'll take a look at it too. Your problem is still the "undefined method 'size' for nil:NilClass" error, right?
yes im still getting that error, when i use the new code(even though ive proven it has 3 objects when its created) anyways im not 100% sure ive added everything back since the script crashed but it seems to be working stable(just the subsections dont work(properly), because i havent converted it to use the new code because i cant get draw_towns to work when i do) if u have any questions let me know, this is my last problem :) http://www.megaupload.com/?d=MCRQ8H0O

p.s. please note that when u load it there are 6 items in the subsection of map 1 or 2, there should only be 3 for each, thats why im trying to pull pieces out of the array
so did that help silver?
Sorry, megaupload didn't let me download it until yesterday. The demo doesn't give any errors... what's the code subsections should be using / what code gives you the error?
i have it set up my way with no errors(except the fact that it show 6 towns instead of 3 on the sub sections), i was hoping you could help me get the method draw_towns in sub_map to work by using @section instead of Sub::Towns, once i see one method working right, im sure i can figure the rest out, i have had it working at one point before i lost all the code, but when i used @section[1].icon it didnt crash, but it didnt show the icon ether, when i replaced that same peice of code with Sub::Towns[1].icon it worked properly.
here is my code trying to get draw_towns to work

  1. #==============================================================================
  2. # ** Sub_Map
  3. #------------------------------------------------------------------------------
  4. #  Script by            :   Plague180
  5. #  Last Update          :   April 26, 2010
  6. #  Version              :   1.0
  7. #  Contact Information  :   plague180(http://www.hbgames.org)
  8. #  Contact Information  :   [email hidden]
  9. #------------------------------------------------------------------------------
  10. # May 9, 2010 - Version 1.0 Release
  11. #------------------------------------------------------------------------------
  12.  
  13. class Sub_Map < Window_Base
  14.   #--------------------------------------------------------------------------
  15.   # * Object Initialization
  16.   #--------------------------------------------------------------------------
  17.   def initialize(index=$game_map.map_id)
  18.     super(0, 0, 640, 480)
  19.     self.contents = Bitmap.new(640-32, 480-32)
  20.     self.z = 2010
  21.     self.opacity = 0
  22.     @section = []
  23.     make_section
  24.     @draw_player = true
  25.     @start = index
  26.     @pindex = first_check
  27.     @cursor = Sprite_Cursor.new()
  28.     @cursor.bitmap = RPG::Cache.icon(Map_Items::CURSOR)
  29.     @cursor.z = 2020
  30.     if Map_Items::CURSOR_TYPE == 2
  31.      @cursor.x = Sub::Towns[@pindex].screen_x + RPG::Cache.icon(Sub::Towns[@pindex].icon).width/4
  32.      @cursor.y = Sub::Towns[@pindex].screen_y + RPG::Cache.icon(Sub::Towns[@pindex].icon).height
  33.      @cursor.z = 2020
  34.    end  
  35.   if second_check != true
  36.     @draw_player = false
  37.   end  
  38.   refresh
  39. end
  40.    #--------------------------------------------------------------------------
  41.    # * find map to start on(for cursor)
  42.    #--------------------------------------------------------------------------
  43.    def make_section
  44.      for i in 1...Sub::Towns.size+1
  45.        if Sub::Towns[i].upper_level == @upper
  46.          @section.push(Sub::Towns[i])
  47.          print(@section.size.to_s)
  48.        end  
  49.      end  
  50.    end
  51.   #--------------------------------------------------------------------------
  52.   # * Return value of @pindex
  53.   #--------------------------------------------------------------------------
  54.   def pindex
  55.     return @pindex
  56.   end  
  57.   #--------------------------------------------------------------------------
  58.   # * Update the value of @pindex so the name box knows what name to disply
  59.   #--------------------------------------------------------------------------
  60.   def update_pindex
  61.     @cursor.x, @cursor.y = Mouse.mouse_pos
  62.   end  
  63.   #--------------------------------------------------------------------------
  64.   # * Set the value of @cursor.x
  65.   #--------------------------------------------------------------------------
  66.   def set_x(set)
  67.     @cursor.x = set
  68.   end
  69.   #--------------------------------------------------------------------------
  70.   # * Set the value of @cursor.x
  71.   #--------------------------------------------------------------------------
  72.   def set_y(set)
  73.     @cursor.y = set
  74.   end
  75.   #--------------------------------------------------------------------------
  76.   # * find map to start on(for cursor)
  77.   #--------------------------------------------------------------------------
  78.   def first_check
  79.     for i in 1...Sub::Towns.size+1
  80.       if $game_switches[Sub::Towns[i].switch] == true
  81.         return i
  82.       end
  83.     end  
  84.   end
  85.   #--------------------------------------------------------------------------
  86.   # * Check to see if the player is on a valid map(inside of Sub::Towns)
  87.   #--------------------------------------------------------------------------
  88.   def second_check
  89.     for i in 1...Sub::Towns.size+1
  90.       if Sub::Towns[i].map_id == $game_map.map_id
  91.         if $game_switches[Sub::Towns[i].switch] == true
  92.           return true
  93.         end  
  94.       end  
  95.     end  
  96.   end
  97.   #--------------------------------------------------------------------------
  98.   # * Move Player To New Map
  99.   #--------------------------------------------------------------------------
  100.   def move(index=0)
  101.       $game_system.se_play(Map_Items::MOVE_SE)
  102.       if Map_Items::CURSOR_TYPE == 1
  103.         index = @pindex
  104.       end
  105.       Audio.bgm_fade(800)
  106.       Audio.bgs_fade(800)
  107.       Audio.me_fade(800)
  108.       $game_temp.player_new_map_id = Sub::Towns[index].map_id
  109.       $game_temp.player_new_x = Sub::Towns[index].map_x
  110.       $game_temp.player_new_y = Sub::Towns[index].map_y
  111.       $game_temp.player_new_direction = Sub::Towns[index].direction
  112.       $game_temp.player_transferring = true
  113.       $game_map.autoplay
  114.       $scene = Scene_Map.new
  115.   end
  116.   #--------------------------------------------------------------------------
  117.   # * Check if can move(and do it if you can)
  118.   #--------------------------------------------------------------------------
  119.   def move_if_can
  120.     for i in 1...Sub::Towns.size+1
  121.       if @cursor.x > Sub::Towns[i].screen_x-Map_Items::PIXEL_OFFSET && @cursor.x < Sub::Towns[i].screen_x + RPG::Cache.icon(Sub::Towns[i].icon).width + Map_Items::PIXEL_OFFSET
  122.         if @cursor.y > Sub::Towns[i].screen_y-Map_Items::PIXEL_OFFSET && @cursor.y < Sub::Towns[i].screen_y + RPG::Cache.icon(Sub::Towns[i].icon).height + Map_Items::PIXEL_OFFSET
  123.           move(i)
  124.         end
  125.       end
  126.     end  
  127.   end  
  128.   #--------------------------------------------------------------------------
  129.   # * Check if you can click on town, if so show name
  130.   #--------------------------------------------------------------------------
  131.   def check_name
  132.       for i in 1...Sub::Towns.size+1
  133.       if @cursor.x > Sub::Towns[i].screen_x - Map_Items::PIXEL_OFFSET && @cursor.x < Sub::Towns[i].screen_x + RPG::Cache.icon(Sub::Towns[i].icon).width + Map_Items::PIXEL_OFFSET
  134.         if @cursor.y > Sub::Towns[i].screen_y - Map_Items::PIXEL_OFFSET && @cursor.y < Sub::Towns[i].screen_y + RPG::Cache.icon(Sub::Towns[i].icon).height + Map_Items::PIXEL_OFFSET
  135.           self.contents.clear
  136.           refresh
  137.           self.contents.font.size = 18
  138.           self.contents.font.color = text_color(Map_Items::TOWN_TEXT_COLOR)
  139.           cx = contents.text_size(Sub::Towns[i].town_name).width
  140.           if $game_switches[Sub::Towns[i].switch] == true #unlocked?
  141.             self.contents.draw_text(@cursor.x-cx/2,@cursor.y,cx,32,Sub::Towns[i].town_name)
  142.           end  
  143.         end
  144.       end
  145.     end
  146.   end  
  147.   #--------------------------------------------------------------------------
  148.   # * Check if you can click on town, if so show name window
  149.   #--------------------------------------------------------------------------
  150.   def check_name_window
  151.     for i in 1...Sub::Towns.size+1
  152.       if @cursor.x > Sub::Towns[i].screen_x - Map_Items::PIXEL_OFFSET && @cursor.x < Sub::Towns[i].screen_x + RPG::Cache.icon(Sub::Towns[i].icon).width + Map_Items::PIXEL_OFFSET
  153.         if @cursor.y > Sub::Towns[i].screen_y-Map_Items::PIXEL_OFFSET && @cursor.y < Sub::Towns[i].screen_y + RPG::Cache.icon(Sub::Towns[i].icon).height + Map_Items::PIXEL_OFFSET
  154.           if $game_switches[Sub::Towns[i].switch] == true #unlocked?
  155.             return Sub::Towns[i].town_name
  156.           end
  157.         end  
  158.       end
  159.     end
  160.     return ""
  161.   end
  162.   #--------------------------------------------------------------------------
  163.   # * Move Cursor Right
  164.   #--------------------------------------------------------------------------
  165.   def move_cursor_right_one
  166.      @cursor.x += Map_Items::MOVE_PIXEL
  167.      refresh
  168.   end
  169.   #--------------------------------------------------------------------------
  170.   # * Move Cursor Left
  171.   #--------------------------------------------------------------------------
  172.   def move_cursor_left_one
  173.      @cursor.x -= Map_Items::MOVE_PIXEL
  174.      refresh
  175.   end
  176.   #--------------------------------------------------------------------------
  177.   # * Move Cursor Up
  178.   #--------------------------------------------------------------------------
  179.   def move_cursor_up_one
  180.      @cursor.y -= Map_Items::MOVE_PIXEL
  181.      refresh
  182.   end
  183.   #--------------------------------------------------------------------------
  184.   # * Move Cursor Down
  185.   #--------------------------------------------------------------------------
  186.   def move_cursor_down_one
  187.      @cursor.y += Map_Items::MOVE_PIXEL
  188.      refresh
  189.   end
  190.   #--------------------------------------------------------------------------
  191.   # * Move Cursor Right
  192.   #--------------------------------------------------------------------------
  193.   def move_cursor_right
  194.       @pindex += 1
  195.       if @pindex > Sub::Towns.size
  196.         @pindex = 1
  197.       end
  198.       for i in 1...Sub::Towns.size+1
  199.         if $game_switches[Sub::Towns[@pindex].switch] == false
  200.           @pindex += 1
  201.           if @pindex > Sub::Towns.size
  202.             @pindex = 1
  203.           end
  204.         end
  205.       end
  206.      refresh
  207.    end
  208.   #--------------------------------------------------------------------------
  209.   # * Move Cursor Left
  210.   #--------------------------------------------------------------------------
  211.   def move_cursor_left
  212.       @pindex -= 1
  213.       if @pindex < 1
  214.         @pindex = Sub::Towns.size
  215.       end
  216.       for i in 1...Sub::Towns.size+1
  217.         if $game_switches[Sub::Towns[@pindex].switch] == false
  218.           @pindex -= 1
  219.            if @pindex < 1
  220.              @pindex = Sub::Towns.size
  221.            end
  222.         end
  223.       end
  224.       refresh
  225.   end  
  226.   #--------------------------------------------------------------------------
  227.   # * Move Cursor Up
  228.   #--------------------------------------------------------------------------
  229.   def move_cursor_up
  230.       @pindex += 1
  231.       if @pindex > Sub::Towns.size
  232.         @pindex = 1
  233.       end
  234.       for i in 1...Sub::Towns.size+1
  235.         if $game_switches[Sub::Towns[@pindex].switch] == false
  236.           @pindex += 1
  237.           if @pindex > Sub::Towns.size
  238.             @pindex = 1
  239.           end
  240.         end
  241.       end
  242.      refresh
  243.   end
  244.   #--------------------------------------------------------------------------
  245.   # * Move Cursor Down
  246.   #--------------------------------------------------------------------------
  247.   def move_cursor_down
  248.       @pindex -= 1
  249.       if @pindex < 1
  250.         @pindex = Sub::Towns.size
  251.       end
  252.       for i in 1...Sub::Towns.size+1
  253.         if $game_switches[Sub::Towns[@pindex].switch] == false
  254.           @pindex -= 1
  255.            if @pindex < 1
  256.              @pindex = Sub::Towns.size
  257.            end
  258.         end
  259.       end
  260.       refresh
  261.   end
  262.   #--------------------------------------------------------------------------
  263.   # * Draw Player
  264.   #--------------------------------------------------------------------------  
  265.   def draw_player
  266.       bitmap = RPG::Cache.icon(Map_Items::PLAYER_ICON)
  267.       @player_offset_x = Sub::Towns[@start].screen_x + ((RPG::Cache.icon(Sub::Towns[@start].icon).width / 2) - (bitmap.width / 2))
  268.       @player_offset_y = Sub::Towns[@start].screen_y - bitmap.height
  269.       self.contents.blt(@player_offset_x,@player_offset_y, bitmap, Rect.new(0,0,RPG::Cache.icon(Map_Items::PLAYER_ICON).height,RPG::Cache.icon(Map_Items::PLAYER_ICON).width))
  270.   end
  271.   #--------------------------------------------------------------------------
  272.   # * Draw Towns
  273.   #--------------------------------------------------------------------------
  274.   def draw_towns
  275.     for i in [email hidden]
  276.       if $game_switches[@section[i].switch] == true #unlocked?
  277.         bitmap = RPG::Cache.icon(@section[i].icon)
  278.         print(bitmap.to_s)
  279.         self.contents.blt(@section[i].screen_x, @section[i].screen_y, bitmap, Rect.new(0,0,bitmap.width,bitmap.height))
  280.       end
  281.     end
  282.   end
  283.   #--------------------------------------------------------------------------
  284.   # * Draw Cursor(i know this could be done better, will fix in update)
  285.   #   been waiting too long to release this.
  286.   #--------------------------------------------------------------------------
  287.   def draw_cursor
  288.      if Map_Items::CURSOR_TYPE == 1
  289.         bitmap = @cursor.bitmap
  290.         if $game_switches[Sub::Towns[@pindex].switch] == true
  291.           if Sub::Towns[@pindex].icon != nil
  292.             x = Sub::Towns[@pindex].screen_x + (RPG::Cache.icon(Sub::Towns[@pindex].icon).width/2)-(bitmap.width/2)
  293.             y = Sub::Towns[@pindex].screen_y + (RPG::Cache.icon(Sub::Towns[@pindex].icon).height+3)
  294.           end
  295.         if Sub::Towns[@pindex].icon != nil
  296.           self.contents.blt(x, y, bitmap, Rect.new(0,0,bitmap.height,bitmap.width))
  297.         else
  298.           self.contents.blt(Sub::Towns[@pindex].screen_x, Sub::Towns[@pindex].screen_y+bitmap.width, bitmap, Rect.new(0,0,bitmap.height,bitmap.width))
  299.         end
  300.         if Map_Items::NAME_STYLE == 1
  301.           self.contents.font.size = 18
  302.           cx = contents.text_size(Sub::Towns[@pindex].town_name).width
  303.           x = Sub::Towns[@pindex].screen_x+(RPG::Cache.icon(Sub::Towns[@pindex].icon).width/2)-cx/2
  304.           self.contents.font.color = text_color(Map_Items::TOWN_TEXT_COLOR)
  305.           self.contents.draw_text(x,y,cx,32,Sub::Towns[@pindex].town_name)
  306.         end
  307.        end
  308.       end
  309.       if Map_Items::CURSOR_TYPE == 2
  310.          bitmap = @cursor.bitmap
  311.          self.contents.blt(@cursor.x, @cursor.y, bitmap, Rect.new(0,0,bitmap.height,bitmap.width))
  312.       end  
  313.     end
  314.   #--------------------------------------------------------------------------
  315.   # * Check cursor bounds so it doesnt go off screen
  316.   #--------------------------------------------------------------------------
  317.   def check_cursor_bounds
  318.      if @cursor.x < 0
  319.        @cursor.x = 0
  320.      end
  321.      if @cursor.x > 640 - RPG::Cache.icon(Map_Items::CURSOR).width - 32
  322.        @cursor.x = 640 - RPG::Cache.icon(Map_Items::CURSOR).width - 32
  323.      end  
  324.      if @cursor.y < 0
  325.        @cursor.y = 0
  326.      end
  327.      if @cursor.y > 480 - RPG::Cache.icon(Map_Items::CURSOR).height - 32
  328.        @cursor.y = 480 - RPG::Cache.icon(Map_Items::CURSOR).height - 32
  329.      end
  330.   end  
  331.   #--------------------------------------------------------------------------
  332.   # * Refresh
  333.   #--------------------------------------------------------------------------
  334.   def refresh
  335.     self.contents.clear
  336.     check_cursor_bounds
  337.     #draws player
  338.     if Map_Items::SHOW_PLAYER == true
  339.       if @draw_player == true
  340.         draw_player
  341.       end  
  342.     end
  343.     #draws all towns
  344.     draw_towns
  345.     #draw cursor
  346.     draw_cursor
  347.   end
  348. end #class
Expand to see the code.


edit:i forgot since i lost all my code, to set @upper, will work on that and tell you my results
edit2:i think i have it all figured out, just have to play test throughly!!!! lol thanks for so much help, ill let you know if i run into anymore problems
great ^^ good luck finishing this up
i think i have it working 100%(unless more bugs are found) if u would like to be the first to play with it ill let you :) since you helped so much, again thank you!!!!!!!!! working on demo atm

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