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.

[APD] Save - Option Saving

Save - Option SavingVersion: 0.5
By: Abt Ploutôn & Hanmac

Introduction

save values into a save slot
or into a game slot (working for the whole system)
or a pc slot (working for all games witch use this script)

Features
  • APD-CodeSniplet
  • quick-save function
  • autosave

Script

Code:
#==============================================================================

# ** Save - Option Saving V0.5.0

# von Abt Ploutôn & Hanmac

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

#

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

 

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

# * APD überhaupt installiert?

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

unless $APD_install == true

    print("Bitte installieren Sie zuerst die APD.")

    exit

end

 

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

# * modul registieren

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

APD.core_register("save",0.5,0,"Save - Option Saving","Abt Ploutôn & Hanmac")

APD.core_register_cs("cs_file_read")

APD.core_register_cs("cs_file_save")

APD.core_register_cs("cs_system_exit")

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

# * vorrausetzungen festlegen

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

APD.core_exist?("APD","core") {|i| i>0.6 }

 

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

# * erfüllt?

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

if APD.core_activ?

    #dann code schreiben

module APD

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

    # * Setzt benötigte Variablen

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

    def self.save_config

        self["save_path"] = self["core_path"]

        

        self["save_file_ext"] = self["core_file_ext"] #nutzt die core_file_ext oder eigene

        

        self["save_load_map"] = true #lad map wenn frue 

        

        self["save_max_autosave"] = 5

        self["save_autosave_name"] = 'Save%d'

        self["save_quicksave_name"] = 'quick'

        

        self["save_score"] = true #APD + dafür eingetragende Sachen im Speicherstand reinpacken?

        self["save_score_save"] = [] #welche APD-Daten sollen in der Spielstand-Datei gespeichert werden?

        #einfach per Kommaliste eintragen, Bsp. "save_pc_save","save_game_file"

        

        self["save_game"] = true #APD + dafür eingetragende Sachen gesamt fürs Spiel speichern?

        self["save_game_file"] = "apd" #wo sollen alle spielbezogenden Daten gespeichert werden?

        self["save_game_save"] = [] #welche APD-Daten sollen in der game-Datei gespeichert werden?

        #einfach per Kommaliste eintragen, Bsp. "save_pc_save","save_game_file"

        

        self["save_pc"] = false #APD + dafür eingetragende Sachen gesamt im PC speichern?

        self["save_pc_file"] = "apd"

        self["save_pc_save"] = [] #welche APD-Daten sollen in der pc-Datei gespeichert werden?

        #einfach per Kommaliste eintragen, Bsp. "save_pc_save","save_game_file"

        

        self["cs_position"]["save_cs_file_read"] = -1

        self["cs_position"]["save_cs_file_write"] = 1

        #benötigte Variablen

        @save_game = {}

        @save_score = {}

        @save_pc = {}

    end 

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

    # * Gibt den Wert einer Variable zurück, die im Speicher ist

    # für Spielbezogende Werte

    # id = ist der Schlüssel, wo wie was gespeichert wird

    # value = ist der Wert der gesetzt werden soll (optional)

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

    def self.save_game(id, value=nil)

        #was drin?

        @save_game[id] = value if value != nil

        return @save_game[id]

    end

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

    # * Gibt den Wert einer Variable zurück, die im Speicher ist

    # für Spielbezogende Werte

    # id = ist der Schlüssel, wo wie was gespeichert wird

    # value = ist der Wert der gesetzt werden soll (optional)

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

    def self.save_score(id, value=nil)

        #was drin?

        @save_score[id] = value if value != nil

        return @save_score[id]

    end

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

    # * Gibt den Wert einer Variable zurück, die im Speicher ist

    # für PCbezogende Werte

    # id = ist der Schlüssel, wo wie was gespeichert wird

    # id2 = gibt an aus welchem Spiel der Wert kommt, 

    # es wird immer der in APD["core_log_header"] hinterlegte Name zum Speichern genommen. (optional)

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

    def self.save_pc_get(id, id2=APD["core_log_header"])

        return @save_pc[id2][id]

    end

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

    # * Gibt den Wert einer Variable zurück, die im Speicher ist

    # für PCbezogende Werte

    # id = ist der Schlüssel, wo wie was gespeichert wird

    # value = ist der Wert der gesetzt werden soll (optional)

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

    def self.save_pc_set(id, value)

        @save_pc[APD["core_log_header"]][id] = value

    end

    

    def self.save_autosave(file=nil)

        if file.nil?

            filename = APD["save_path"] + APD["save_autosave_name"]

            if APD["save_max_autosave"].nil? || Dir.glob(filename.gsub(/\%.*d/){ "*" }).size < APD["save_max_autosave"]

                #füg hinzu

                i = 0

                while file.nil? or File.exist?(file) do

                    file = sprintf(filename,i)

                    i += 1

                end

            else

                #lösche

                temp = nil

                Dir.glob(filename.gsub(/\%.*d/){ "*" }).reverse_each { |name|

                    unless temp.nil?

                        File.rename(name,temp)

                    else

                        File.delete(name)

                    end

                    temp = name

                }

                self.save_autosave          

            end

        end

        self.save_save_file(file)

    end

    

    def self.save_quick_save

        self.save_save_file(self["save_quicksave_name"])

    end

    

    def self.save_quick_load

        self.save_load_file(self["save_quicksave_name"],true)

    end

    

    def self.save_load_file(filename,load_map = APD["save_load_map"])

        unless filename.is_a?(File)

            temp = APD["save_path"] + filename

            temp += "." + APD["save_file_ext"] if !filename.include?(".") && !APD["save_file_ext"].nil?

            File.open(temp, "r+b") { | file | self.save_load_file(file) }

        else

            file = filename

            if APD["core_vx"]

                characters                   = Marshal.load(file)

                Graphics.frame_count = Marshal.load(file)

                last_bgm                        = Marshal.load(file)

                last_bgs                        = Marshal.load(file)

                $game_system                 = Marshal.load(file)

                $game_message               = Marshal.load(file)

                $game_switches           = Marshal.load(file)

                $game_variables         = Marshal.load(file)

                $game_self_switches = Marshal.load(file)

                $game_actors                 = Marshal.load(file)

                $game_party                 = Marshal.load(file)

                $game_troop                 = Marshal.load(file)

                $game_map                       = Marshal.load(file)

                $game_player                 = Marshal.load(file)

                if $game_system.version_id != $data_system.version_id

                    $game_map.setup($game_map.map_id)

                    $game_player.center($game_player.x, $game_player.y)

                end

            else

                characters = Marshal.load(file)

                # Read frame count for measuring play time

                Graphics.frame_count = Marshal.load(file)

                # Read each type of game object

                $game_system                = Marshal.load(file)

                $game_switches          = Marshal.load(file)

                $game_variables      = Marshal.load(file)

                $game_self_switches = Marshal.load(file)

                $game_screen                = Marshal.load(file)

                $game_actors                = Marshal.load(file)

                $game_party              = Marshal.load(file)

                $game_troop              = Marshal.load(file)

                $game_map                    = Marshal.load(file)

                $game_player                = Marshal.load(file)

                # If magic number is different from when saving

                # (if editing was added with editor)

                if $game_system.magic_number != $data_system.magic_number

                    # Load map

                    $game_map.setup($game_map.map_id)

                    $game_player.center($game_player.x, $game_player.y)

                end

                # Refresh party members

                $game_party.refresh

            end

            #codestellen einbinden

            APD.cs_file_read(file) if APD["save_score"] == true

            $scene = Scene_Map.new if load_map

        end

    end

    

    def self.save_save_file(filename)

        unless filename.is_a?(File)

            temp = APD["save_path"] + filename

            temp += "." + APD["save_file_ext"] if !filename.include?(".")

            File.open(temp, "w+b") { | file | self.save_save_file(file) }

        else

            file = filename

            if APD["core_vx"]

                characters = []

                $game_party.members.each { | actor|

                    characters.push([actor.character_name, actor.character_index])

                }

                $game_system.save_count += 1

                $game_system.version_id = $data_system.version_id

                last_bgm = RPG::BGM.last

                last_bgs = RPG::BGS.last

                Marshal.dump(characters,                     file)

                Marshal.dump(Graphics.frame_count, file)

                Marshal.dump(last_bgm,                      file)

                Marshal.dump(last_bgs,                      file)

                Marshal.dump($game_system,               file)

                Marshal.dump($game_message,             file)

                Marshal.dump($game_switches,             file)

                Marshal.dump($game_variables,           file)

                Marshal.dump($game_self_switches,   file)

                Marshal.dump($game_actors,               file)

                Marshal.dump($game_party,                   file)

                Marshal.dump($game_troop,                   file)

                Marshal.dump($game_map,                     file)

                Marshal.dump($game_player,               file)

            else

                # Make character data for drawing save file

                characters = []

                $game_party.members.each { |actor|

                    characters.push([actor.character_name, actor.character_hue])

                }

                # Write character data for drawing save file

                Marshal.dump(characters, file)

                # Wrire frame count for measuring play time

                Marshal.dump(Graphics.frame_count, file)

                # Increase save count by 1

                $game_system.save_count += 1

                # Save magic number

                # (A random value will be written each time saving with editor)

                $game_system.magic_number = $data_system.magic_number

                # Write each type of game object

                Marshal.dump($game_system, file)

                Marshal.dump($game_switches, file)

                Marshal.dump($game_variables, file)

                Marshal.dump($game_self_switches, file)

                Marshal.dump($game_screen, file)

                Marshal.dump($game_actors, file)

                Marshal.dump($game_party, file)

                Marshal.dump($game_troop, file)

                Marshal.dump($game_map, file)

                Marshal.dump($game_player, file)

            end

            # hier kommt das mit den Bild   

            #codestellen einbinden

            APD.cs_file_save(file) if APD["save_score"] == true

        end

    end

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

    # * startsprungstelle

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

    def self.save_cs_start

        #laden?/game

        temp = APD["save_path"] + APD["save_game_file"]

        temp += "." + APD["save_file_ext"] if !APD["save_game_file"].include?(".") && !APD["save_file_ext"].nil?

        if APD["save_game"] == true

            #laden

            File.open(temp, "r+b") { | game_file |

                load = Marshal.load(game_file)

                @save_game = Marshal.load(game_file)

                APD["save_game_save"].each { |i| APD[i] = load[i] }

            }

        end

        #laden?/pc

        temp = "#{ENV["APPDATA"]}/#{APD["save_pc_file"]}"

        temp += "." + APD["save_file_ext"] if !APD["save_pc_file"].include?(".") && !APD["save_file_ext"].nil?

        if APD["save_pc"] == true

            #laden

            File.open(temp, "rb") { |pc_file| 

                load = Marshal.load(pc_file)

                @save_pc = Marshal.load(pc_file)

                APD["save_pc_save"].each { |i| APD[i] = load[i] }

            }

        end

    end

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

    # * endsprungstelle

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

    def self.save_cs_system_exit

        #speichern?/game

        temp = APD["save_path"] + APD["save_game_file"]

        temp += "." + APD["save_file_ext"] if !APD["save_game_file"].include?(".")       && !APD["save_file_ext"].nil?

        if APD["save_game"] == true and File.readable?(temp)

            save = {}

            APD["save_game_save"].each { |i| save[i] = APD[i] }

            File.open(temp, "w+b") { |game_file|

                Marshal.dump(save,game_file)

                Marshal.dump(@save_game,game_file)

            }

        end

        temp = "#{ENV["APPDATA"]}/#{APD["save_pc_file"]}"

        temp += "." + APD["save_file_ext"] if !APD["save_pc_file"].include?(".") && !APD["save_file_ext"].nil?

        #speichern?/pc

        if APD["save_pc"] == true and File.readable?(temp)

            save = {}

            APD["save_pc_save"].each { |i| save[i] = APD[i] }

            File.open(temp, "wb") { |pc_file|

                Marshal.dump(save, pc_file)

                Marshal.dump(@save_pc, pc_file)

            }

        end

    end

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

    # * Spielstand laden

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

    def self.save_cs_file_read(file)

        load = Marshal.load(file)

        @save_score = Marshal.load(file)

        #wieder eintragen

        APD["save_score_save"].each { |i| APD[i] = load[i] }

    end

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

    # * Spielstand speichern

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

    def self.save_cs_file_save(file)

        save = {}

        APD["save_score_save"].each { |i| save[i] = APD[i] }

        Marshal.dump(save, file)

        Marshal.dump(@save_score,file)      

    end

