Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

A Quick Question

Ok, lemme try to explain this, I have one Window that has attribute accessors that are global, and they are used in a sentance, for instance:
Code:
"You just made a " + $itemmade + "."
And in another Window you want to store new information in that global variable, something like
Code:
$itemmade = Bronze Shoe
what i want to know is do you have to put something like:
Code:
object = Window_ItemsMadeOptions.new
Window_ItemsMadeOptions.refresh(Bronze Shoe)
That is not working, I was helped with this problem once beforebut I sorta had to reboot the computer and kinda lost my project and everything else...

If you still do not understand the question, lemme try this:
Code:
class Window_ItemOptions < Window_Base
  attr_accessor  :item
  attr_accessor  :drop
  attr_accessor  :use
  def initialize
    super(340,64,300,415)
    self.contents = Bitmap.new(width-32, height-32)
    self.contents.font.name = "Arial"  
    self.contents.font.size = 22
    refresh(" "," "," ")
  end
  
  def refresh(item,drop,use)
    $itemcrazy = item
    $dropcrazy = item
    $usecrazy = use
    self.contents.clear
    self.contents.font.color = normal_color
    self.contents.draw_text(0, 0, 300, 32, "test 1-3"+"-"+$itemcrazy+"-"+$dropcrazy+"-"+$usecrazy)
  end
end

and in another window I have it so once you click on a option it will transfer information:
Code:
  def update_command
    if Input.trigger?(Input::B) # Back
      $game_system.se_play($data_system.cancel_se) # Makes a cancelation sound.
      $scene = Scene_Item.new # Restores Scene_map.
      return
    end
    if Input.trigger?(Input::C) # Confirm
      case @command_window # Main option window index controls.
      when 0
        object = Window_ItemOptions.new
        Window_ItemOptions.refresh(Bronze Shoe)
      end
    end
  end        
end
Hope this helps enough to get help.
 
Actually ruby is trying to interpret Bronze Shoe as a constant, but that fails, because spaces are not allowed in constant or variable-names.

I think you want to save the String "Bronze Shoe", if that is the case, you need to tell ruby to interpret it as a constant string using "" around it.

The second problem i see with your code is, the refresh-method of your window needs 3 parameters, but you actually only pass 1 to it. This will not work. Either pass 3 parameters to the method, or make the last two parameters optional by giving them a default value.
The third is, you try to call the refresh-method of the class, not of the object you just created (and using "object" as a variable-identifier is bad, its just to unspecific) but you want your recently created window to be refreshed so you need to call it own refresh-method. Apart from the fact, that there is no class-method called refresh.

Code:
def refresh(item,drop = "",use = "")
    $itemcrazy = item
    $dropcrazy = item
    $usecrazy = use
    self.contents.clear
    self.contents.font.color = normal_color
    self.contents.draw_text(0, 0, 300, 32, "test 1-3"+"-"+$itemcrazy+"-"+$dropcrazy+"-"+$usecrazy)
  end

Code:
when 0
        itemOption = Window_ItemOptions.new
        itemOption.refresh("Bronze Shoe")

Edit:
Some things about attribute. If you create an attribute by attr_reader, attr_writer or attr_accessor it will be created as an "instance variable". That means that variable belongs to an actual instance of the class.
So for your example you would access it by
Code:
itemOption = Window_ItemOption.new
x = itemOption.item
y = itemOption.drop
z = itemOption.use
Different instances of "Window_ItemOption" would have different instance-variables with these names.
Inside the class, like in the methods you can access instance-variables with the @-symbol
Code:
def refresh(item, drop, use)
# The local variable item is different from the instance variable @item
@item = item
@drop = drop
@use = use
# .. more code
end
If you have an instance of Window_ItemOption you don't need global variables to pass the information to another window, if the instance-variable is created using attr_reader or attr_accessor it can be read from the outside. The same goes for writing, if it is created with attr_writer or attr_accessor it can also be written from the outside.

I would advise you to read up something general about classes and objects, these two concepts are vital to ruby.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top