Public Instance Variables

i am going to use a practical example to explain what my question is.

i get nilclass errors when trying to modify an instance variable from a window created in a scene.

i was under the impression that if you make an instance variable public, then it could be referenced by other scripts that created an instance of the class that initialized the instance variable?...

this is how i publicize instance variables:

#==============================================================================# ** Window_NoteInput#------------------------------------------------------------------------------# This window is displayed on the instrument scene and handles note# processing.#==============================================================================class Window_NoteInput < Window_Selectable #-------------------------------------------------------------------------- # * Public Instance Variables #-------------------------------------------------------------------------- attr_accessor :score # array of note inputs attr_accessor :note_index # current index of note input #-------------------------------------------------------------------------- # * Object Initialization #-------------------------------------------------------------------------- def initialize super(window_x, window_y, window_width, window_height) @score = [] @note_index = 0 refresh activate endafter creating an instance of Window_NoteInput,

this is how i try to reference them from my scene:

#--------------------------------------------------------------------------# * Process Note#--------------------------------------------------------------------------def process_note(note) Audio.se_play(note_audio(note), note_volume, note_pitch(note)) @score.push note @note_index += 1endwhy do i get nilclass errors?

this thread is not about the problem i am having, i am more concerned with focusing on the effective use of public instance variables and how they are called by scripts that utilize the classes that initialized the variable in the first place.
From your scene, you would do:


@note_window = Window_NoteInput.new


and later


@note_window.score.push(note)


@note_window.note_index += 1


In fact, it would be better to NOT make them attr_accessors at all, and to have a method within Window_NoteInput:


def add_note(note)


@score.push(note)


@note_index += 1


end


and to call @note_window.add_note(note) from your scene.
that just doesn't make sense to me...

i mean, it does...but why would you reference an instance variable in the same manner you would perform a method call?

wouldn't the scene inevitably perform a method call to a method which i haven't written, causing an error anyway? (which is why i believe you recommended creating a method def as a better way of doing things)?
I don't understand what you're asking.


The way you're trying to do it, the variables are properties of the window class, but you're trying to reference them in the scene class as if they were properties of the scene class. They aren't. You've still got to instantiate a local (to the scene) variable based on the window class, but that still doesn't make its properties belong to the scene itself - you've still got to refer to them as properties of the window.


And why move the logic from the scene class into the windows class? Encapsulation. Have changes to the variables belonging to that class IN that class.
i never learned through conventional means how to program Ruby, so some of the mechanics escape me. by reading the default scripts i was under the impression that the purpose of an instance variable was so that other classes could read and change them?

also, my original version of the script handles all of the processing within the window's class.

i am experimenting in hopes to better understand public instance variables, that is why i mentioned that i wasn't focusing on the problem i was experiencing. it isn't really a problem. my finished version will have all of the processing within the window's class.
(...) public instance variables (...)
Instance variables are always private. You can still breach that encapsulation with instance_eval and instance_variable_get.
Consider attr_accessor a macro for generating two methods (get+set) based on the argument 🙂score for example), not as a keyword which makes something public.


With this in mind read what Shaz wrote again.


*hugs*
Then if you want to keep doing it that way, my original reply stands.


You have to instantiate the window class into a variable inside your scene class, and then you use that variable to refer to the window class's variables.


You need to do this in your scene:


@myvar = Window.new


@myvar.variable = value


Look at the way the scenes in the default scripts access the variables of the windows they use, and how many of them change the variables directly, and how many use methods to change them.
i didn't show it in my example, but i did instantiate my window into my class. here is the method.

#-------------------------------------------------------------------------- # * Create Note Input Window #-------------------------------------------------------------------------- def create_note_input_window @note_input_window = Window_NoteInput.new @note_input_window.set_handler🙂cancel, method🙂return_scene)) endafter doing so, there is no problem accessing the array and pushing the note, however for some reason i get an argument error when i attempt to increase the note_index. any thoughts why that produces an error?

this is the new method.

#--------------------------------------------------------------------------# * Process Note#--------------------------------------------------------------------------def process_note(note) Audio.se_play(note_audio(note), note_volume, note_pitch(note)) @note_input_window.score.push note @note_input_window.note_index += 1endwhen processing @note_input.note_index += 1, the game returns an argument error (0 for 1).
I can't see how that line could possibly generate that error.


