Jump to content
New account registrations are disabed. This website is now an archive. Read more here.
  • 0
Sign in to follow this  
xxultimaxx

Setting Values for Element Script

Question

So I wanted to try my hand at making an Element script, but I am having a problem. Let me explain:

 

I created an event that calls

$scene = Scene_Gem.new

So, my scene runs... no problem there. Now, I am expecting certain values to be set when I enter Window_Gems and select the empty index; that is, for the values

@original_gem

and

@new_gem

to remain equaling zero. But, alas, this is not the case... I set

$game_variables[1] = @original_gem

and

$game_variables[2] = @new_gem

to determine the values, and for some reason, I get variable 1 = 9 and variable 2 = 4. I am pasting the script below, and if anyone could fill me in on the problem, that would be awesome! btw... I know the coding is hideous, but it is most definitely a WIP.

 

 

module RPG
 class Weapon
 alias old_initialize initialize
   def initialize
     old_initialize
     @gem_id = 0
   end
   attr_accessor :gem_id
 end
end

module Xx
 # {Item_id => [[element_set]]}
 Gems = {
 32 => [[1]]
 }
end

class Scene_Title
alias old_command_new_game command_new_game
 def command_new_game
   $data_weapons[1].gem_id = 0
   old_command_new_game
 end
end

class Window_Base < Window
 def draw_gem_name(weapon)
   self.contents.font.color = normal_color
   if weapon.gem_id > 0
     self.contents.draw_text(100, 0, 80, 32, weapon.gem_id.to_s)
   else
     self.contents.draw_text(100, 0, 80, 32, "None") 
   end
 end
end

class Window_EquippedGem < Window_Selectable
 def initialize(actor)
   super(320, 416, 320, 64)
   self.contents = Bitmap.new(width - 32, height - 32)
   @actor = actor
   self.index = 0
   self.active = true
   refresh
 end
 def refresh
   self.contents.clear
   @data = []
   @data.push($data_weapons[@actor.weapon_id])
   @item_max = @data.size
   self.contents.font.color = system_color
   self.contents.draw_text(4, 0, 92, 32, "Gem: ")
   draw_gem_name(@data[0])
 end
 def item
   return $data_weapons[@actor.weapon_id].gem_id
 end  
end

class Window_Weapon < Window_Base
 def initialize(actor)
   super(0, 416, 320, 64)
   self.contents = Bitmap.new(width - 32, height - 32)
   @actor = actor
   refresh
 end
 def refresh
   self.contents.clear
   @data = []
   @data.push($data_weapons[@actor.weapon_id])
   @item_max = @data.size
   self.contents.font.color = system_color
   self.contents.draw_text(4, 32 * 0, 92, 32, @actor.name)
   draw_item_name(@data[0], 92, 32 * 0)
 end
end

class Window_Gems < Window_Selectable
 def initialize
   super(0, 64, 640, 352)
   @column_max = 2
   refresh
   self.index = -1
   self.active = false
 end
 def item
     return @data[self.index]
 end  
 def refresh
   if self.contents != nil
     self.contents.dispose
     self.contents = nil
   end
   @data = []
   for i in 1...$data_items.size
     if Xx::Gems.keys.include?(i)
       if $game_party.item_number(i) > 0
         @data.push($data_items[i])
       end
     end
   end
   @item_max = @data.size
   if @item_max > 0
     self.contents = Bitmap.new(width - 32, row_max * 32)
     for i in 0...@item_max
       draw_item(i)
     end
   end
 end
 def draw_item(index)
   item = @data[index]
   case item
   when RPG::Item
     number = $game_party.item_number(item.id)
   end
   x = 4 + index % 2 * (288 + 32)
   y = index / 2 * 32
   rect = Rect.new(x, y, self.width / @column_max - 32, 32)
   self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
   bitmap = RPG::Cache.icon(item.icon_name)
   opacity = self.contents.font.color == normal_color ? 255 : 128
   self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
   self.contents.draw_text(x + 28, y, 212, 32, item.name, 0)
   self.contents.draw_text(x + 240, y, 16, 32, ":", 1)
   self.contents.draw_text(x + 256, y, 24, 32, number.to_s, 2)
 end
 def update_help
   @help_window.set_text(self.item == nil ? "" : self.item.description)
 end
