RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
[VXA] Problems with Hashes/Arrays and loading Data from a File

0 Members and 1 Guest are viewing this topic.

**
Rep:
Level 75
RMRK Junior
Heyey guys,

since I'm scripting a bit with the VXA, I found a little problem^^

I save certain data from a hash which is defined in a module and it works very good.
Now my problem is, that if I try to load the data, I keep getting this error:

Quote
dynamic constant assignment
    TEST = stuff

here is the script:

Code: [Select]
module Flip
TEST = []
  def self.do_save
    File.open("Data/Test.rvdata2", "wb") { |file|
      Marshal.dump(TEST,file)
    }
  end
 
  def self.do_load
    File.open("Data/Test.rvdata2", "rb") { |file|
      stuff = Marshal.load(file)
      p stuff
      TEST = stuff
    }
  end
end

and I set the events call script up like this:
Quote
Flip::TEST = [1,2,3,4,"Hello",5,6]
Flip.do_save
Flip.do_load

Is it in any way possible to load the data and pass it to an hash/array in a module?
Or do I have now to change everything to a game_ class?

I'm thankfull for every help I get (:

~flipy

*
Rep:
Level 82
RGSS3 doesn't let you reassign or change a constant variable once it has been given a value, where Ruby normally would (but would give you a warning about it). This really makes sense because it's a constant value, it shouldn't change. But in cases where you want to change it, like when you are essentially trying to initializing it from an input file, you may need to do so.

A couple of solutions.

You can make TEST a class variable (or something other than a constant). So something like:

Quote
module Flip
   
  @@test = []
   
  def self.test
    @@test
  end
 
  def self.do_save
    File.open("Data/Test.rvdata2", "wb") { |file|
      Marshal.dump(@@test,file)
    }
  end
 
  def self.do_load
    File.open("Data/Test.rvdata2", "rb") { |file|
      stuff = Marshal.load(file)
      p stuff
      @@test = stuff
    }
  end
 
end

Or you can make use of the const_set and const_get methods when you want to operate on a constant variable:

Code: [Select]
module Flip
TEST = []
  def self.do_save
    File.open("Data/Test.rvdata2", "wb") { |file|
      Marshal.dump(const_get("TEST"),file)
    }
  end
 
  def self.do_load
    File.open("Data/Test.rvdata2", "rb") { |file|
      stuff = Marshal.load(file)
      p stuff
      const_set("TEST", stuff)
    }
  end
end

You won't typically need to use const_get because constant variables can be read from without much issue. You can, however, use const_get like:

Code: [Select]
Flip.const_get("TEST")

You do need to use const_set, however, to avoid the "dynamic constant assignment" warning/error.

Because const_set exists and is granted by the Object class, the concept of constants in Ruby is merely a concept and not something that is enforced by Ruby itself. The reason for designing a variable to be constant is to tell other programmers that "the value in this variable should not be changed."

RGSS3 provides a warning/error as a design choice to prevent accidental data change, but as you can see, using Ruby's const_set lets you change it all the same.
« Last Edit: May 04, 2013, 09:28:10 AM by LoganF »
(Why do I always feel like it's the end of the world and I'm the last man standing?)

**
Rep:
Level 75
RMRK Junior
heyey,

