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.

rxdata help/ data structure issue

ok so ive been working on my interactive map script. and ive been trying really hard to get the game to properly store and recover my map data so i can make a simple editor so people who cant script can use the mouse to make thier map. anyways here is what the data currently looks like
Map_Items::Town.new(80,130,1,8,4,2,"town3","Saar",1)
and here is my data structure if u need it
Code:
#==============================================================================

# ** Data::Object, Map_Items::Town, Sub::Town

#------------------------------------------------------------------------------

#  These classes were created by SephirothSpawn

#==============================================================================

module Map_Items

  Towns = {}

end

module Sub

  Towns = {}

end

class Data::Object

  @@ids = {}

  @@containers = {}

  attr_accessor :id

  def initialize

    @@ids[self.class] = 0 unless @@ids.has_key?(self.class)

    @@ids[self.class] += 1

    @id = @@ids[self.class]

    if @@containers.has_key?(self.class)

      @@containers[self.class][@id] = self

    end

  end

  def self.add_container(class_name, container)

    @@containers[class_name] = container

  end

end

class Map_Items::Town < Data::Object

  Data::Object.add_container(self, Map_Items::Towns)

  attr_reader :screen_x

  attr_reader :screen_y

  attr_reader :map_id

  attr_reader :map_x

  attr_reader :map_y

  attr_reader :direction

  attr_reader :icon

  attr_reader :town_name

  attr_reader :switch

  def initialize(screen_x, screen_y, map_id, map_x, map_y, direction, icon, town_name, switch)

   super()

    @screen_x = screen_x

    @screen_y = screen_y

    @map_id = map_id

    @map_x = map_x

    @map_y = map_y

    @direction = direction

    @icon = icon

    @town_name = town_name

    @switch = switch

  end

end

 

class Sub::Town < Data::Object

  Data::Object.add_container(self, Sub::Towns)

  attr_reader :screen_x

  attr_reader :screen_y

  attr_reader :map_id

  attr_reader :map_x

  attr_reader :map_y

  attr_reader :direction

  attr_reader :icon

  attr_reader :town_name

  attr_reader :switch

  attr_reader :upper_level

  def initialize(screen_x, screen_y, map_id, map_x, map_y, direction, icon, town_name, switch, upper_level)

   super()

    @screen_x = screen_x

    @screen_y = screen_y

    @map_id = map_id

    @map_x = map_x

    @map_y = map_y

    @direction = direction

    @icon = icon

    @town_name = town_name

    @switch = switch

    @upper_level = upper_level

  end

end

#==============================================================================

# ** Sprite_Cursor

#------------------------------------------------------------------------------

#  This sprite is used to display the cursor for World_Map.

#==============================================================================

 

class Sprite_Cursor < Sprite

 def initialize(viewport = Viewport.new(0, 0, 0, 0))

    super(viewport)

    update

  end

  #--------------------------------------------------------------------------

  # * Dispose

  #--------------------------------------------------------------------------

  def dispose

    if self.bitmap != nil

      self.bitmap.dispose

    end

    super

  end

  #--------------------------------------------------------------------------

  # * Frame Update

  #--------------------------------------------------------------------------

  def update

    super

  end

end

#==============================================================================

# ** Window_Map

#------------------------------------------------------------------------------

#  This window displays the name box for World_Map

#==============================================================================

 

class Window_Map < Window_Base

  #--------------------------------------------------------------------------

  # * Object Initialization

  #--------------------------------------------------------------------------

  def initialize

    super(0, 0, 640, 64)

    self.contents = Bitmap.new(width - 32, height - 32)

    self.z = 2010

    @text = ""

  end

  def text=(text)

    if @text != text

      @text = text

      self.contents.clear

      self.contents.draw_text(0, 0, contents.width, 32, @text, 1)

    end

  end

end

 

 

ive managed to get the game to store the data into a rxdata file, and when i open it in notepad it looks right. but i cant seem to get the data back out properly. so i was wondering if some one could show me a simple example of storeing data and recovering it.

p.s. btw the reason im asking for rxdata is so people can copy and paste from editor to game with no issues
p.s.s. its really late sorry if i forgot anything you need to know
 
you could try Marshal.dump
This is how RMXP creates and loads save files.
You'll need to define marshal_dump & marshal_load for your class.
An example I found on the net :
[rgss]  def marshal_dump  
    a = (super rescue [])  
    a.push(@var1, @var2, @var3, @var4)  
  end  
     
  def marshal_load(dumped)  
    super rescue nil  
    @var1 = *dumped.shift
    @var2 = *dumped.shift
    @var3 = *dumped.shift
    @var4 = *dumped
  end
[/rgss]
use: Marshal.dump(object, file) # to save to a file
object = Marshal.load(file) # to load

where file = File.open(filename, "wb")
Just to give you a rough idea.
 
so i add marshal_dump and marshal_load to my class and then i should have no issues saving just the one rxdata? or does this add to save file? im at work and im trying to ask everything i can so when i go home i can try it. i think i understand tho, i add the method then i can call:Marshal.dump(Map_Items::Towns, "Maps") to save an Maps.rxdata file. and Map_Items::Towns = Marshal.load("Maps") to load
 