end

class Scene_Gem
 def initialize(actor_index = 0, equip_index = 0)
   @actor_index = actor_index
   @equip_index = equip_index
 end
 def main
   @actor = $game_party.actors[@actor_index]
   @help_window = Window_Help.new
   @weapon_window = Window_Weapon.new(@actor)
   @gems_window = Window_Gems.new
   @equipped_gem_window = Window_EquippedGem.new(@actor)
   refresh
   Graphics.transition
   loop do
     Graphics.update
     Input.update
     update
     if $scene != self
       break
     end
   end
   Graphics.freeze
   @help_window.dispose
   @equipped_gem_window.dispose
   @weapon_window.dispose
   @gems_window.dispose
 end
 def refresh
   if @equipped_gem_window.active
     @original_gem = $data_weapons[@actor.weapon_id].gem_id
     $game_variables[1] = @original_gem
   end
   if @gems_window.active
     @new_gem = @gems_window.item.id
     $game_variables[2] = @new_gem
   end
 end
 def update
   @help_window.update
   @equipped_gem_window.update
   @weapon_window.update
   @gems_window.update
   refresh
   if @equipped_gem_window.active
     update_equipped_gem
     return
   end
   if @gems_window.active
     update_gems
     return
   end
 end
 def update_equipped_gem
   if Input.trigger?(Input::B)
     $game_system.se_play($data_system.cancel_se)
     $scene = Scene_Map.new
     return
   end
   if Input.trigger?(Input::C)
     $game_system.se_play($data_system.decision_se)
     @equipped_gem_window.active = false
     @gems_window.active = true
     @gems_window.index = 0
     return
   end
 end
 def update_gems
   if Input.trigger?(Input::B)
     $game_system.se_play($data_system.cancel_se)
     @equipped_gem_window.active = true
     @gems_window.active = false
     @gems_window.index = -1
     return
   end
   if Input.trigger?(Input::C)
     $game_system.se_play($data_system.equip_se)
     $data_weapons[@actor.weapon_id].gem_id = @new_gem.id
     if $data_weapons[@actor.weapon_id].gem_id != 0
       $game_party.gain_item(@original_gem, 1)
       $game_party.lose_item(@new_gem, 1)
     else
       $game_party.lose_item(@new_gem, 1)
     end
     @equipped_gem_window.active = true
     @gems_window.active = false
     @gems_window.index = -1
     @equipped_gem_window.refresh
     @gems_window.refresh
     return
   end
 end
end

 

 

Share this post


Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Okay, I have inserted comments in the code to hopefully make it more apparent what is going on with the script. I still do not have instructions or anything because it is not finished yet.

 

I would assume the @data array for Window_Gem would be empty, therefore returning zero, but that is not happening. If, for example, I rename item 32 in the database "Ruby" and add to the inventory, it returns 65 to gem_id when it is selected, not 32 as I would expect.

 

Here is the commented script:

 

module RPG
 class Weapon
 alias old_initialize initialize
   def initialize
     # Runs previous contents
     old_initialize
     # Sets gem_id value
     @gem_id = 0
   end
   # Adds gem_id to weapons
   attr_accessor :gem_id
 end
end

module Xx
 # Creates hash to specify item with an element
 # {Item_id => [[element_set]]}
 Gems = {
 32 => [[1]]
 }
end

class Scene_Title
alias old_command_new_game command_new_game
 def command_new_game
   # Sets gem_id for weapons
   $data_weapons[1].gem_id = 0
   # Runs previous content
   old_command_new_game
 end
