Another Strange Error... Throwing Me Off

● ARCHIVED · READ-ONLY
Started by Euphoria 4 posts View original ↗
  1. Okay so the way I THOUGHT I learned to acces 2-dimensional arrays was with this sort of syntax:

    array[0][0] #Which would get the first piece of the array, and it's first sub-piece, right?Well in my script (below) that is throwing me an error saying that "No method error occured; undefined method [] for <NewCurrency>" 

    Here's the code: The error seems to only be on lines 220, 230, and 240... even though I'm using the same syntax on lines 160-180 above...

    Spoiler
    module Euphoria module Currencies CURRENCY_ONE_ON = true CURRENCY_TWO_ON = true CURRENCY_THR_ON = true XTRA_CURRENCIES_USED = 3 CURR_HASH = { "Dollars" => { :SYMBOL => "$", :DESC => "Official currency of the U.S.", :ICON => 30, :MAX => 999999, }, "Random" => { :SYMBOL => "Q", :DESC => "A strange foreign looking currency", :ICON => 31, :MAX => 999999, }, "Randomm" => { :SYMBOL => "nD", :DESC => "Unofficial currency", :ICON => 32, :MAX => 999, }, } endend#┌──────────────────────────────────────────────────────────────────────────────#│■ DataManager#└──────────────────────────────────────────────────────────────────────────────class << DataManager #ALIAS - CREATE_GAME_OBJECTS alias euphoria_multcurrency_datamanager_creategameobjects_24 create_game_objects def create_game_objects euphoria_multcurrency_datamanager_creategameobjects_24 $ECurrency = ExtraCurrencies.new end end#┌──────────────────────────────────────────────────────────────────────────────#│■ ExtraCurrencies#└──────────────────────────────────────────────────────────────────────────────class NewCurrency attr_accessor :name attr_accessor :symbol attr_accessor :desc attr_accessor :icon attr_accessor :max attr_accessor :amount #NEW - INITIALIZE def initialize(name, symbol, desc, icon, max, amount) @name = name @symbol = symbol @desc = desc @icon = icon @max = max @amount = amount end end#┌──────────────────────────────────────────────────────────────────────────────#│■ ExtraCurrencies#└──────────────────────────────────────────────────────────────────────────────class ExtraCurrencies #NEW - INITIALIZE def initialize @ecurrencies = [] add_currencies end #NEW - ADD_CURRENCIES def add_currencies Euphoria::Currencies::CURR_HASH.each {|name, val| currency = NewCurrency.new(name, val[:SYMBOL], val[:DESC], val[:ICON], val[:MAX], 0) @ecurrencies.push(currency) } end #NEW - ECURRENCIES def ecurrencies_array @ecurrencies end #NEW - INCREASE_CURRENCY def increase_currency(curr_name, amt) if @ecurrencies.any? @ecurrencies.each {|currency| if currency.name == curr_name currency.amount = [[currency.amount + amt, 0].max, currency.max].min end } end end #NEW - DECREASE_CURRENCY def decrease_currency(curr_name, amt) if @ecurrencies.any? @ecurrencies.each {|currency| if currency.name == curr_name currency.amount = [[currency.amount - amt, 0].max, currency.max].min end } end end #NEW - SET_CURRENCY def set_currency(curr_name, amt) if @ecurrencies.any? @ecurrencies.each {|currency| if currency.name == curr_name currency.amount = [[amt, 0].max, currency.max].min end } end end #NEW - CURRENCY_COUNT def currency_count(curr_name) if @ecurrencies.any? @ecurrencies.each {|currency| if currency.name == curr_name return currency.amount end } end end end#┌──────────────────────────────────────────────────────────────────────────────#│■ Game_Party#└──────────────────────────────────────────────────────────────────────────────class Game_Party < Game_Unit attr_accessor :cur_one attr_accessor :cur_two attr_accessor :cur_thr #ALIAS - INITIALIZE alias euphoria_multcurrency_gameparty_initialize_24 initialize def initialize @cur_one = 0 @cur_two = 0 @cur_thr = 0 euphoria_multcurrency_gameparty_initialize_24 end #NEW - CUR_ONE_REAL def cur_one_real @cur_one = $ECurrency.ecurrencies_array[0][5] return $ECurrency.ecurrencies_array[0][5] end #NEW - CUR_TWO_REAL def cur_two_real @cur_two = $ECurrency.ecurrencies_array[1][5] return $ECurrency.ecurrencies_array[1][5] end #NEW - CUR_THR_REAL def cur_thr_real @cur_two = $ECurrency.ecurrencies_array[2][5] return $ECurrency.ecurrencies_array[2][5] end end#┌──────────────────────────────────────────────────────────────────────────────#│■ Window_Gold#└────────────────────────────────────────────────────────────────────────────── class Window_Gold < Window_Base #OVERWRITE - INITIALIZE def initialize case Euphoria::Currencies::XTRA_CURRENCIES_USED when 3 super(0, 0, window_width, 120) when 2 super(0, 0, window_width, 96) when 1 super(0, 0, window_width, 72) else super(0, 0, window_width, 48) end refresh end #ALIAS - REFRESH alias euphoria_multcurrency_windowgold_refresh_24 refresh def refresh euphoria_multcurrency_windowgold_refresh_24 if Euphoria::Currencies::CURRENCY_ONE_ON == true draw_currency_value(val_one, unit_one, 4, 24, contents.width - 8) end if Euphoria::Currencies::CURRENCY_TWO_ON == true draw_currency_value(val_two, unit_two, 4, 48, contents.width - 8) end if Euphoria::Currencies::CURRENCY_THR_ON == true draw_currency_value(val_thr, unit_thr, 4, 72, contents.width - 8) end end #NEW - VAL_ONE def val_one $game_party.cur_one end #NEW - UNIT_ONE def unit_one return $ECurrency.ecurrencies_array[0][1].to_s end #NEW - VAL_TWO def val_two $game_party.cur_two end #NEW - UNIT_TWO def unit_two return $ECurrency.ecurrencies_array[1][1].to_s end #NEW - VAL_THR def val_thr $game_party.cur_thr end #NEW - UNIT_THR def unit_thr return $ECurrency.ecurrencies_array[2][1].to_s end end
    So I tried accessing the array by using this syntax:

    array[0[0]] #This no longer throws me an errorNo error, but it does not display the information in that spot of the array, instead it's displaying this:

    Untitled_1.png

    Instead of displaying the NewCurrency Amount (sub-array piece 5) and the symbol (sub-array piece 1).

    I'm greatly confused... Any help would be awesome and very appreciated, explaining things to me would also be very very helpful!

    Thanks, and sorry for all the questions, I just seem to be "trying" to script a lot today.
  2. Your currencies_array is a one-dimensional array consisting of NewCurrency instances. So instead of accessing it like you are as a two dimensional array, try accessing it by just one bracket, and then calling #name on that. Objects aren't arrays, you can't call their properties like you would an array's indices.

    As for your methods in ExtraCurrencies, you are using #each inside of #any?, which isn't the right way to use those methods. You are looping through the array of currencies twice, which is quite unnecessary. In fact, neither #any? or #each is the way to go. The following should give you the gist:

    class ExtraCurrencies #NEW - INCREASE_CURRENCY def increase_currency(curr_name, amt) currency = @ecurrencies.find {|cur| cur.name == curr_name } return false if currency.nil? # can also just be 'return false unless currency' currency.amount = [[currency.amount + amt, 0].max, currency.max].min endendIf I haven't explained well enough, let me know and I'll elaborate :) EDIT: I also wouldn't recommend using switches and hardcoding each currency in; rather, write a few methods in Game_Interpreter that allow turning currencies on and off, and keep an instance_variable in NewCurrency instances that keeps track of wether to show them in the menu or not. For instance:

    Code:
    class NewCurrency  attr_reader :show_in_menu  def initialize(name, symbol, desc, icon, max, amount)    ...# Your code here    @show_in_menu = true  end  def show    @show_in_menu = true  end  def hide    @show_in_menu = false  endendclass Window_Gold  def can_show_currency?(currency)    return currency.show_in_menu  endendclass Game_Interpreter  def hide_currency(curr_name)    currency = $Ecurrencies.currency_array.find {|c| c.name == curr_name }    return false unless currency    currency.hide  end  def show_currency(curr_name)    #... same as above, only calling currency.show  endend
    This way, it doesn't tie up twenty switches for twenty currencies, and you can use this new property to determine whether to show the currency in the menu, and you don't have to hard code the currency drawing in; simply iterate through $Ecurrencies.currency_array and draw it only if it can be shown!
  3. Can you give me an example of the call for it?

    I think I understand the rest, I'll try to fix things up :p I'm not that great at coding, yet. Just learned how to use hashes and arrays last night really... lol.. and I was referencing another code that used "any" and "each", but I'm pretty sure I get what's wrong there.

    Thanks a ton for the reply though :) I thought it was a 2-dimensional array that I made xD but that makes much more sense. I'm just not sure how to make the call.

    Edit: Nevermind, I figured it out.    array[x].variable

    Thanks a million!
  4. 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.