end

 

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

# * System Exitspeichereinbau, Idee: Leif

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

class SystemExit 

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

    # * Alias

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

    if @apd_initialize.nil?

        alias apd_initialize initialize 

        @apd_initialize = true

    end

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

    # * Überschreibt 

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

    def initialize(status=0,se=nil)

        #print status,se

        apd_initialize(status,se)

        #alles eintragen

        APD.cs_system_exit

    end 

end

 

if APD["core_vx"]

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

    # ** Scene_Files

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

    #   This class performs save and load screen processing.

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

    class Scene_File

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

        # * Alias

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

        if @apd_save_data.nil?

            alias_method(:apd_write_save_data, :write_save_data)

            alias_method(:apd_read_save_data, :read_save_data)

            @apd_save_data = true

        end

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

        # * Write Save Data

        #        file : write file object (opened)

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

        def write_save_data(file)

            apd_write_save_data(file)

            #überhaupt speichern?

            return if APD["save_score"] == false

            #speichern

            #codestellen einbinden

            APD.cs_file_save(file)

        end

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

        # * Read Save Data

        #        file : file object for reading (opened)

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

        def read_save_data(file)

            apd_read_save_data(file)

            #überhaupt laden?

            return if APD["save_score"] == false    

            #codestellen einbinden

            APD.cs_file_read(file)

        end

    end