end

class Window_Base < Window
 # Will eventually draw the weapon's attached gem name
 # Currently, draws weapon's gem_id value
 def draw_gem_name(weapon)
   # Defines font color
   self.contents.font.color = normal_color
   # Checks if weapon's gem_id is greater than zero
   if weapon.gem_id > 0
     # Draws gem_id value
     self.contents.draw_text(100, 0, 80, 32, weapon.gem_id.to_s)
   else
     # Draws "None" if gem_id == 0
     self.contents.draw_text(100, 0, 80, 32, "None") 
   end
 end
end

class Window_EquippedGem < Window_Selectable
 def initialize(actor)
   super(320, 416, 320, 64)
   # Creates window
   self.contents = Bitmap.new(width - 32, height - 32)
   # Sets actor
   @actor = actor
   # Sets index
   self.index = 0
   # Sets active
   self.active = true
   refresh
 end
 def refresh
   self.contents.clear
   # Creates array
   @data = []
   # Adds actor's weapon to array
   @data.push($data_weapons[@actor.weapon_id])
   # Sets font color
   self.contents.font.color = system_color
   # Draws "Gem:" in window
   self.contents.draw_text(4, 0, 92, 32, "Gem: ")
   # Retrieves gem_id currently, but will retrieve gem_name later
   draw_gem_name(@data[0])
 end
 def item
   # Returns actor's weapon gem_id
   return $data_weapons[@actor.weapon_id].gem_id
 end  
end

class Window_Weapon < Window_Base
 def initialize(actor)
   super(0, 416, 320, 64)
   # Creates window
   self.contents = Bitmap.new(width - 32, height - 32)
   # Sets actor
   @actor = actor
   refresh
 end
 def refresh
   self.contents.clear
   # Creates array
   @data = []
   # Adds actor's weapon to array
   @data.push($data_weapons[@actor.weapon_id])
   # Sets font color
   self.contents.font.color = system_color
   # Draws actor's name in window
   self.contents.draw_text(4, 32 * 0, 92, 32, @actor.name)
   # Draws actor's weapon name in window
   draw_item_name(@data[0], 92, 32 * 0)
 end
end

class Window_Gems < Window_Selectable
 def initialize
   super(0, 64, 640, 352)
   # Creates two columns
   @column_max = 2
   refresh
   # Sets index off window
   self.index = -1
   # Sets deactive
   self.active = false
 end
 def item
     # I think it returns the item info for the item selected in the window???
     return @data[self.index]
 end  
 def refresh
   if self.contents != nil
     # Clears all content???
     self.contents.dispose
     self.contents = nil
   end
   # Creates an array
   @data = []
   # Runs through items...
   for i in 1...$data_items.size
     # ... and determines if it is contained in the hash from module Xx
     if Xx::Gems.keys.include?(i)
       if $game_party.item_number(i) > 0
         # Adds item to array
         @data.push($data_items[i])
       end
     end
   end
   # Determines how many items are in array
   @item_max = @data.size
   if @item_max > 0
     self.contents = Bitmap.new(width - 32, row_max * 32)
     # Draws each item in array
     for i in 0...@item_max
       draw_item(i)
     end
   end
 end
 def draw_item(index)
   # Determines item in array
   item = @data[index]
   case item
   # Checks if item is an item
   when RPG::Item
     # Checks how many are in inventory
     number = $game_party.item_number(item.id)
   end
   # Sets draw parameters
   x = 4 + index % 2 * (288 + 32)
   y = index / 2 * 32
   rect = Rect.new(x, y, self.width / @column_max - 32, 32)
   self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
   bitmap = RPG::Cache.icon(item.icon_name)
   opacity = self.contents.font.color == normal_color ? 255 : 128
   self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
   # Draws item and amount in inventory
   self.contents.draw_text(x + 28, y, 212, 32, item.name, 0)
   self.contents.draw_text(x + 240, y, 16, 32, ":", 1)
   self.contents.draw_text(x + 256, y, 24, 32, number.to_s, 2)
 end
 def update_help
   # Puts description of item in help window
   @help_window.set_text(self.item == nil ? "" : self.item.description)
 end
