Main Menu
  • Welcome to The RPG Maker Resource Kit.

[Resolved] trouble passing variable between classes

Started by shintashi, April 30, 2011, 03:12:13 PM

0 Members and 1 Guest are viewing this topic.

shintashi

I'm trying to figure out how to change a non global variable "@term" normally listed as
@term = $data_chants[0].name

into "@chant" by doing this:
@fragment_1.term = @chant


Here's the two parts in more detail:

from here in:

"class Scene_Battle < SDK::Scene_Base"


i have this command, and the " p @chant.name.to_s" works fine, it only crashes on the next line:
"@fragment_1.term = @chant"

Which means my error is in my failure to properly address something, either the fragment_1 class, or the @term (I'm guessing).


    if Input.trigger?(Input::C)
      #play decision SE
      $game_system.se_play($data_system.decision_se)
      @chant = @chant_window.chant
      p @chant.name.to_s
      @fragment_1.term = @chant
      return
    end




to this window here:


#====================================================================
#      Fragment 1 - super (x, y, width, height)
#====================================================================
class Fragment_1 < Window_Base #Window_Fragment

  def initialize
    super(100, 278, 90,42)
    self.contents = Bitmap.new(width-32, height-32)
    refresh
    self.back_opacity = 160
    end
   
  def refresh
    self.contents.clear
        self.contents.font.color = text_color(0)#white
        self.contents.font.size = 16 #was 20
        @term = $data_chants[0].name
        #@term = "ichi"
        self.contents.draw_text(0, -10, 90, 32, @term.to_s)
      end
     
  def update
    if Graphics.frame_count / Graphics.frame_rate != @total_sec
      refresh
    end
  end
 
end



LoganF

I think this is something I run into when I'm not really paying much attention. My thoughts:

Normally, @fragment_1.term is a method call to 'term' on the object held in @fragment_1. @fragment_1.term = @chant (again normally) is trying to assign the @chant variable to the method 'term', which doesn't really work. What you're missing is a set method. Two ways I can think of to do this:


def term=(chant)
   @chant = chant
end


I think that should work, if I follow how VX normally does set methods.

I usually do it like this:


def term(chant)
    @chant = chant
end


The different being instead of using @fragment_1.term = @chant, I would have to do @fragment_1.term(@chant) - i.e. passing an argument instead of overriding the '=' operator.

The other way I can think of is to make 'term' a class variable that is read/writeable. Something like 'attr_accessor: term' at the top of Class Fragment_1 should work for that. Then you're @fragment_1.term = @chant should also work.

I may have something wrong, but that's how I'm seeing it.

Also: the Fragment_1 class doesn't have a term method anyway, so if you're getting a NoMethodError, which I'd assume is happening when it crashes, that's why.
(Why do I always feel like it's the end of the world and I'm the last man standing?)

shintashi

much thanks. The Attr accessor thing worked. I just need to figure out how to be able to switch from one window to the next (I have six windows to pass info into... similar to an alchemy drag drop system), but I'm grateful for a working system at this time ^^

LoganF

The only problem with using attr_accessor is that anybody can access it and change it's contents without having to go through any potential checks you may want to implement.

Using a proper set method (either term= or term(args)) means you can control what happens to the passed in information. If you use a set method, you can then make either a get method (can simply return the variable data) or use attr_reader like you have attr_accessor to get hold of the information.

attr_accessor = read and write access to the variable
attr_reader = read-only access to the variable
attr_writer = write-only access to the variable

As for the window thing, I don't quite understand enough to offer any advice on that.
(Why do I always feel like it's the end of the world and I'm the last man standing?)