else

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

    # ** Scene_Save

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

    #   This class performs save screen processing.

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

    class Scene_Save

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

        # * Alias

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

        if @apd_write_save_data.nil?

            alias_method(:apd_write_save_data, :write_save_data)

            @apd_write_save_data = true

        end

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

        # * Write Save Data

        #        file : write file object (opened)

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

        def write_save_data(file)

            apd_write_save_data(file)

            #überhaupt speichern?

            return if APD["save_score"] == false

            #codestellen einbinden

            APD.cs_file_save(file)

        end

    end

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

    # ** Scene_Load

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

    #   This class performs load screen processing.

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

    class Scene_Load

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

        # * Alias

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

        if @apd_read_save_data.nil?

            alias_method(:apd_read_save_data, :read_save_data)

            @apd_read_save_data = true

        end

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

        # * Read Save Data

        #        file : file object for reading (opened)

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

        def read_save_data(file)

            apd_read_save_data(file)

            #überhaupt laden?

            return if APD["save_score"] == false

            #codestellen einbinden

            APD.cs_file_read(file)

        end

    end

end

 

end

APD.config

Compatibility

it does not support any scripts witch overrides save functions
and it does not have a GUI (im working on)

Author's Notes

it is still in development so if you use it please notic that you use it on your own risk
its a APD script. so you need this: viewtopic.php?f=11&t=63186
Terms and Conditions

you can use it if you credit us
please call us if you indend to use it in a Commercial game
 

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