thanks for your help it works now (:

But I got another little problem. I am saving a few hashes in an array which is then put into the file which will be created.
The saved hashes can be empty at the time of saving.
Now I have this error to apear: {} is not a symbol

any ideas of fixing this? /:

*
Rep:
Level 82
I'm not sure what it is you might be doing to get that. Can you put up your code? If you want to send it to me through private message, that is fine.
(Why do I always feel like it's the end of the world and I'm the last man standing?)

**
Rep:
Level 75
RMRK Junior
Spoiler for:
Code: [Select]
#===============================================================================
#                           FLIPELYFLIP CREATIONS
#-------------------------------------------------------------------------------
#
#                         Extended Shop Prices v1.0
#                              by FlipelyFlip
#
#-------------------------------------------------------------------------------
#  What does this script do?
#  -------------------------
#  This script allows you to define automaticly while the game is running.
#  It dyes the name in the defined colors if it's more expensive or cheaper
#  than the standard price.
#
#  How to use?
#  -----------
#  The script itself is plug and play. To use it without any problems,
#  define with the first item in the shop selection the ID of the shop.
#  This is done through the "Specify"-option. This is used to prevent
#  from pushing the standard price down by entering multiple times the
#  same shop.
#
#  Terms of Use?
#  -------------
#  If you use this script for your non-commercial project then only credits
#  needed to give to me. FlipelyFlip is my name.
#  If you want to use this script in your commercial project then please write
#  me an mail to flipely@hotmail.com or write me a pm.
#===============================================================================

#===============================================================================
#  ** Module Flip
#-------------------------------------------------------------------------------
#  * Used to define needed variables and includes the main method for the script
#===============================================================================
module Flip
  # COUNTER is used to set the countdown for the price, when it will turn to the
  # standard-price. The counter will count every time one step down, if the
  # item ID and price match. Else it would add it to the hash.
  COUNTER = 5
  # EXPENSIVE_COLOR is used to set the color for the items which are more
  # expensive then the standard-price.
  # Color.new(r,g,b)
  EXPENSIVE_COLOR = Color.new(255,100,100)
  # CHEAPER_COLOR is the same like EXPENSIVE_COLOR but it will be aplied when
  # the item is cheaper than the standard-price
  # Color.new(r,g,b)
  CHEAPER_COLOR = Color.new(100,255,100)
  # USE_LOW_RANGE: if true then it will use a different color if the prices are
  # in a certain range. if false then it only will aply the colors defined above
  USE_LOW_RANGE = true
  # EXPENSIVE_RANGE is the range to whom the EXPENSIVE_MID_RANGE_COLOR will be
  # aplied.
  EXPENSIVE_RANGE = 20 # value is given as per cent
  # CHEAPER_RANGE is the range to whom the CHEAPER_MID_RANGE_COLOR will be
  # aplied.
  CHEAPER_RANGE = 20 # value is given as per cent
  # EXPENSIVE_LOW_RANGE is the range to whom the EXPENSIVE_LOW_RANGE_COLOR will
  # be aplied.
  EXPENSIVE_LOW_RANGE = 10 # value is given as per cent
  # CHEAPER_LOW_RANGE is the range to whom the CHEAPER_LOW_RANGE_COLOR will
  # be aplied.
  CHEAPER_LOW_RANGE = 10 # value is given as per cent
  # EXPENSIVE_MID_RANGE_COLOR is used to set the color to the defined amount if
  # the price is between EXPENSIVE_LOW_RANGE and EXPENSIVE_MID_RANGE.
  # Color.new(r,g,b)
  EXPENSIVE_MID_RANGE_COLOR = Color.new(255,160,160)
  # CHEAPER_MID_RANGE_COLOR is used to set the color to the defined amount if
  # the price is between CHEAPER_LOW_RANGE and CHEAPER_MID_RANGE.
  # Color.new(r,g,b)
  CHEAPER_MID_RANGE_COLOR = Color.new(180,255,180)
  # EXPENSIVE_LOW_RANGE_COLOR is used to set the color to the defined amount if
  # the price is in the EXPENSIVE_LOW_RANGE.
  # Color.new(r,g,b)
  EXPENSIVE_LOW_RANGE_COLOR = Color.new(255,220,220)
  # CHEAPER_LOW_RANGE_COLOR is used to set the color to the defined amount if
  # the price is in the CHEAPER_LOW_RANGE.
  # Color.new(r,g,b)
  CHEAPER_LOW_RANGE_COLOR = Color.new(220,255,220)

################################################################################
#                                                                              #
#          ONLY EDIT BELOW THIS LINE IF YOU KNOW WHAT ARE YOU DOING!!          #
#                                                                              #
################################################################################

  ITEM_COSTS = {} # do not edit
  ARMOR_COSTS = {} # do not edit
  WEAPON_COSTS = {} # do not edit
  ITEM_STANDARD_COST = {} # do not edit
  ARMOR_STANDARD_COST = {} # do not edit
  WEAPON_STANDARD_COST = {} # do not edit

  #-----------------------------------------------------------------------------
  # * Pass the costs Away
  #   goodies = shop goods
  #-----------------------
  #   This is used to get the standard-price and count down the counter for the
  #   new prices.
  #-----------------------------------------------------------------------------
  def self.pass_the_costs_away(goodies)
    $new_shop_id = goodies[0][3]
    goodies.delete_at(0)
    if $old_shop_id != $new_shop_id
      for goods in goodies
        case goods[0]
        when 0 # Item
          item = $data_items[goods[1]]
          price = goods[3]
          price = item.price if goods[2] == 0
          if !ITEM_STANDARD_COST.include?(item.id)
            ITEM_STANDARD_COST[item.id] = price
          else
            if price != ITEM_STANDARD_COST[item.id]
              if ITEM_COSTS.include?([item.id,price])
                ITEM_COSTS[[item.id,price]] -= 1
                if ITEM_COSTS[[item.id,price]] <= 0
                  ITEM_STANDARD_COST[item.id] = price
                  ITEM_COSTS[[item.id,price]] = COUNTER
                  ITEM_COSTS.each_key { |item_id|
                  if item_id[0] == item.id
                    ITEM_COSTS[item_id] = COUNTER
                  end
                  }
                end
              else
                ITEM_COSTS[[item.id,price]] = COUNTER
              end
            end
          end
        when 1 # Weapon
          item = $data_weapons[goods[1]]
          price = goods[3]
          price = item.price if goods[2] == 0
          if !WEAPON_STANDARD_COST.include?(item.id)
            WEAPON_STANDARD_COST[item.id] = price
          else
            if price != WEAPON_STANDARD_COST[item.id]
              if WEAPON_COSTS.include?([item.id,price])
                WEAPON_COSTS[[item.id,price]] -= 1
                if WEAPON_COSTS[[item.id,price]] <= 0
                  WEAPON_STANDARD_COST[item.id] = price
                  WEAPON_COSTS[[item.id,price]] = COUNTER
                  WEAPON_COSTS.each_key { |item_id|
                  if item_id[0] == item.id
                    WEAPON_COSTS[item_id] = COUNTER
                  end
                  }
                end
              else
                WEAPON_COSTS[[item.id,price]] = COUNTER
              end
            end
          end
        when 2 # Armor
          item = $data_armors[goods[1]]
          price = goods[3]
          price = item.price if goods[2] == 0
          if !ARMOR_STANDARD_COST.include?(item.id)
            ARMOR_STANDARD_COST[item.id] = price
          else
            if price != ARMOR_STANDARD_COST[item.id]
              if ARMOR_COSTS.include?([item.id,price])
                ARMOR_COSTS[[item.id,price]] -= 1
                if ARMOR_COSTS[[item.id,price]] <= 0
                  ARMOR_STANDARD_COST[item.id] = price
                  ARMOR_COSTS[[item.id,price]] = COUNTER
                  ARMOR_COSTS.each_key { |item_id|
                  if item_id[0] == item.id
                    ARMOR_COSTS[item_id] = COUNTER
                  end
                  }
                end
              else
                ARMOR_COSTS[[item.id,price]] = COUNTER
              end
            end
          end
        end
      end
      $old_shop_id = $new_shop_id
    end
  end

  #--------------------------------------------------------------------------
  # * Create Filename
  #     index : File Index
  #--------------------------------------------------------------------------
  def self.filename(index)
    sprintf("Data/Shop_Prices%02d.rvdata2", index + 1)
  end
 
  def self.do_save(index)
    ary = []
    ary = [ITEM_COSTS,WEAPON_COSTS,ARMOR_COSTS,ITEM_STANDARD_COST,
    WEAPON_STANDARD_COST,ARMOR_STANDARD_COST,$old_shop_id]
    File.open(filename(index), "wb") { |file|
      Marshal.dump(ary,file)
    }
  end
 
  def self.do_load(index)
    File.open(filename(index), "rb") { |file|
      stuff = Marshal.load(file)
      const_set(ITEM_COSTS,stuff[0])
      const_set(WEAPON_COSTS,stuff[1])
      const_set(ARMOR_COSTS,stuff[2])
      const_set(ITEM_STANDARD_COST,stuff[3])
      const_set(WEAPON_STANDARD_COST,stuff[4])
      const_set(ARMOR_STANDARD_COST,stuff[5])
      $old_shop_id = stuff[6]
    }
  end
end

#==============================================================================
# ** Scene_Save
#------------------------------------------------------------------------------
#  This class performs save screen processing.
#==============================================================================

class Scene_Save < Scene_File
  #--------------------------------------------------------------------------
  # * Confirm Save File
  #--------------------------------------------------------------------------
  def on_savefile_ok
    super
    if DataManager.save_game(@index)
      on_save_success
      Flip.do_save(@index)
    else
      Sound.play_buzzer
    end
  end
end

#==============================================================================
# ** Scene_Load
#------------------------------------------------------------------------------
#  This class performs load screen processing.
#==============================================================================

class Scene_Load < Scene_File
  #--------------------------------------------------------------------------
  # * Confirm Save File
  #--------------------------------------------------------------------------
  def on_savefile_ok
    super
    if DataManager.load_game(@index)
      on_load_success
      Flip.do_load(@index)
    else
      Sound.play_buzzer
    end
  end
end

#==============================================================================
# ** Window_Base
#------------------------------------------------------------------------------
#  This is a super class of all windows within the game.
#==============================================================================

class Window_Base < Window
 
  def expensive_color;           Flip::EXPENSIVE_COLOR ;  end;  # Color for  more expensive Items
  def cheaper_color;             Flip::CHEAPER_COLOR ;  end;  # Color for cheaper Items
  def expensive_low_range_color; Flip::EXPENSIVE_LOW_RANGE_COLOR ; end; # Color for low expensive Items
  def cheaper_low_range_color;   Flip::CHEAPER_LOW_RANGE_COLOR ; end; # Color for low cheaper Items
  def expensive_mid_range_color; Flip::EXPENSIVE_MID_RANGE_COLOR ; end; # Color for mid expensive Items
  def cheaper_mid_range_color;   Flip::CHEAPER_MID_RANGE_COLOR  ; end; # Color for mid cheaper Items
 
  #--------------------------------------------------------------------------
  # * Draw Shop Text
  #     Same as draw_text but modified for this script (:
  #     rect = x,y,width,height of the text
  #     text = text which would be written
  #     pos  = position of the text
  #     item = the Item which will be drawn for this time.
  #--------------------------------------------------------------------------
  def draw_shop_text(rect,text,pos,item)
    pricing_color(item)
    contents.draw_text(rect.x,rect.y,rect.width,rect.height,text,pos)
  end
  #--------------------------------------------------------------------------
  # * Draw Item Name
  #     enabled : Enabled flag. When false, draw semi-transparently.
  #--------------------------------------------------------------------------
  def draw_item_buy_name(item, x, y, enabled = true, width = 172)
    return unless item
    draw_icon(item.icon_index, x, y, enabled)
    pricing_color(item)
    draw_text(x + 24, y, width, line_height, item.name)
  end
 
  #--------------------------------------------------------------------------
  # * Pricing Color
  #     Checks for the right colors to use.
  #--------------------------------------------------------------------------
  def pricing_color(item)
    if item.is_a?(RPG::Item)
      costing = Flip::ITEM_STANDARD_COST[item.id]
    elsif item.is_a?(RPG::Armor)
      costing = Flip::ARMOR_STANDARD_COST[item.id]
    elsif item.is_a?(RPG::Weapon)
      costing = Flip::WEAPON_STANDARD_COST[item.id]
    end
    if price(item) > costing
      pricey = costing * Flip::EXPENSIVE_RANGE  / 100 + costing
      low_pricey = costing * Flip::EXPENSIVE_LOW_RANGE / 100 + costing
      if price(item) >= pricey
        change_color(expensive_color,enable?(item))
      elsif Flip::USE_LOW_RANGE
        if price(item) >= low_pricey && price(item) <= pricey
          change_color(expensive_mid_range_color,enable?(item))
        elsif price(item) <= low_pricey
          change_color(expensive_low_range_color,enable?(item))
        end
      end
    elsif price(item) < costing
      pricey = costing - (costing * Flip::CHEAPER_RANGE / 100)
      low_pricey = costing - (costing * Flip::CHEAPER_LOW_RANGE / 100)
      if price(item) <= pricey
        change_color(cheaper_color,enable?(item))
      elsif Flip::USE_LOW_RANGE
        if price(item) >= pricey && price(item) <= low_pricey
          change_color(cheaper_mid_range_color,enable?(item))
        elsif price(item) >= low_pricey
          change_color(cheaper_low_range_color,enable?(item))
        end
      end
    else
      change_color(normal_color,enable?(item))
    end
  end
end

#==============================================================================
# ** Window_ShopCommand
#------------------------------------------------------------------------------
#  This window is for selecting buy/sell on the shop screen.
#==============================================================================

class Window_ShopCommand < Window_HorzCommand
  #--------------------------------------------------------------------------
  # * Get Digit Count
  #--------------------------------------------------------------------------
  def col_max
    return 3 if !@purchase_only
    return 2 if @purchase_only
  end
  #--------------------------------------------------------------------------
  # * Create Command List
  #--------------------------------------------------------------------------
  def make_command_list
    add_command(Vocab::ShopBuy,    :buy)
    add_command(Vocab::ShopSell,   :sell) if !@purchase_only
    add_command(Vocab::ShopCancel, :cancel)
  end
end

#==============================================================================
# ** Window_ShopBuy
#------------------------------------------------------------------------------
#  This window displays a list of buyable goods on the shop screen.
#==============================================================================

class Window_ShopBuy < Window_Selectable

  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  alias flipitialize initialize
  def initialize(x, y, height, shop_goods)
    Flip.pass_the_costs_away(shop_goods)
    flipitialize(x, y, height, shop_goods)
  end
  #--------------------------------------------------------------------------
  # * Draw Item
  #--------------------------------------------------------------------------
  def draw_item(index)
    item = @data[index]
    rect = item_rect(index)
    draw_item_buy_name(item, rect.x, rect.y, enable?(item))
    rect.width -= 4
    draw_shop_text(rect, price(item), 2, item)
  end
end

#==============================================================================
# ** Scene_Shop
#------------------------------------------------------------------------------
#  This class performs shop screen processing.
#==============================================================================

class Scene_Shop < Scene_MenuBase
  #--------------------------------------------------------------------------
  # * Get Sale Price
  #--------------------------------------------------------------------------
  def selling_price
    @buy_window.price(@item) / 2
  end
end

################################################################################
#                                                                              #
#                                 END OF SCRIPT                                #
#                                                                              #
################################################################################

it's for my Extended Shop Prices Script. I found out that it won't save and load the data.

*
Rep:
Level 82
Ah, it's very simple.

When you do, for example:

Code: [Select]
const_set(ITEM_COSTS, stuff[0])

You are passing in the value referenced by ITEM_COSTS as the symbol that const_set is asking for. You need to pass the name of the symbol, in this case, the name of the symbol, ITEM_COSTS, not it's value.

So you need to give the name as a string or a symbol:

Code: [Select]
const_set("ITEM_COSTS", stuff[0])

or

Code: [Select]
const_set(:ITEM_COSTS, stuff[0])

Which you prefer is up to you, but either format will work.
(Why do I always feel like it's the end of the world and I'm the last man standing?)

**
Rep:
Level 75
RMRK Junior
thanks a lot for your help (:
works perfect now (: