Problem with marshalling data..

● ARCHIVED · READ-ONLY
Started by ?????? 14 posts View original ↗
  1. Hey there.

    So to cut a long story short - for a few hours now I have been completely puzzled as to why my game isnt reading the data back in correctly at load.

    Here is the code that is 'saving / loading' my data...

    def self.save_game_without_rescue File.open(filename, "wb") do |file| Marshal.dump({ :space_shooter => $space_shooter }, file) end return true end def self.load_game_without_rescue p "Loading Data" File.open(filename, "rb") do |file| Marshal.load(file) extract_save_contents(Marshal.load(file)) end p "Loaded Data #{$space_shooter}" return true end def self.extract_save_contents(data) $space_shooter = data[:space_shooter] endNow for whatever reason - it is making the save file no problem. But when I try to load the data back in is not allowing it.

    If I remove the rescue clause for the loading process I get an end of file error :/

    Not really sure whats causing this..

    anyone have any insight on that?

    Or alternatively - anyone have a way for me to save a global variable to be re-utilized when the game is next loaded?
  2. It might be the data structure of the $space_shooter object. Do you have a hash in there that sets default values upon key initialization, for example? That should crash when you dump, though, not when you load...
  3. Nope, its just a normal little class containing what is essentially my game_system.

    Looks like this...

    Spoiler
    #===============================================================================##===============================================================================class Space_Shooter_Game_Data#===============================================================================  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  attr_accessor :player  attr_accessor :score  attr_accessor :progress  attr_accessor :difficulty  attr_accessor :hud_hue  attr_accessor :unlocked_levels  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def initialize    @player     = Space_Shooter_Player.new    @unlocked_levels = [true,false,false,nil]    @difficulty = 1    @hud_hue    = 0    @score      = 0    @progress   = 0  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def complete_level    return if @level == final_level    @unlocked_levels[@level] = true    @level += 1  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def final_level    return 2  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def gain_points(value=1)    @score += value  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def progress_through_area    @progress += 1  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def area_complete?    @progress >= 100  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def unlocked_level?(id)    @unlocked_levels[id-1]  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------end 

    As I said as well - its saving the data fine - I can open up the text file and clearly see all the correct information.

    But its throwing some kind of error each time it tries to load the file and its just not reapplying the data to the variable :/
  4. I notice you call Marshal.load(file) twice in a row in self.load_game_without_rescue; could that be an issue?
  5. dont think so man. Thats how the default scripts do it as well...

      #--------------------------------------------------------------------------  # * Execute Load (No Exception Processing)  #--------------------------------------------------------------------------  def self.load_game_without_rescue(index)    File.open(make_filename(index), "rb") do |file|      Marshal.load(file)      extract_save_contents(Marshal.load(file))      reload_map_if_updated      @last_savefile_index = index    end    return true  end
    I basically copy/pasted the default data manager script and deleted what was not needed - ie, all the other global variables. Literally none of them are being used so they where just eating processing power sitting doing nothing lol

    I am completely lost with what my other options could be :/
  6. working with the save system always makes my head hurt cause I don't understand it all that well... but looking at the default way and your way. They don't match up 100%.

    Like

    Marshal.dump({ :space_shooter => $space_shooter }, file)the default way creates a hash and stores each of the variables to a key.. which is no big deal since you only have the one item. Have you tried dropping they key all togeather and just putting your global variable or make the hash and just have the one entry in it.

    When you're calling the data

    def self.extract_save_contents(data) $space_shooter = data[:space_shooter] endyou're using the "data" like you had stored it in the hash.

    so maybe try calling the method above like so if you're dropping the hash concept

    def self.extract_save_contents(data) $space_shooter = data endhowever.. I could be totally off base
  7. OMFG VENKA I could hump your leg like a randy dog right now :D

    There was me away individualizing all the individual elements of the variable. Your way works fine (only done one successful test) but thats all thats needed really. :D

    Thank you so much. I knew there would have been a simple thing I was overlooking. Seems to have been the fact I only have the one variable and have no need for the hash llol

    Anyway, the working code looks like this...

      #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def self.save_game_without_rescue    File.open(filename, "wb") do |file|      Marshal.dump( $space_shooter , file)    end    return true  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def self.load_game_without_rescue    File.open(filename, "rb") do |file|      $space_shooter = Marshal.load(file)    end    return true  end   so simple... <3

    Edit

    Just done a few more quick tests quickly modifying data and its all loading as it should.

    I really appreciate that help there guys. I have spent FAR too many hours trying to solve this. Think im gonna go to bed soon. I've literally been at my keyboard for around 26 hours non-stop (no lie!)
  8. Makes me happy to see you guys found the problem! Now go get some rest Dekita: that's an order :p
  9. LOL! good night and glad it worked :)
  10. lol...

    p Galenmereth.order.valid?  # => falsep reason == :too_far_behind # => trueI just have to code in a little bit more of a scene (what I was actually doing before I realized had no save function lmao) Just a little more code and the game will be finished tomorrow. then just tweaks and such and all will be done ^_^

    FYI - I plan on giving you guys a lil thanks in my game - saving would not have been possible unless this thread got answered as I didnt really have the time to sit and study it - at least not right now lol

    So yea - I will have to remember to do that ;)

    @any mod who cares - This thread can be closed now - much thankies ^_^
  11. Dekita said:
    some kind of error
    Some? You're a programmer. You should know better that isn't helpful.
    Galenmereth is right. Try removing the first one.


    The usual VC Ace save file consists of a header and the “real” saved state. The header is only used in the preview (which is why there isn't any assignment). I'm sure you remember seeing party member graphics and playtime in the load / save screen, don't you?


    You don't have a header yet. Either remove the line or write one if needed.


    Edit: Oops, too late.
  12. I did know better - I was getting either end of file error or a marshal dump 0x5f error < never had that one before and it only happened once when i was messing around to fix the issue. :)

    I had also already removed the line that calls the game header data.

    I'm not too clued up with the whole marshaling data side of ruby so didnt realise I only needed the one line - I just went by the default script for reference :p
  13. the file saving and loading works in a way that every set of data that you dump (one by one) should be loaded one by one too...

    the game uses load twice because it saves 2 times too (saves 1 header then the other data), so if you didn't save the header, you need to remove one of the loads... it got me a while back when I was using it to notice that too
  14. Yea - I understand why it wasnt working properly now. (i think)

    Anyway- its fixed, which is the main thing. The code below works flawlessly :D

    Code:
      def self.save_game_without_rescue    File.open(filename, "wb") { |file|  Marshal.dump( $space_shooter , file) }    return true  end  #-----------------------------------------------------------------------------  #   #-----------------------------------------------------------------------------  def self.load_game_without_rescue    File.open(filename, "rb") { |file| $space_shooter = Marshal.load(file) }    return true  end