end

class Scene_Gem
 def initialize(actor_index = 0, equip_index = 0)
   # Determines $game_party actor
   @actor_index = actor_index
 end
 def main
   # Sets actor
   @actor = $game_party.actors[@actor_index]
   # Creates help window
   @help_window = Window_Help.new
   # Creates weapon name window
   @weapon_window = Window_Weapon.new(@actor)
   # Creates gem window
   @gems_window = Window_Gems.new
   # Creates window showing gem_id currently, but gem_name later
   @equipped_gem_window = Window_EquippedGem.new(@actor)
   refresh
   Graphics.transition
   # Loop
   loop do
     Graphics.update
     Input.update
     update
     if $scene != self
       break
     end
   end
   Graphics.freeze
   # Disposes windows
   @help_window.dispose
   @equipped_gem_window.dispose
   @weapon_window.dispose
   @gems_window.dispose
 end
 def refresh
   # Checks which window is active
   if @equipped_gem_window.active
     # Sets equal to actor's weapon gem_id
     @original_gem = $data_weapons[@actor.weapon_id].gem_id
     # Check what is passed by pressing f9 during gameplay
     $game_variables[1] = @original_gem
   end
   if @gems_window.active
     # Sets equal to item_id for selected gem in window
     @new_gem = @gems_window.item.id
     # Check what is passed by pressing f9 during gameplay
     $game_variables[2] = @new_gem
   end
 end
 def update
   # Updates windows
   @help_window.update
   @equipped_gem_window.update
   @weapon_window.update
   @gems_window.update
   refresh
   if @equipped_gem_window.active
     update_equipped_gem
     return
   end
   if @gems_window.active
     update_gems
     return
   end
 end
 def update_equipped_gem
   # If cancelled
   if Input.trigger?(Input::B)
     $game_system.se_play($data_system.cancel_se)
     $scene = Scene_Map.new
     return
   end
   # If entered
   if Input.trigger?(Input::C)
     $game_system.se_play($data_system.decision_se)
     @equipped_gem_window.active = false
     @gems_window.active = true
     @gems_window.index = 0
     return
   end
 end
 def update_gems
   # If cancelled
   if Input.trigger?(Input::B)
     $game_system.se_play($data_system.cancel_se)
     @equipped_gem_window.active = true
     @gems_window.active = false
     @gems_window.index = -1
     return
   end
   # If entered
   if Input.trigger?(Input::C)
     $game_system.se_play($data_system.equip_se)
     # Set gem selected's item_id to weapon's gem_id 
     $data_weapons[@actor.weapon_id].gem_id = @new_gem.id
     # Decrease inventory of selected gem and increase inventory of previous gem
     if $data_weapons[@actor.weapon_id].gem_id != 0
       $game_party.gain_item(@original_gem, 1)
       $game_party.lose_item(@new_gem, 1)
     else
       # If no gem was previously attached
       $game_party.lose_item(@new_gem, 1)
     end
     @equipped_gem_window.active = true
     @gems_window.active = false
     @gems_window.index = -1
     # Refreshes content in windows
     @equipped_gem_window.refresh
     @gems_window.refresh
     return
   end
 end
end

 

 

 

I was able to get it to change to the right number if a gem is already attached through initialized assignment or selection assignment in Window_Gems by altering line 271 to this:

$data_weapons[@actor.weapon_id].gem_id = @new_gem

But, if the index is empty (no items to be drawn from module Xx), a selection causes the values for @original_gem and @new_gem to become 4. Why???

 

I was able to come up with something to fix it : )

Now, back to work...

Edited by Kiriashi

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...