Plague180":3556la00 said:
so i add marshal_dump and marshal_load to my class and then i should have no issues saving just the one rxdata? or does this add to save file? im at work and im trying to ask everything i can so when i go home i can try it. i think i understand tho, i add the method then i can call:Marshal.dump(Map_Items::Towns, "Maps") to save an Maps.rxdata file. and Map_Items::Towns = Marshal.load("Maps") to load

With Marshal.dump and marshal.load, you open a file (or create one) and feed it to the method. (Along with a variable, in the case of dump) It will then work with that file either to load or save data. From what I remember, it uses a binary data format, as opposed to text based.
 
It can take any type of variable. (Including entire classes) One thing to note is that if you put multiple things into the file, you have to pull them back out in the same order you put them in. (This is known as a first in, first out system, or fifo for short)
 
You feed it the array, it stores the array. You feed it the individual objects, it stores the objects. You feed it a class, it stores the class. (As in, a class that has been instantiated into a variable, using variable = ClassName.new)
 
Be careful though, it can't store reference to objects which data is loaded in memory, like graphics or audio. so you cant do a marshal dump on a Bitmap, plane, viewport, window, sprite. But you can save a reference to the file (i.e. the filename ) so you can reload it later.

Also depending on the way you open the file, it'll either add your stuff at the end of it, or overwrite the whole thing. So you better overwrite the file with this type of application as else the loading method will probably crash and not understand the file structure.
 
i got save to work right im pretty sure(because i can open it in notepad and looks right) but load seems to be having issues.

im still messing with the code, but when i use :
Code:
 filename = "Data/Test.rxdata"

file = File.open(filename, "rb")

Map_Items::Towns[] = Marshal.load(file)

file.close
i get wrong number of arguments 1 for 2 i cant figure our why, thanks for advice
 
Map_Items::Towns[] = Marshal.load(file)

You can't leave that [] empty ever. If you include them, type a number, there are now exceptions while declaring an array value.

Is Map_Items::Towns the first value (array, hash, struct) you stored in your Test.rxdata file?
 
OK, as you already did, you start with an empty Map_Items::Towns hash, but later when you try to retrieve the data from the file do it like this...

Map_Items::Towns.merge!(Marshal.load(file))
 
sweetness, as far as i can tell it is working. my test was as follows: i called a town saar2 then saved it. then load the data from test and it went back to saar. will continue to work and let you know if i need more help. prob will
 
dont wanna start yet another thread so was wondering if u could help me with new issue. im having issues deleting an index out of Map_Items::Towns. prob because of the data structure seph made for it. here it is.
Code:
 

#==============================================================================

# ** Data::Object, Map_Items::Town, Sub::Town

#------------------------------------------------------------------------------

#  These classes were created by SephirothSpawn

#==============================================================================

module Map_Items

  Towns = {}

end

module Sub

  Towns = {}

end

class Data::Object

  @@ids = {}

  @@containers = {}

  attr_accessor :id

  def initialize

    @@ids[self.class] = 0 unless @@ids.has_key?(self.class)

    @@ids[self.class] += 1

    @id = @@ids[self.class]

    if @@containers.has_key?(self.class)

      @@containers[self.class][@id] = self

    end

  end

  def self.add_container(class_name, container)

    @@containers[class_name] = container

  end

end

class Map_Items::Town < Data::Object

  Data::Object.add_container(self, Map_Items::Towns)

  attr_accessor :screen_x

  attr_accessor :screen_y

  attr_accessor :map_id

  attr_accessor :map_x

  attr_accessor :map_y

  attr_accessor :direction

  attr_accessor :icon

  attr_accessor :town_name

  attr_accessor :switch

  def initialize(screen_x, screen_y, map_id, map_x, map_y, direction, icon, town_name, switch)

   super()

    @screen_x = screen_x

    @screen_y = screen_y

    @map_id = map_id

    @map_x = map_x

    @map_y = map_y

    @direction = direction

    @icon = icon

    @town_name = town_name

    @switch = switch

  end 

end

 

class Sub::Town < Data::Object

  Data::Object.add_container(self, Sub::Towns)

  attr_accessor :screen_x

  attr_accessor :screen_y

  attr_accessor :map_id

  attr_accessor :map_x

  attr_accessor :map_y

  attr_accessor :direction

  attr_accessor :icon

  attr_accessor :town_name

  attr_accessor :switch

  attr_accessor :upper_level

  def initialize(screen_x, screen_y, map_id, map_x, map_y, direction, icon, town_name, switch, upper_level)

   super()

    @screen_x = screen_x

    @screen_y = screen_y

    @map_id = map_id

    @map_x = map_x

    @map_y = map_y

    @direction = direction

    @icon = icon

    @town_name = town_name

    @switch = switch

    @upper_level = upper_level

  end

end

 

 
basically what i need is a way to do something like: Map_Items::Towns.delete_index(2)
 
I guess you need to read this...

RI":2iuc7h5k said:
Hash#delete
hsh.delete(key) => value
hsh.delete(key) {| key | block } => value
------------------------------------------------------------------------
Deletes and returns a key-value pair from _hsh_ whose key is equal to _key_. If the key is not found, returns +nil+. If the optional code block is given and the key is not found, pass in the key and return the result of _block_.

h = { "a" => 100, "b" => 200 }
h.delete("a") #=> 100
h.delete("z") #=> nil
h.delete("z") { |el| "#{el} not found" } #=> "z not found"
 

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