Can you provide your full script for both the window and the scene, and either a screenshot or the exact and full text of the error?


This line actually looks very suspicious to me:

Code:
Audio.se_play(note_audio(note), note_volume, note_pitch(note))
original version: (this is the main development, all processing is done within the window's class):

Spoiler
#==============================================================================# ** Instrument#------------------------------------------------------------------------------# This module defines all data associated with playable compositions. Data is# defined within constants as hash expressions / hash arrays.#==============================================================================module Instrument #-------------------------------------------------------------------------- # * Constants (General Settings) #-------------------------------------------------------------------------- MAX = 6 # max key input before clearing score VOLUME = 80 # Volume of note playback PITCH = {:LOW => 50, # Sets low pitch playback :MID => 100, # Sets standard pitch playback :HIG => 150} # Sets high pitch playback NOTESET = 'Noteset' # filename for graphics (system folder) #-------------------------------------------------------------------------- # * Constants (Note Input Processing) #-------------------------------------------------------------------------- # where KEYS is array of available input keys. # where DATA is [filename, index, line number, pitch] #-------------------------------------------------------------------------- KEYS = [😀OWN, :LEFT, :RIGHT, :UP, :A, :C] DATA = {😀OWN => ['Audio\SE\Cursor2', 0, 3, PITCH[:LOW]], :LEFT => ['Audio\SE\Cursor2', 1, 2, PITCH[:MID]], :RIGHT => ['Audio\SE\Cursor2', 2, 1, PITCH[:MID]], :UP => ['Audio\SE\Cursor2', 3, 0, PITCH[:HIG]], :A => ['Audio\SE\Cursor2', 4, 3, PITCH[:LOW]], # :B => ['Audio\SE\Cursor2', 5, 2, PITCH[:MID]], :C => ['Audio\SE\Cursor2', 6, 1, PITCH[:HIG]], :X => ['Audio\SE\Cursor2', 4, 3, PITCH[:LOW]], :Y => ['Audio\SE\Cursor2', 4, 2, PITCH[:MID]], :Z => ['Audio\SE\Cursor2', 4, 1, PITCH[:HIG]], :L => ['Audio\SE\Cursor2', 4, 0, PITCH[:HIG]], :R => ['Audio\SE\Cursor2', 4, 0, PITCH[:HIG]],} #-------------------------------------------------------------------------- # * Constants (Songs) #-------------------------------------------------------------------------- # where SONGS[symbol] is song symbol # where :name is string value of song # where :score is array of keys comprising song # where :event is common event to be called #-------------------------------------------------------------------------- SONGS = {} SONGS[:songofstorms] = {:name => 'Song of Storms', :score => [:LEFT, 😀OWN, :UP, :LEFT, 😀OWN, :UP], :event => 1 } SONGS[:songoftravel] = {:name => 'Song of Travel', :score => [:UP, :LEFT, :RIGHT, :UP, :LEFT, :RIGHT], :event => 2 } SONGS[:songofdeath] = {:name => 'Song of Death', :score => [:LEFT, :RIGHT, :A, :LEFT, :RIGHT, :A], :event => 3 }end#==============================================================================# ** Game_System#------------------------------------------------------------------------------# This class handles system data. It has been modified to save the disable# state of instruments. Instances of this class are referenced by $game_system.#==============================================================================class Game_System #-------------------------------------------------------------------------- # * Public Instance Variables #-------------------------------------------------------------------------- attr_accessor :instrument_disabled # instrument forbidden #-------------------------------------------------------------------------- # * Object Initialization #-------------------------------------------------------------------------- alias init_instrument_disabled initialize def initialize init_instrument_disabled @instrument_disabled = false endend#==============================================================================# ** Game_Party#------------------------------------------------------------------------------# This class handles parties. It has been modified to include the party's# acquired songs. Instances of this class are referenced by $game_party.#==============================================================================class Game_Party < Game_Unit #-------------------------------------------------------------------------- # * Public Instance Variables #-------------------------------------------------------------------------- attr_accessor = :songs # party's acquired songs #-------------------------------------------------------------------------- # * Object Initialization #-------------------------------------------------------------------------- alias init_party_songs initialize def initialize init_party_songs @songs = [] end def list_songs p @songs end #-------------------------------------------------------------------------- # * Add Song #-------------------------------------------------------------------------- def add_song(song_id) return unless song_exists?(song_id) return if song_acquired?(song_id) @songs.push song_id end #-------------------------------------------------------------------------- # * Determine if Song Exists #-------------------------------------------------------------------------- def song_exists?(song_id) return true if Instrument::SONGS.include?(song_id) end #-------------------------------------------------------------------------- # * Determine if Song Already Acquired #-------------------------------------------------------------------------- def song_acquired?(song_id) return true if @songs.include?(song_id) endend#==============================================================================# ** Scene_Map#------------------------------------------------------------------------------# This class performs the map screen processing. It has been modified to# include processing for calling an instrument to play. In turn, a constant has# been provided for specifying an Input key for instrument calling.#==============================================================================class Scene_Map < Scene_Base #-------------------------------------------------------------------------- # * Constants #-------------------------------------------------------------------------- INSTRUMENT = :SHIFT # calls instrument #-------------------------------------------------------------------------- # * Start Processing #-------------------------------------------------------------------------- alias init_instrument_calling start def start init_instrument_calling @instrument_calling = false end #-------------------------------------------------------------------------- # * Update Scene Transition #-------------------------------------------------------------------------- alias call_instrument_scene update_scene def update_scene call_instrument_scene update_call_instrument unless scene_changing? end #-------------------------------------------------------------------------- # * Determine if Instrument is Called due to Instrument Calling Key #-------------------------------------------------------------------------- def update_call_instrument if $game_system.instrument_disabled || $game_map.interpreter.running? @instrument_calling = false else @instrument_calling ||= Input.trigger?(INSTRUMENT) call_instrument if @instrument_calling && !$game_player.moving? end end #-------------------------------------------------------------------------- # * Call Instrument #-------------------------------------------------------------------------- def call_instrument Sound.play_ok SceneManager.call(Scene_Instrument) endend#==============================================================================# ** Window_NoteInput#------------------------------------------------------------------------------# This window is displayed on the instrument scene and handles note# processing.#==============================================================================class Window_NoteInput < Window_Selectable #-------------------------------------------------------------------------- # * Object Initialization #-------------------------------------------------------------------------- def initialize super(window_x, window_y, window_width, window_height) @note_index = 0 @score = [] refresh activate end #-------------------------------------------------------------------------- # * Get Window X-Coordinate #-------------------------------------------------------------------------- def window_x return Graphics.width / 8 end #-------------------------------------------------------------------------- # * Get Window Y-Coordinate #-------------------------------------------------------------------------- def window_y return Graphics.height / 2 - window_height / 2 end #-------------------------------------------------------------------------- # * Get Window Width #-------------------------------------------------------------------------- def window_width return Graphics.width - Graphics.width / 4 end #-------------------------------------------------------------------------- # * Get Window Height #-------------------------------------------------------------------------- def window_height return line_height * 6 end #-------------------------------------------------------------------------- # * Refresh #-------------------------------------------------------------------------- def refresh draw_all_horz_lines draw_all_notes unless @score.empty? end #-------------------------------------------------------------------------- # * Draw All Notes #-------------------------------------------------------------------------- def draw_all_notes @score.each_with_index do |note, index| draw_note(note_index(note), note_x(index), note_y(note)) end end #-------------------------------------------------------------------------- # * Draw Note #-------------------------------------------------------------------------- def draw_note(index, x, y) bitmap = Cache.system(Instrument::NOTESET) rect = Rect.new(index % 6 * 24, index / 6 * 24, 24, 24) contents.blt(x, y, bitmap, rect) end #-------------------------------------------------------------------------- # * Get Graphics Index for Drawing Note #-------------------------------------------------------------------------- def note_index(note) return Instrument:😀ATA[note][1] end #-------------------------------------------------------------------------- # * Get X-Coordinate for Drawing Note #-------------------------------------------------------------------------- def note_x(index) width = 24 return (index + 1) * (width + standard_padding) end #-------------------------------------------------------------------------- # * Get Y-Coordinate for Drawing Note #-------------------------------------------------------------------------- def note_y(note) return (Instrument:😀ATA[note][2] * line_height) + standard_padding end #-------------------------------------------------------------------------- # * Draw All Horizontal Lines #-------------------------------------------------------------------------- def draw_all_horz_lines hl = Instrument::KEYS.size + 1 hl.times {|hl| draw_horz_line(hl * line_height)} end #-------------------------------------------------------------------------- # * Draw Horizontal Line #-------------------------------------------------------------------------- def draw_horz_line(y) line_y = y + line_height / 2 - 1 contents.fill_rect(0, line_y, contents_width, 2, line_color) end #-------------------------------------------------------------------------- # * Get Color of Horizontal Line #-------------------------------------------------------------------------- def line_color color = normal_color color.alpha = 48 color end #-------------------------------------------------------------------------- # * Frame Update #-------------------------------------------------------------------------- def update super process_input unless score_complete? process_score if score_complete? end #-------------------------------------------------------------------------- # * Input Processing #-------------------------------------------------------------------------- def process_input return unless open? && active process_note🙂DOWN) if Input.trigger?🙂DOWN) process_note🙂LEFT) if Input.trigger?🙂LEFT) process_note🙂RIGHT) if Input.trigger?🙂RIGHT) process_note🙂UP) if Input.trigger?🙂UP) process_note🙂A) if Input.trigger?🙂A) process_note🙂C) if Input.trigger?🙂C) contents.clear refresh end #-------------------------------------------------------------------------- # * Process Note #-------------------------------------------------------------------------- def process_note(note) Audio.se_play(note_audio(note), note_volume, note_pitch(note)) @score.push note @note_index += 1 end #-------------------------------------------------------------------------- # * Get Note Audio File #-------------------------------------------------------------------------- def note_audio(note) return Instrument:😀ATA[note][0] end #-------------------------------------------------------------------------- # * Get Note Playback Volume #-------------------------------------------------------------------------- def note_volume return Instrument::VOLUME end #-------------------------------------------------------------------------- # * Get Note Pitch #-------------------------------------------------------------------------- def note_pitch(note) return Instrument:😀ATA[note][3] end #-------------------------------------------------------------------------- # * Process Score #-------------------------------------------------------------------------- def process_score process_song if song_played? clear_score if score_complete? && !song_played? end #-------------------------------------------------------------------------- # * Determine if Song Played #-------------------------------------------------------------------------- def song_played? Instrument::SONGS.each do |song_symbol| return true if song_symbol[1][:score] == @score end end #-------------------------------------------------------------------------- # * Process Song #-------------------------------------------------------------------------- def process_song song = Instrument::SONGS case @score when song[:songofstorms] ; $game_temp.reserve_common_event(song[:songofstorms][2]) when song[:songoftravel] ; $game_temp.reserve_common_event(song[:songoftravel][2]) when song[:songofdeath] ; $game_temp.reserve_common_event(song[:songofdeath][2]) end Graphics.wait(6) Sound.play_ok Graphics.wait(60) SceneManager.return end #-------------------------------------------------------------------------- # * Clear Score #-------------------------------------------------------------------------- def clear_score @note_index = 0 @score.clear Graphics.wait(6) Sound.play_buzzer Graphics.wait(60) contents.clear refresh end #-------------------------------------------------------------------------- # * Determine if Score is Complete #-------------------------------------------------------------------------- def score_complete? return true if @note_index == Instrument::MAX endend#==============================================================================# ** Scene_Instrument#------------------------------------------------------------------------------# This class performs song playing processing.#==============================================================================class Scene_Instrument < Scene_MenuBase #-------------------------------------------------------------------------- # * Start Processing #-------------------------------------------------------------------------- def start super create_note_input_window end #-------------------------------------------------------------------------- # * Create Note Input Window #-------------------------------------------------------------------------- def create_note_input_window @note_input_window = Window_NoteInput.new @note_input_window.set_handler🙂cancel, method🙂return_scene)) endend
modified version: (experimentation with instance variables, crashes when comparing the value of note_index to a constant)

Spoiler
#==============================================================================# ** Instrument#------------------------------------------------------------------------------# This module defines all data associated with playable compositions. Data is# defined within constants as hash expressions / hash arrays.#==============================================================================module Instrument #-------------------------------------------------------------------------- # * Constants (General Settings) #-------------------------------------------------------------------------- MAX = 6 # max key input before clearing score VOLUME = 80 # Volume of note playback PITCH = {:LOW => 50, # Sets low pitch playback :MID => 100, # Sets standard pitch playback :HIG => 150} # Sets high pitch playback NOTESET = 'Noteset' # filename for graphics (system folder) #-------------------------------------------------------------------------- # * Constants (Note Input Processing) #-------------------------------------------------------------------------- # where KEYS is array of available input keys. # where DATA is [filename, index, line number, pitch] #-------------------------------------------------------------------------- KEYS = [😀OWN, :LEFT, :RIGHT, :UP, :A, :C] DATA = {😀OWN => ['Audio\SE\Cursor2', 0, 3, PITCH[:LOW]], :LEFT => ['Audio\SE\Cursor2', 1, 2, PITCH[:MID]], :RIGHT => ['Audio\SE\Cursor2', 2, 1, PITCH[:MID]], :UP => ['Audio\SE\Cursor2', 3, 0, PITCH[:HIG]], :A => ['Audio\SE\Cursor2', 4, 3, PITCH[:LOW]], # :B => ['Audio\SE\Cursor2', 5, 2, PITCH[:MID]], :C => ['Audio\SE\Cursor2', 6, 1, PITCH[:HIG]], :X => ['Audio\SE\Cursor2', 4, 3, PITCH[:LOW]], :Y => ['Audio\SE\Cursor2', 4, 2, PITCH[:MID]], :Z => ['Audio\SE\Cursor2', 4, 1, PITCH[:HIG]], :L => ['Audio\SE\Cursor2', 4, 0, PITCH[:HIG]], :R => ['Audio\SE\Cursor2', 4, 0, PITCH[:HIG]],} #-------------------------------------------------------------------------- # * Constants (Songs) #-------------------------------------------------------------------------- # where SONGS[symbol] is song symbol # where :name is string value of song # where :score is array of keys comprising song # where :event is common event to be called #-------------------------------------------------------------------------- SONGS = {} SONGS[:songofstorms] = {:name => 'Song of Storms', :score => [:LEFT, 😀OWN, :UP, :LEFT, 😀OWN, :UP], :event => 1 } SONGS[:songoftravel] = {:name => 'Song of Travel', :score => [:UP, :LEFT, :RIGHT, :UP, :LEFT, :RIGHT], :event => 2 } SONGS[:songofdeath] = {:name => 'Song of Death', :score => [:LEFT, :RIGHT, :A, :LEFT, :RIGHT, :A], :event => 3 }end#==============================================================================# ** Game_System#------------------------------------------------------------------------------# This class handles system data. It has been modified to save the disable# state of instruments. Instances of this class are referenced by $game_system.#==============================================================================class Game_System #-------------------------------------------------------------------------- # * Public Instance Variables #-------------------------------------------------------------------------- attr_accessor :instrument_disabled # instrument forbidden #-------------------------------------------------------------------------- # * Object Initialization #-------------------------------------------------------------------------- alias init_instrument_disabled initialize def initialize init_instrument_disabled @instrument_disabled = false endend#==============================================================================# ** Game_Party#------------------------------------------------------------------------------# This class handles parties. It has been modified to include the party's# acquired songs. Instances of this class are referenced by $game_party.#==============================================================================class Game_Party < Game_Unit #-------------------------------------------------------------------------- # * Public Instance Variables #-------------------------------------------------------------------------- attr_accessor = :songs # party's acquired songs #-------------------------------------------------------------------------- # * Object Initialization #-------------------------------------------------------------------------- alias init_party_songs initialize def initialize init_party_songs @songs = [] end def list_songs p @songs end #-------------------------------------------------------------------------- # * Add Song #-------------------------------------------------------------------------- def add_song(song_id) return unless song_exists?(song_id) return if song_acquired?(song_id) @songs.push song_id end #-------------------------------------------------------------------------- # * Determine if Song Exists #-------------------------------------------------------------------------- def song_exists?(song_id) return true if Instrument::SONGS.include?(song_id) end #-------------------------------------------------------------------------- # * Determine if Song Already Acquired #-------------------------------------------------------------------------- def song_acquired?(song_id) return true if @songs.include?(song_id) endend#==============================================================================# ** Scene_Map#------------------------------------------------------------------------------# This class performs the map screen processing. It has been modified to# include processing for calling an instrument to play. In turn, a constant has# been provided for specifying an Input key for instrument calling.#==============================================================================class Scene_Map < Scene_Base #-------------------------------------------------------------------------- # * Constants #-------------------------------------------------------------------------- INSTRUMENT = :SHIFT # calls instrument #-------------------------------------------------------------------------- # * Start Processing #-------------------------------------------------------------------------- alias init_instrument_calling start def start init_instrument_calling @instrument_calling = false end #-------------------------------------------------------------------------- # * Update Scene Transition #-------------------------------------------------------------------------- alias call_instrument_scene update_scene def update_scene call_instrument_scene update_call_instrument unless scene_changing? end #-------------------------------------------------------------------------- # * Determine if Instrument is Called due to Instrument Calling Key #-------------------------------------------------------------------------- def update_call_instrument if $game_system.instrument_disabled || $game_map.interpreter.running? @instrument_calling = false else @instrument_calling ||= Input.trigger?(INSTRUMENT) call_instrument if @instrument_calling && !$game_player.moving? end end #-------------------------------------------------------------------------- # * Call Instrument #-------------------------------------------------------------------------- def call_instrument Sound.play_ok SceneManager.call(Scene_Instrument) endend#==============================================================================# ** Window_NoteInput#------------------------------------------------------------------------------# This window is displayed on the instrument scene and handles note# processing.#==============================================================================class Window_NoteInput < Window_Selectable #-------------------------------------------------------------------------- # * Public Instance Variables #-------------------------------------------------------------------------- attr_accessor :score attr_accessor :note_index #-------------------------------------------------------------------------- # * Object Initialization #-------------------------------------------------------------------------- def initialize super(window_x, window_y, window_width, window_height) @note_index = 0 @score = [] refresh activate end #-------------------------------------------------------------------------- # * Get Window X-Coordinate #-------------------------------------------------------------------------- def window_x return Graphics.width / 8 end #-------------------------------------------------------------------------- # * Get Window Y-Coordinate #-------------------------------------------------------------------------- def window_y return Graphics.height / 2 - window_height / 2 end #-------------------------------------------------------------------------- # * Get Window Width #-------------------------------------------------------------------------- def window_width return Graphics.width - Graphics.width / 4 end #-------------------------------------------------------------------------- # * Get Window Height #-------------------------------------------------------------------------- def window_height return line_height * 6 end #-------------------------------------------------------------------------- # * Refresh #-------------------------------------------------------------------------- def refresh draw_all_horz_lines draw_all_notes unless @score.empty? end #-------------------------------------------------------------------------- # * Draw All Notes #-------------------------------------------------------------------------- def draw_all_notes @score.each_with_index do |note, index| draw_note(note_index(note), note_x(index), note_y(note)) end end #-------------------------------------------------------------------------- # * Draw Note #-------------------------------------------------------------------------- def draw_note(index, x, y) bitmap = Cache.system(Instrument::NOTESET) rect = Rect.new(index % 6 * 24, index / 6 * 24, 24, 24) contents.blt(x, y, bitmap, rect) end #-------------------------------------------------------------------------- # * Get Graphics Index for Drawing Note #-------------------------------------------------------------------------- def note_index(note) return Instrument:😀ATA[note][1] end #-------------------------------------------------------------------------- # * Get X-Coordinate for Drawing Note #-------------------------------------------------------------------------- def note_x(index) width = 24 return (index + 1) * (width + standard_padding) end #-------------------------------------------------------------------------- # * Get Y-Coordinate for Drawing Note #-------------------------------------------------------------------------- def note_y(note) return (Instrument:😀ATA[note][2] * line_height) + standard_padding end #-------------------------------------------------------------------------- # * Draw All Horizontal Lines #-------------------------------------------------------------------------- def draw_all_horz_lines hl = Instrument::KEYS.size + 1 hl.times {|hl| draw_horz_line(hl * line_height)} end #-------------------------------------------------------------------------- # * Draw Horizontal Line #-------------------------------------------------------------------------- def draw_horz_line(y) line_y = y + line_height / 2 - 1 contents.fill_rect(0, line_y, contents_width, 2, line_color) end #-------------------------------------------------------------------------- # * Get Color of Horizontal Line #-------------------------------------------------------------------------- def line_color color = normal_color color.alpha = 48 color endend#==============================================================================# ** Scene_Instrument#------------------------------------------------------------------------------# This class performs song playing processing.#==============================================================================class Scene_Instrument < Scene_MenuBase #-------------------------------------------------------------------------- # * Start Processing #-------------------------------------------------------------------------- def start super create_note_input_window end #-------------------------------------------------------------------------- # * Create Note Input Window #-------------------------------------------------------------------------- def create_note_input_window @note_input_window = Window_NoteInput.new @note_input_window.set_handler🙂cancel, method🙂return_scene)) end #-------------------------------------------------------------------------- # * Frame Update #-------------------------------------------------------------------------- def update super process_input unless score_complete? process_score if score_complete? end #-------------------------------------------------------------------------- # * Input Processing #-------------------------------------------------------------------------- def process_input return unless @note_input_window.open? && @note_input_window.active process_note🙂DOWN) if Input.trigger?🙂DOWN) process_note🙂LEFT) if Input.trigger?🙂LEFT) process_note🙂RIGHT) if Input.trigger?🙂RIGHT) process_note🙂UP) if Input.trigger?🙂UP) process_note🙂A) if Input.trigger?🙂A) process_note🙂C) if Input.trigger?🙂C) @note_input_window.contents.clear @note_input_window.refresh end #-------------------------------------------------------------------------- # * Process Note #-------------------------------------------------------------------------- def process_note(note) Audio.se_play(note_audio(note), note_volume, note_pitch(note)) @note_input_window.score.push note @note_input_window.note_index += 1 end #-------------------------------------------------------------------------- # * Get Note Audio File #-------------------------------------------------------------------------- def note_audio(note) return Instrument:😀ATA[note][0] end #-------------------------------------------------------------------------- # * Get Note Playback Volume #-------------------------------------------------------------------------- def note_volume return Instrument::VOLUME end #-------------------------------------------------------------------------- # * Get Note Pitch #-------------------------------------------------------------------------- def note_pitch(note) return Instrument:😀ATA[note][3] end #-------------------------------------------------------------------------- # * Process Score #-------------------------------------------------------------------------- def process_score process_song if song_played? clear_score if score_complete? && !song_played? end #-------------------------------------------------------------------------- # * Determine if Song Played #-------------------------------------------------------------------------- def song_played? Instrument::SONGS.each do |song_symbol| return true if song_symbol[1][:score] == @note_input_window.score end end #-------------------------------------------------------------------------- # * Process Song #-------------------------------------------------------------------------- def process_song song = Instrument::SONGS case @note_input_window.score when song[:songofstorms] ; $game_temp.reserve_common_event(song[:songofstorms][2]) when song[:songoftravel] ; $game_temp.reserve_common_event(song[:songoftravel][2]) when song[:songofdeath] ; $game_temp.reserve_common_event(song[:songofdeath][2]) end Graphics.wait(6) Sound.play_ok Graphics.wait(60) SceneManager.return end #-------------------------------------------------------------------------- # * Clear Score #-------------------------------------------------------------------------- def clear_score @note_input_window.note_index = 0 @note_input_window.score.clear Graphics.wait(6) Sound.play_buzzer Graphics.wait(60) contents.clear refresh end #-------------------------------------------------------------------------- # * Determine if Score is Complete #-------------------------------------------------------------------------- def score_complete? return true if @note_input_window.note_index == Instrument::MAX endend
p.s. open the note window with shift.

oops, forgot to add the attr_accessors...fixed
And the exact and full error message? And is that happening in the original version or in the experimental one?


The error message you showed before has nothing to do with comparing the index to a constant either.
i made a few errors uploading the scripts into the spoilers the first couple of times. if it isn't too much trouble, copy and paste both of the scripts again. i have made sure that the scripts inside the spoilers are exact copies of what is in my project. here is a screen of the error message. line 391, argument error. line 391 is a comparison between the instance variable and a constant.

Untitled-1.png

Untitled-1.png
First thing that jumped out at me based on the one thing I read, I'll leave this problem to Shaz.

change this:

Code:
#--------------------------------------------------------------------------  # * Determine if Score is Complete  #--------------------------------------------------------------------------  def score_complete?    return true if @note_input_window.note_index == Instrument::MAX  end
to this:
Code:
#--------------------------------------------------------------------------  # * Determine if Score is Complete  #--------------------------------------------------------------------------  def score_complete?    return @note_input_window.note_index == Instrument::MAX  end
i can't imagine that would make any kind of difference...

after trying...nope. i'm still getting an argument error, but thank you anyway Diamond.

also, this isn't a problem. i apologize. i didn't mean for this to turn into what it did. if this were a problem i would've posted it in a different thread. i just wanted to discuss instance variables in this thread, and honestly...you guys gave me some really good information ond accessing instance variables properly and what the attr_readers, writers, and accessors are, so this is actually a mission accomplsihed.
We'll stop helping you if you want us to stop helping you, but you do still have an issue.


You have two scripts named dbchest_Instrumentals. I'm pretty sure line 391 of the script in your screenshot will not produce that error (unless @note_input_window.note_index or Instrument::MAX are actually methods and not variables or constants). What's on line 391 in your OTHER (top) dbchest_Instruments script?
I didn't actually read the error, I looked at the image with the code first. I mentioned it because you're not return boolean values. You are returning true if you can but never returning false. The little edit fixes that.

Your actual issue on the other hand is because you're trying to access the attr_accessor when you have a method underneath it that takes priority.

One thing to know about Ruby, lowest object takes the priority, fix your method name or the accessor.

Spoiler
Code:
#==============================================================================# ** Window_NoteInput#------------------------------------------------------------------------------#  This window is displayed on the instrument scene and handles note# processing.#==============================================================================class Window_NoteInput < Window_Selectable  #--------------------------------------------------------------------------  # * Public Instance Variables  #--------------------------------------------------------------------------  attr_accessor :score  attr_accessor :note_index      # Some Time Later ...      #--------------------------------------------------------------------------  # * Get Graphics Index for Drawing Note  #--------------------------------------------------------------------------  def note_index(note)    return Instrument::DATA[note][1]  endend
Shaz:

i'm not asking anyone to stop helping, in fact i would love to see this resolved.

the top one is the original. i created a duplicate and placed it below to edit without risking a loss of the original.

before playtesting, i select and comment out the script i don't want to process during the test play.

Diamond:

wait, what? i think i understand the lower priority thing. hence, why we paste our scripts below default and above main, right? where specifically is the confusion within the script though? what do you mean i am trying to reference the accessor when a line below is taking priority? i don't understand...

SHAZ!! Lol, i ran a search "def note_index."

i actually did create a method under this identifier, LMAO. i use it to specify the index for the graphics within the spriteset of my note graphics! obviously, i passed the note being input. let me make a change...
Does your method take parameters? If so, that's the problem.


If you're making them attr_accessors, then you don't need to provide getter/setter methods. Actually, it looks like your note_index method might not be referring to the note_index variable. In that case, you should rename one or the other and ensure all references are updated accordingly.
you're exactly right. it wasn't referencing the variable. i had a method note_index within my window class that takes one argument: the note assigned to the button being pressed. a quick name change to the instance variable solved the problem entirely.

MOST importantly. a lot of useful information passed through this thread and i feel very confident with publicizing and accessing instance variables accurately. thank you very much Shaz. thank you also Diamond. this was a winner winner, chicken dinner. you can lock this if you'd like.
Zeriab gave a little bit of good info up there too 😉


This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/public-instance-variables.19005/